Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning string instead of JSON when error validating API parameters #328

Open
surfcoders opened this issue Feb 1, 2024 · 2 comments
Open

Comments

@surfcoders
Copy link

Discovered this in v3-alpha, but same thing happens in v2:

When making the following API call with an erroneous parameter (for example limit=-200):

https://api.openaq.org/v3/countries?limit=-200&page=1

It correctly returns an error object as described in the documentation:

{ "detail": [ { "type": "greater_than", "loc": [ "query", "limit" ], "msg": "Input should be greater than 0", "input": -200, "ctx": { "gt": 0 }, "url": "https://errors.pydantic.dev/2.1.2/v/greater_than" } ] }

But when making a call with a different erroneous parameter like:

https://api.openaq.org/v3/countries?limit=null&page=1

It returns as a string:

"[{'type': 'int_parsing', 'loc': ('query', 'limit'), 'msg': 'Input should be a valid integer, unable to parse string as an integer', 'input': 'null', 'url': 'https://errors.pydantic.dev/2.1.2/v/int_parsing'}]"

In addition if not returning a JSON object, I found that:

  1. It does not follow the JSON error response pattern as shown above, and
  2. The 'loc' key returns the correct values, but not as an array ( should be 'loc': ['query', 'limit'] instead of 'loc': ('query**'**, 'limit'))

Thanks in advance

@russbiggs
Copy link
Member

Thanks this is related to #300. These errors are essentially what is returned by Pydantic, which we use for schema validation. Ideally these shouldnt be exposed directly, but instead reshaped to be a bit more consumer friendly. Either way the JSON vs string is odd. Ill take a look, but more than likely ill jut wrap these into a better JSON payload instead of the error directly

@surfcoders
Copy link
Author

Thanks. Keep us posted. I did a workaround to accommodate this. But the string payload cannot be converted to JSON because of using "( )" to represent an array, when it should be "[ ]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants