stac-fastapi icon indicating copy to clipboard operation
stac-fastapi copied to clipboard

`stac_types.Collection` .vs `stac_pydantic.Collection`

Open iliion opened this issue 2 years ago • 3 comments

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 avatar Oct 17 '22 11:10 iliion

@iliion can you please post the error / traceback?

geospatial-jeff avatar Oct 17 '22 13:10 geospatial-jeff

In the following Image you see a TypeError when I override the getCollection method.

image

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)

iliion avatar Oct 17 '22 13:10 iliion

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).

geospatial-jeff avatar Oct 18 '22 14:10 geospatial-jeff

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.

gadomski avatar Jan 31 '23 15:01 gadomski