diff --git a/packages/plugins/openapi/src/rest-generator.ts b/packages/plugins/openapi/src/rest-generator.ts index 86ed9736a..e6da0268b 100644 --- a/packages/plugins/openapi/src/rest-generator.ts +++ b/packages/plugins/openapi/src/rest-generator.ts @@ -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 = {}; const relationships: Record = {}; @@ -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'); } diff --git a/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml index 0ea258018..b6e0ad750 100644 --- a/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml @@ -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: diff --git a/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml index 9bd34467c..364062e4e 100644 --- a/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml @@ -3135,7 +3135,6 @@ components: type: object description: The "PostLike" model required: - - id - type - attributes properties: diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index a2df2707f..1107fbc64 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -1071,7 +1071,7 @@ class RequestHandler extends APIHandlerBase { return this.makeError('invalidRelationData'); } updatePayload.data[key] = { - set: { + connect: { [this.makePrismaIdKey(relationInfo.idFields)]: data.data.id, }, }; diff --git a/packages/server/tests/api/rest.test.ts b/packages/server/tests/api/rest.test.ts index ec974494d..7367a4b64 100644 --- a/packages/server/tests/api/rest.test.ts +++ b/packages/server/tests/api/rest.test.ts @@ -1742,7 +1742,6 @@ describe('REST server tests', () => { requestBody: { data: { type: 'postLike', - id: `1${idDivider}user1`, attributes: { userId: 'user1', postId: 1, superLike: false }, }, }, @@ -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: 'user1@abc.com' } }); + 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: 'user1@abc.com' } }); await prisma.post.create({