stac-fastapi
stac-fastapi copied to clipboard
`stac_types.Collection` .vs `stac_pydantic.Collection`
In the following line it is evident that the creation of a Collection is tested refferring to stac_pydantic .Collection
https://github.com/stac-utils/stac-fastapi/blob/e6b353a9638f2e2d6030dd409718ff6eecaa176e/stac_fastapi/pgstac/tests/resources/test_collection.py#L8
While in the core.py
the following Collections
type is returned:
https://github.com/stac-utils/stac-fastapi/blob/e6b353a9638f2e2d6030dd409718ff6eecaa176e/stac_fastapi/types/stac_fastapi/types/core.py#L84-L87
which is referring to stac_types.Collection and not stac_pydantic .Collection
I am a bit confused because I want to implement a customized Pydantic model by extending it. This leads to an error because the response of the abstract class is not of stac_types.Collection. How do I correclty extend the model in order to cstomiing to my needs?
@iliion can you please post the error / traceback?
In the following Image you see a TypeError when I override the getCollection
method.
The return type is BDAPCollection
which is a Class that inherits from stac_pydantic.Collection
The implementation of the customised class is:
class BDAPCollection(Collection):
def to_dict(self, **kwargs):
return self.dict(by_alias=True, exclude_unset=True, **kwargs)
def to_json(self, **kwargs):
return self.json(by_alias=True, exclude_unset=True, **kwargs)
The response signature uses a typed dict (stac_types.Collection
). Typed dictionaries are really just dictionaries, you can extend them by adding any key/value pair. For example this should work
def get_collection(self, collection_id: str) -> stac_types.Collection:
return {"type": "Collection", "foo": "bar", "hello": "world"}
Make sure that enable_response_models
is set to False
if you are returning data that isn't a valid collection as otherwise it would fail validation (https://github.com/stac-utils/stac-fastapi/blob/master/stac_fastapi/types/stac_fastapi/types/config.py#L27).
This issue looks like it was answered, so I'm going to close as completed ... @iliion please re-open if @geospatial-jeff's answer was not sufficient to fix your problem.