Skip to content

Commit

Permalink
Fix: Cast id to int on plurals
Browse files Browse the repository at this point in the history
Threat the request param as integer
closes typicode#925, closes typicode#396
  • Loading branch information
Filippo Conti committed Mar 19, 2019
1 parent 93e4011 commit 5f495f0
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion __tests__/server/plural-with-custom-foreign-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('Server with custom foreign key', () => {
.post('/posts/1/comments')
.send({ body: 'foo' })
.expect('Content-Type', /json/)
.expect({ id: 4, post_id: '1', body: 'foo' })
.expect({ id: 4, post_id: 1, body: 'foo' })
.expect(201))
})

Expand Down
2 changes: 1 addition & 1 deletion __tests__/server/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ describe('Server', () => {
.post('/posts/1/comments')
.send({ body: 'foo' })
.expect('Content-Type', /json/)
.expect({ id: 6, postId: '1', body: 'foo' })
.expect({ id: 6, postId: 1, body: 'foo' })
.expect(201))
})

Expand Down
2 changes: 1 addition & 1 deletion src/server/router/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = opts => {
// Rewrite URL (/:resource/:id/:nested -> /:nested) and request body
function post(req, res, next) {
const prop = pluralize.singular(req.params.resource)
req.body[`${prop}${opts.foreignKeySuffix}`] = req.params.id
req.body[`${prop}${opts.foreignKeySuffix}`] = parseInt(req.params.id)

This comment has been minimized.

Copy link
@wilgert

wilgert Aug 15, 2019

Great that you tackled this isssue!

You should:

  1. Always provide a radix when doing parseInt. See: https://stackoverflow.com/a/6611870/357601
  2. Handle when id is not a number.

This comment has been minimized.

Copy link
@b4dnewz

b4dnewz Aug 15, 2019

Member

I completely forgot that id can be non numeric also.. I'll update this and put some tests for that too, thanks!

req.url = `/${req.params.nested}`
next()
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/router/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ module.exports = (db, name, opts) => {
// PUT /name/:id
// PATCH /name/:id
function update(req, res, next) {
const id = req.params.id
const id = parseInt(req.params.id)

This comment has been minimized.

Copy link
@wilgert

wilgert Aug 15, 2019

See comment on nested.js

let resource

if (opts._isFake) {
Expand Down

0 comments on commit 5f495f0

Please sign in to comment.