From d61b22727b826cca329efbd0df48cd71e2fb4394 Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Tue, 22 Oct 2024 13:09:36 +0200 Subject: [PATCH 1/3] Add test case for empty data in put response --- packages/server/tests/api/rest.test.ts | 82 ++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/packages/server/tests/api/rest.test.ts b/packages/server/tests/api/rest.test.ts index 640dcbebe..def6460af 100644 --- a/packages/server/tests/api/rest.test.ts +++ b/packages/server/tests/api/rest.test.ts @@ -305,6 +305,40 @@ describe('REST server tests', () => { }); }); + it('returns an empty data array when loading empty related resources', async () => { + // Create a user first + await prisma.user.create({ + data: { myId: 'user1', email: 'user1@abc.com' }, + }); + + const r = await handler({ + method: 'get', + path: '/user/user1', + prisma, + }); + + expect(r.status).toBe(200); + expect(r.body).toMatchObject({ + data: { + type: 'user', + id: 'user1', + attributes: { email: 'user1@abc.com' }, + links: { + self: 'http://localhost/api/user/user1', + }, + relationships: { + posts: { + links: { + self: 'http://localhost/api/user/user1/relationships/posts', + related: 'http://localhost/api/user/user1/posts', + }, + data: [], + }, + }, + }, + }); + }); + it('fetches a related resource with a compound ID', async () => { await prisma.user.create({ data: { @@ -1785,6 +1819,54 @@ describe('REST server tests', () => { }); }); + it("returns an empty data list in relationships if it's empty", async () => { + await prisma.user.create({ + data: { + myId: 'user1', + email: 'user1@abc.com', + }, + }); + + const r = await handler({ + method: 'put', + path: '/user/user1', + query: {}, + requestBody: { + data: { + type: 'user', + attributes: { email: 'user2@abc.com' }, + }, + }, + prisma, + }); + + expect(r.status).toBe(200); + expect(r.body).toMatchObject({ + links: { + self: 'http://localhost/api/user/user1', + }, + data: { + type: 'user', + id: 'user1', + attributes: { + email: 'user2@abc.com', + }, + links: { + self: 'http://localhost/api/user/user1', + }, + relationships: { + posts: { + links: { + self: 'http://localhost/api/user/user1/relationships/posts', + related: 'http://localhost/api/user/user1/posts', + }, + data: [], + }, + }, + }, + }); + }); + it('returns 404 if the user does not exist', async () => { const r = await handler({ method: 'put', From 5157f442f3d3c71812b39e06b05575bc5c97dc78 Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Tue, 22 Oct 2024 14:52:11 +0200 Subject: [PATCH 2/3] Add includes to update result payload --- packages/server/src/api/rest/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index 42bf0ef5c..4d1d24c40 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -970,6 +970,10 @@ class RequestHandler extends APIHandlerBase { } } + // include IDs of relation fields so that they can be serialized. + // This ensures valid relationships are included in the result payload. + this.includeRelationshipIds(type, updatePayload, 'include'); + const entity = await prisma[type].update(updatePayload); return { status: 200, From c6cc84fa7088ed978b421b6ff8728ee8240d4859 Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Tue, 22 Oct 2024 15:02:15 +0200 Subject: [PATCH 3/3] Add fix to create, remove redundant comment --- packages/server/src/api/rest/index.ts | 4 +++- packages/server/tests/api/rest.test.ts | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index 4d1d24c40..2e0bcaec5 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -799,6 +799,9 @@ class RequestHandler extends APIHandlerBase { } } + // include IDs of relation fields so that they can be serialized. + this.includeRelationshipIds(type, createPayload, 'include'); + const entity = await prisma[type].create(createPayload); return { status: 201, @@ -971,7 +974,6 @@ class RequestHandler extends APIHandlerBase { } // include IDs of relation fields so that they can be serialized. - // This ensures valid relationships are included in the result payload. this.includeRelationshipIds(type, updatePayload, 'include'); const entity = await prisma[type].update(updatePayload); diff --git a/packages/server/tests/api/rest.test.ts b/packages/server/tests/api/rest.test.ts index def6460af..3fee62d9a 100644 --- a/packages/server/tests/api/rest.test.ts +++ b/packages/server/tests/api/rest.test.ts @@ -1461,7 +1461,21 @@ describe('REST server tests', () => { expect(r.status).toBe(201); expect(r.body).toMatchObject({ jsonapi: { version: '1.1' }, - data: { type: 'user', id: 'user1', attributes: { email: 'user1@abc.com' } }, + data: { + type: 'user', + id: 'user1', + attributes: { email: 'user1@abc.com' }, + relationships: { + posts: { + links: { + self: 'http://localhost/api/user/user1/relationships/posts', + related: 'http://localhost/api/user/user1/posts', + }, + data: [], + }, + }, + links: { self: 'http://localhost/api/user/user1' }, + }, }); });