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

use simplejson to allow NaN/inf/-inf in JSON response #374

Merged
merged 2 commits into from
Sep 23, 2021

Conversation

vincentsarago
Copy link
Member

@vincentsarago vincentsarago commented Sep 22, 2021

ref #373 and tiangolo/fastapi#3417

By default FastAPI/starlette will raise an error when we try to JSON encode float('nan') or +/-float('inf').

Recently in rio-tiler we added nodata_value in rio_tiler.models.Info which can sometimes be set to Nan/inf/-inf. But because of FastAPI/starlette defaults, titiler was failing to return response for /info and /metadata.

This PR adds a custom JSONResponse using the great simplejson (which will translate NaN/inf/-inf values to null).

Another solution would have been to change rio-tiler behaviour to stringify nodata value but in my opinion this is an application issues not rio-tiler's

ensure_ascii=False,
allow_nan=False,
indent=None,
ignore_nan=True,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vincentsarago
Copy link
Member Author

Note: in the last commit I choose to use the custom JSONResponse directly in the tiler factories directly to kinda force the response. I think this is safer choice.

@vincentsarago vincentsarago merged commit a9dbf5b into master Sep 23, 2021
@vincentsarago vincentsarago deleted the customJSONResponseForNaN branch September 23, 2021 08:29
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

Successfully merging this pull request may close these issues.

None yet

1 participant