Skip to content

Commit 21affec

Browse files
authored
fix: rest-api, wrong links generated for to-one relationship (#481)
1 parent 7de7623 commit 21affec

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

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

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,43 +1042,19 @@ class RequestHandler {
10421042
{
10431043
relatedName: field,
10441044
linkers: {
1045-
related: new Linker((primary, related) =>
1046-
!related || Array.isArray(related)
1047-
? this.makeLinkUrl(
1048-
`/${lowerCaseFirst(model)}/${this.getId(
1049-
model,
1050-
primary,
1051-
modelMeta
1052-
)}/${field}`
1053-
)
1054-
: this.makeLinkUrl(
1055-
`/${lowerCaseFirst(model)}/${this.getId(
1056-
model,
1057-
primary,
1058-
modelMeta
1059-
)}/${field}/${this.getId(fieldMeta.type, related, modelMeta)}`
1060-
)
1045+
related: new Linker((primary) =>
1046+
this.makeLinkUrl(
1047+
`/${lowerCaseFirst(model)}/${this.getId(model, primary, modelMeta)}/${field}`
1048+
)
10611049
),
1062-
relationship: new Linker((primary, related) =>
1063-
!related || Array.isArray(related)
1064-
? this.makeLinkUrl(
1065-
`/${lowerCaseFirst(model)}/${this.getId(
1066-
model,
1067-
primary,
1068-
modelMeta
1069-
)}/relationships/${field}`
1070-
)
1071-
: this.makeLinkUrl(
1072-
`/${lowerCaseFirst(model)}/${this.getId(
1073-
model,
1074-
primary,
1075-
modelMeta
1076-
)}/relationships/${field}/${this.getId(
1077-
fieldMeta.type,
1078-
related,
1079-
modelMeta
1080-
)}`
1081-
)
1050+
relationship: new Linker((primary) =>
1051+
this.makeLinkUrl(
1052+
`/${lowerCaseFirst(model)}/${this.getId(
1053+
model,
1054+
primary,
1055+
modelMeta
1056+
)}/relationships/${field}`
1057+
)
10821058
),
10831059
},
10841060
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,8 +1386,8 @@ describe('REST server tests - regular prisma', () => {
13861386
relationships: {
13871387
author: {
13881388
links: {
1389-
self: 'http://localhost/api/post/1/relationships/author/user1',
1390-
related: 'http://localhost/api/post/1/author/user1',
1389+
self: 'http://localhost/api/post/1/relationships/author',
1390+
related: 'http://localhost/api/post/1/author',
13911391
},
13921392
data: { type: 'user', id: 'user1' },
13931393
},

0 commit comments

Comments
 (0)