Raise 400 error for invalid contributor param type #433
Conversation
0b1d533
to
e27725c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 tested, works well.
I tried briefly to see if this could be done with validators, but they would require having a serializer for the payload which we currently don't.
On one hand that feels like a heavier lift. On another, maybe it would be a good idea to have well-defined parameters. Maybe for a future task.
Good point. It appears that we can do this using a serializer on its own -- https://tinyendian.com/articles/parsing-query-parameters-in-rest-framework/ -- so I will update this to do that. |
494e2d0
to
bbf02ed
Compare
bbf02ed is an amended commit that users a custom serializer + the built-in ValidationError for handling errors related to the queryparams |
Add a FacilityQueryParamsSerializer to validate the types of the querystring parameters provided to the /api/facilities/ endpoint & send back a 400 error if any of the parameters has an invalid type.
bbf02ed
to
c3f867b
Compare
Rebased on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 clean, elegant solution.
Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was delighted by how simple and declarative the serializer solution was but I wondered how it was dealing with the fact that a query string argument could appear multiple times. This lead me to an edge case:
This returns 400 like we want
http://localhost:8081/api/facilities/?contributors=1&contributors=two
This, unfortunately, still returns 500
http://localhost:8081/api/facilities/?contributors=one&contributors=2
Oof, I merged the PR before I saw this. Will make a new PR to fix. |
Overview
Raise a 400 error if not all of the contributor querystring parameters
are integers, sending back a detailed error response about the type.
The other custom parameters already expect strings, so they don't fail
in the same way, and the pagination params have their own error
handling. Therefore we don't need to add similar handling to the other
errors.
Connects #424
Demo
Testing Instructions
Checklist
fixup!
commits have been squashed