Skip to content

Commit 545898b

Browse files
fix(openapi): bugfix for compound id create and update (#1855)
1 parent caac46c commit 545898b

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

packages/plugins/openapi/src/rest-generator.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -857,10 +857,8 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
857857

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

865863
const attributes: Record<string, OAPI.SchemaObject> = {};
866864
const relationships: Record<string, OAPI.ReferenceObject | OAPI.SchemaObject> = {};
@@ -911,7 +909,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
911909
if (mode === 'create') {
912910
// 'id' is required if there's no default value
913911
const idFields = model.fields.filter((f) => isIdField(f));
914-
if (idFields.length && idFields.every((f) => !hasAttribute(f, '@default'))) {
912+
if (idFields.length === 1 && !hasAttribute(idFields[0], '@default')) {
915913
properties = { id: { type: 'string' }, ...properties };
916914
toplevelRequired.unshift('id');
917915
}

packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,14 +3123,11 @@ components:
31233123
type: object
31243124
description: The "PostLike" model
31253125
required:
3126-
- id
31273126
- type
31283127
- attributes
31293128
properties:
31303129
type:
31313130
type: string
3132-
attributes:
3133-
type: object
31343131
relationships:
31353132
type: object
31363133
properties:

packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3135,7 +3135,6 @@ components:
31353135
type: object
31363136
description: The "PostLike" model
31373137
required:
3138-
- id
31393138
- type
31403139
- attributes
31413140
properties:

packages/server/src/api/rest/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ class RequestHandler extends APIHandlerBase {
10711071
return this.makeError('invalidRelationData');
10721072
}
10731073
updatePayload.data[key] = {
1074-
set: {
1074+
connect: {
10751075
[this.makePrismaIdKey(relationInfo.idFields)]: data.data.id,
10761076
},
10771077
};

packages/server/tests/api/rest.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,6 @@ describe('REST server tests', () => {
17421742
requestBody: {
17431743
data: {
17441744
type: 'postLike',
1745-
id: `1${idDivider}user1`,
17461745
attributes: { userId: 'user1', postId: 1, superLike: false },
17471746
},
17481747
},
@@ -2141,6 +2140,31 @@ describe('REST server tests', () => {
21412140
expect(r.status).toBe(200);
21422141
});
21432142

2143+
it('update the id of an item with compound id', async () => {
2144+
await prisma.user.create({ data: { myId: 'user1', email: '[email protected]' } });
2145+
await prisma.post.create({ data: { id: 1, title: 'Post1' } });
2146+
await prisma.post.create({ data: { id: 2, title: 'Post2' } });
2147+
await prisma.postLike.create({ data: { userId: 'user1', postId: 1, superLike: false } });
2148+
2149+
const r = await handler({
2150+
method: 'put',
2151+
path: `/postLike/1${idDivider}user1`,
2152+
query: {},
2153+
requestBody: {
2154+
data: {
2155+
type: 'postLike',
2156+
relationships: {
2157+
post: { data: { type: 'post', id: 2 } },
2158+
},
2159+
},
2160+
},
2161+
prisma,
2162+
});
2163+
2164+
expect(r.status).toBe(200);
2165+
expect(r.body.data.id).toBe(`2${idDivider}user1`);
2166+
});
2167+
21442168
it('update a single relation', async () => {
21452169
await prisma.user.create({ data: { myId: 'user1', email: '[email protected]' } });
21462170
await prisma.post.create({

0 commit comments

Comments
 (0)