Skip to content

Commit

Permalink
fix(server): rest handler not returning correct id when model with co…
Browse files Browse the repository at this point in the history
…mpound id is read as a relation (#1784)
  • Loading branch information
ymc9 authored Oct 16, 2024
1 parent c0ab830 commit 4fc4cf7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
44 changes: 28 additions & 16 deletions packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DbClientContract,
FieldInfo,
PrismaErrorCode,
clone,
enumerate,
getIdFields,
isPrismaClientKnownRequestError,
Expand Down Expand Up @@ -1120,22 +1121,8 @@ class RequestHandler extends APIHandlerBase {
throw new Error(`serializer not found for model ${model}`);
}

const typeInfo = this.typeMap[model];

let itemsWithId: any = items;
if (typeInfo.idFields.length > 1 && Array.isArray(items)) {
itemsWithId = items.map((item: any) => {
return {
...item,
[this.makeIdKey(typeInfo.idFields)]: this.makeCompoundId(typeInfo.idFields, item),
};
});
} else if (typeInfo.idFields.length > 1 && typeof items === 'object') {
itemsWithId = {
...items,
[this.makeIdKey(typeInfo.idFields)]: this.makeCompoundId(typeInfo.idFields, items),
};
}
const itemsWithId = clone(items);
this.injectCompoundId(model, itemsWithId);

// serialize to JSON:API structure
const serialized = await serializer.serialize(itemsWithId, options);
Expand All @@ -1154,6 +1141,31 @@ class RequestHandler extends APIHandlerBase {
return result;
}

private injectCompoundId(model: string, items: unknown) {
const typeInfo = this.typeMap[lowerCaseFirst(model)];
if (!typeInfo) {
return;
}

// recursively traverse the entity to create synthetic ID field for models with compound ID
enumerate(items).forEach((item: any) => {
if (!item) {
return;
}

if (typeInfo.idFields.length > 1) {
item[this.makeIdKey(typeInfo.idFields)] = this.makeCompoundId(typeInfo.idFields, item);
}

for (const [key, value] of Object.entries(item)) {
if (typeInfo.relationships[key]) {
// field is a relationship, recurse
this.injectCompoundId(typeInfo.relationships[key].type, value);
}
}
});
}

private toPlainObject(data: any): any {
if (data === undefined || data === null) {
return data;
Expand Down
34 changes: 34 additions & 0 deletions packages/server/tests/api/rest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,40 @@ describe('REST server tests', () => {
},
});
});

it('get as relationship', async () => {
const r = await handler({
method: 'get',
path: `/post/1`,
query: { include: 'likes' },
prisma,
});

expect(r.status).toBe(200);
expect(r.body).toMatchObject({
data: {
relationships: {
likes: {
data: [{ type: 'postLike', id: `1${idDivider}user2` }],
},
},
},
included: [
expect.objectContaining({
type: 'postLike',
id: '1_user2',
attributes: {
postId: 1,
userId: 'user2',
superLike: false,
},
links: {
self: 'http://localhost/api/postLike/1_user2',
},
}),
],
});
});
});
});

Expand Down

0 comments on commit 4fc4cf7

Please sign in to comment.