From 98a218617c00b85e19752ed0ce15aacf10b1636a Mon Sep 17 00:00:00 2001 From: Stijn Caerts Date: Wed, 13 Mar 2024 15:52:45 +0100 Subject: [PATCH] do not overwrite links before persisting Item/Collection in database --- CHANGELOG.md | 2 ++ stac_fastapi/core/stac_fastapi/core/core.py | 16 +++++-------- .../core/stac_fastapi/core/serializers.py | 24 +++++++++++++++---- .../tests/resources/test_collection.py | 16 +++++++++++++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f32cea9..9c5dc40c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed +- Do not overwrite links in Item and Collection objects before persisting in database [#210](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/issues/210) + ## [v2.1.0] diff --git a/stac_fastapi/core/stac_fastapi/core/core.py b/stac_fastapi/core/stac_fastapi/core/core.py index 99b58e16..c27e6859 100644 --- a/stac_fastapi/core/stac_fastapi/core/core.py +++ b/stac_fastapi/core/stac_fastapi/core/core.py @@ -37,7 +37,6 @@ from stac_fastapi.types.config import Settings from stac_fastapi.types.conformance import BASE_CONFORMANCE_CLASSES from stac_fastapi.types.extension import ApiExtension -from stac_fastapi.types.links import CollectionLinks from stac_fastapi.types.requests import get_base_url from stac_fastapi.types.search import BaseSearchPostRequest from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection @@ -731,10 +730,9 @@ async def create_collection( ConflictError: If the collection already exists. """ base_url = str(kwargs["request"].base_url) - collection_links = CollectionLinks( - collection_id=collection["id"], base_url=base_url - ).create_links() - collection["links"] = collection_links + collection = self.database.collection_serializer.stac_to_db( + collection, base_url + ) await self.database.create_collection(collection=collection) return CollectionSerializer.db_to_stac(collection, base_url) @@ -767,11 +765,9 @@ async def update_collection( "collection_id", collection["id"] ) - collection_links = CollectionLinks( - collection_id=collection["id"], base_url=base_url - ).create_links() - collection["links"] = collection_links - + collection = self.database.collection_serializer.stac_to_db( + collection, base_url + ) await self.database.update_collection( collection_id=collection_id, collection=collection ) diff --git a/stac_fastapi/core/stac_fastapi/core/serializers.py b/stac_fastapi/core/stac_fastapi/core/serializers.py index 8e83ef7c..ba588025 100644 --- a/stac_fastapi/core/stac_fastapi/core/serializers.py +++ b/stac_fastapi/core/stac_fastapi/core/serializers.py @@ -60,11 +60,7 @@ def stac_to_db(cls, stac_data: stac_types.Item, base_url: str) -> stac_types.Ite Returns: stac_types.Item: The database-ready STAC item object. """ - item_links = ItemLinks( - collection_id=stac_data["collection"], - item_id=stac_data["id"], - base_url=base_url, - ).create_links() + item_links = resolve_links(stac_data.get("links", []), base_url) stac_data["links"] = item_links now = now_to_rfc3339_str() @@ -111,6 +107,24 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item: class CollectionSerializer(Serializer): """Serialization methods for STAC collections.""" + @classmethod + def stac_to_db( + cls, collection: stac_types.Collection, base_url: str + ) -> stac_types.Collection: + """ + Transform STAC Collection to database-ready STAC collection. + + Args: + stac_data: the STAC Collection object to be transformed + base_url: the base URL for the STAC API + + Returns: + stac_types.Collection: The database-ready STAC Collection object. + """ + collection = deepcopy(collection) + collection["links"] = resolve_links(collection.get("links", []), base_url) + return collection + @classmethod def db_to_stac(cls, collection: dict, base_url: str) -> stac_types.Collection: """Transform database model to STAC collection. diff --git a/stac_fastapi/tests/resources/test_collection.py b/stac_fastapi/tests/resources/test_collection.py index dbd88da2..39945451 100644 --- a/stac_fastapi/tests/resources/test_collection.py +++ b/stac_fastapi/tests/resources/test_collection.py @@ -1,3 +1,4 @@ +import copy import uuid import pystac @@ -163,3 +164,18 @@ async def test_pagination_collection(app_client, ctx, txn_client): # Confirm we have paginated through all collections assert collection_ids == ids + + +@pytest.mark.asyncio +async def test_links_collection(app_client, ctx, txn_client): + await delete_collections_and_items(txn_client) + collection = copy.deepcopy(ctx.collection) + collection["links"].append( + {"href": "https://landsat.usgs.gov/", "rel": "license", "type": "text/html"} + ) + await create_collection(txn_client, collection=collection) + response = await app_client.get(f"/collections/{collection['id']}") + assert ( + len([link for link in response.json()["links"] if link["rel"] == "license"]) + == 1 + )