Skip to content

Commit 5074314

Browse files
Fixes based on coderabbit review
1 parent c002fa2 commit 5074314

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ const FilterOperations = [
9292

9393
type FilterOperationType = (typeof FilterOperations)[number] | undefined;
9494

95+
const prismaIdDivider = '_';
96+
9597
registerCustomSerializers();
9698

9799
/**
@@ -216,7 +218,7 @@ class RequestHandler extends APIHandlerBase {
216218

217219
constructor(private readonly options: Options) {
218220
super();
219-
this.idDivider = options.idDivider ?? '_';
221+
this.idDivider = options.idDivider ?? prismaIdDivider;
220222
const segmentCharset = options.urlSegmentNameCharset ?? 'a-zA-Z0-9-_~ %';
221223
this.urlPatterns = this.buildUrlPatterns(this.idDivider, segmentCharset);
222224
}
@@ -1131,7 +1133,7 @@ class RequestHandler extends APIHandlerBase {
11311133
if (ids.length === 0) {
11321134
return undefined;
11331135
} else {
1134-
return data[ids.map((id) => id.name).join(this.idDivider)];
1136+
return data[this.makeIdKey(ids)];
11351137
}
11361138
}
11371139

@@ -1233,7 +1235,7 @@ class RequestHandler extends APIHandlerBase {
12331235
} else {
12341236
return {
12351237
// TODO: support `@@id` with custom name
1236-
[idFields.map((idf) => idf.name).join('_')]: idFields.reduce(
1238+
[idFields.map((idf) => idf.name).join(prismaIdDivider)]: idFields.reduce(
12371239
(acc, curr, idx) => ({
12381240
...acc,
12391241
[curr.name]: this.coerce(curr.type, resourceId.split(this.idDivider)[idx]),
@@ -1257,7 +1259,7 @@ class RequestHandler extends APIHandlerBase {
12571259

12581260
private makePrismaIdKey(idFields: FieldInfo[]) {
12591261
// TODO: support `@@id` with custom name
1260-
return idFields.map((idf) => idf.name).join('_');
1262+
return idFields.map((idf) => idf.name).join(prismaIdDivider);
12611263
}
12621264

12631265
private makeCompoundId(idFields: FieldInfo[], item: any) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,7 @@ describe('REST server tests', () => {
25322532
model User {
25332533
email String
25342534
role Role
2535+
enabled Boolean @default(true)
25352536
25362537
@@id([email, role])
25372538
}
@@ -2553,7 +2554,7 @@ describe('REST server tests', () => {
25532554
endpoint: 'http://localhost/api',
25542555
pageSize: 5,
25552556
idDivider,
2556-
urlSegmentNameCharset: 'a-zA-Z0-9-_~ %@.',
2557+
urlSegmentNameCharset: 'a-zA-Z0-9-_~ %@.:',
25572558
});
25582559
handler = (args) =>
25592560
_handler({ ...args, zodSchemas, modelMeta, url: new URL(`http://localhost/${args.path}`) });
@@ -2624,13 +2625,14 @@ describe('REST server tests', () => {
26242625
requestBody: {
26252626
data: {
26262627
type: 'user',
2627-
attributes: { role: 'ADMIN_USER' },
2628+
attributes: { enabled: false },
26282629
},
26292630
},
26302631
prisma,
26312632
});
26322633

26332634
expect(r.status).toBe(200);
2635+
expect(r.body.data.attributes.enabled).toBe(false);
26342636
});
26352637
});
26362638
});

0 commit comments

Comments
 (0)