Skip to content

Commit 673b16e

Browse files
Merge pull request #875 from pataar/fix/bindings-mutation
Fix shared route bindings mutation in _substituteBindings
2 parents f92110a + 87864c4 commit 673b16e

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/js/Router.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -321,18 +321,19 @@ export default class Router extends String {
321321
return { ...result, [key]: value };
322322
}
323323

324-
if (!value.hasOwnProperty(bindings[key])) {
325-
if (value.hasOwnProperty('id')) {
326-
// As a fallback, we still accept an 'id' key not explicitly registered as a binding
327-
bindings[key] = 'id';
328-
} else {
329-
throw new Error(
330-
`Ziggy error: object passed as '${key}' parameter is missing route model binding key '${bindings[key]}'.`,
331-
);
332-
}
324+
const binding = value.hasOwnProperty(bindings[key])
325+
? bindings[key]
326+
: value.hasOwnProperty('id')
327+
? 'id'
328+
: undefined;
329+
330+
if (binding === undefined) {
331+
throw new Error(
332+
`Ziggy error: object passed as '${key}' parameter is missing route model binding key '${bindings[key]}'.`,
333+
);
333334
}
334335

335-
return { ...result, [key]: value[bindings[key]] };
336+
return { ...result, [key]: value[binding] };
336337
}, {});
337338
}
338339

tests/js/route.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@ describe('route()', () => {
437437
);
438438
});
439439

440+
test("falling back to 'id' key does not mutate registered route model bindings", () => {
441+
expect(route('postComments.show', [1, { id: 1 }])).toBe(
442+
'https://ziggy.dev/posts/1/comments/1',
443+
);
444+
445+
expect(route('postComments.show', [1, { id: 1, uuid: '1-2-3' }])).toBe(
446+
'https://ziggy.dev/posts/1/comments/1-2-3',
447+
);
448+
});
449+
440450
test('can generate a URL for an app installed in a subfolder', () => {
441451
global.Ziggy.url = 'https://ziggy.dev/subfolder';
442452

0 commit comments

Comments
 (0)