stac-utils / stac-fastapi

STAC API implementation with FastAPI.
https://stac-utils.github.io/stac-fastapi/
MIT License
238 stars 100 forks source link

Response models are not being used to validate responses #461

Closed drnextgis closed 1 month ago

drnextgis commented 1 year ago

I've got confused by the fact that response models are not being used to validate responses. Isn't that its main purpose? As you can see here for example response model is Collection. But if we modify collection's content in the database (pgstac) in order not to be align with the response model stac-fastapi simple ignores it and returns what we have in the database without any warning/error:

$ postgis=# UPDATE collections SET content = JSONB_SET(content, '{type}', '"MyCollection"');
$ curl -s -X 'GET' \
  'http://localhost:8082/collections/joplin' \
  -H 'accept: application/json' | jq '.type'
"MyCollection"

But according to the response model type should be Collection.

ENABLE_RESPONSE_MODELS set to true

gadomski commented 1 year ago

I believe you're correct -- I've opened https://github.com/stac-utils/stac-fastapi/pull/519 to track the issue. Currently I just have a failing test to demonstrate the issue.

vincentsarago commented 4 months ago

I confirmed this.

The thing was that we are using wrap_response to make sure the endpoints return a starlette Response, but this issue is then FastAPI will not run pydantic validation/serialization if the endpoint respond a Response object

https://github.com/stac-utils/stac-fastapi/blob/4fb10ec6ba758f28ddef749ea7bac6fbb5963f9b/stac_fastapi/api/stac_fastapi/api/routes.py#L18-L24

I've changed this in https://github.com/thomas-maschler/stac-fastapi/pull/2 for next stac-fastapi major version and will also change this for ~2.4 (maybe 2.5) https://github.com/stac-utils/stac-fastapi/pull/650#issuecomment-2042798205

vincentsarago commented 1 month ago

I believe this is not fixed and tested