Skip to content

Commit

Permalink
fix(openapi): bugfix for compound id create and update (#1855)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomassnielsen authored Nov 26, 2024
1 parent caac46c commit 545898b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
8 changes: 3 additions & 5 deletions packages/plugins/openapi/src/rest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,8 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {

private generateModelEntity(model: DataModel, mode: 'read' | 'create' | 'update'): OAPI.SchemaObject {
const idFields = model.fields.filter((f) => isIdField(f));
// For compound ids each component is also exposed as a separate fields for read operations,
// but not required for write operations
const fields =
idFields.length > 1 && mode === 'read' ? model.fields : model.fields.filter((f) => !isIdField(f));
// For compound ids each component is also exposed as a separate fields.
const fields = idFields.length > 1 ? model.fields : model.fields.filter((f) => !isIdField(f));

const attributes: Record<string, OAPI.SchemaObject> = {};
const relationships: Record<string, OAPI.ReferenceObject | OAPI.SchemaObject> = {};
Expand Down Expand Up @@ -911,7 +909,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
if (mode === 'create') {
// 'id' is required if there's no default value
const idFields = model.fields.filter((f) => isIdField(f));
if (idFields.length && idFields.every((f) => !hasAttribute(f, '@default'))) {
if (idFields.length === 1 && !hasAttribute(idFields[0], '@default')) {
properties = { id: { type: 'string' }, ...properties };
toplevelRequired.unshift('id');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3123,14 +3123,11 @@ components:
type: object
description: The "PostLike" model
required:
- id
- type
- attributes
properties:
type:
type: string
attributes:
type: object
relationships:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3135,7 +3135,6 @@ components:
type: object
description: The "PostLike" model
required:
- id
- type
- attributes
properties:
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ class RequestHandler extends APIHandlerBase {
return this.makeError('invalidRelationData');
}
updatePayload.data[key] = {
set: {
connect: {
[this.makePrismaIdKey(relationInfo.idFields)]: data.data.id,
},
};
Expand Down
26 changes: 25 additions & 1 deletion packages/server/tests/api/rest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,6 @@ describe('REST server tests', () => {
requestBody: {
data: {
type: 'postLike',
id: `1${idDivider}user1`,
attributes: { userId: 'user1', postId: 1, superLike: false },
},
},
Expand Down Expand Up @@ -2141,6 +2140,31 @@ describe('REST server tests', () => {
expect(r.status).toBe(200);
});

it('update the id of an item with compound id', async () => {
await prisma.user.create({ data: { myId: 'user1', email: '[email protected]' } });
await prisma.post.create({ data: { id: 1, title: 'Post1' } });
await prisma.post.create({ data: { id: 2, title: 'Post2' } });
await prisma.postLike.create({ data: { userId: 'user1', postId: 1, superLike: false } });

const r = await handler({
method: 'put',
path: `/postLike/1${idDivider}user1`,
query: {},
requestBody: {
data: {
type: 'postLike',
relationships: {
post: { data: { type: 'post', id: 2 } },
},
},
},
prisma,
});

expect(r.status).toBe(200);
expect(r.body.data.id).toBe(`2${idDivider}user1`);
});

it('update a single relation', async () => {
await prisma.user.create({ data: { myId: 'user1', email: '[email protected]' } });
await prisma.post.create({
Expand Down

0 comments on commit 545898b

Please sign in to comment.