Skip to content

Commit 6df5f95

Browse files
committed
Merge pull request #90 from digitalsadhu/protect_pk_against_early_delete
Guard agains pk being deleted too early
2 parents 4109c34 + 4067d68 commit 6df5f95

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/serializer.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,13 @@ function parseRelations (data, relations, options) {
180180
// Without this /:collection/:id/:relatedcollection relationship
181181
// object is the same as /:collection/:id's.
182182
if (collection === name) {
183+
183184
// fk should not be leaked out of the server
184-
delete data[fkName];
185+
// however, only delete if fk is not also pk. Pk will be cleaned up later
186+
// and cleaning up too early results in no resource id
187+
if (fkName !== options.primaryKeyField) {
188+
delete data[fkName];
189+
}
185190
relationships = parseRelations(data, relation.modelTo.relations, options);
186191
return false;
187192
}
@@ -206,8 +211,13 @@ function parseRelations (data, relations, options) {
206211
var relationship = null;
207212

208213
if (!_.isUndefined(fk)) {
214+
209215
// fk should not be leaked out of the server
210-
delete data[fkName];
216+
// however, only delete if fk is not also pk. Pk will be cleaned up later
217+
// and cleaning up too early results in no resource id
218+
if (fkName !== options.primaryKeyField) {
219+
delete data[fkName];
220+
}
211221

212222
if (_.isArray(fk)) {
213223
relationship = makeRelations(toType, fk, options);

0 commit comments

Comments
 (0)