Skip to content

Commit f34e249

Browse files
authored
Merge pull request #211 from StijnCaerts/#210-links
Don't overwrite links before persisting Item/Collection in database
2 parents 0c7cfcd + 98a2186 commit f34e249

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515

1616
### Fixed
1717

18+
- 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)
19+
1820

1921
## [v2.1.0]
2022

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from stac_fastapi.types.config import Settings
3838
from stac_fastapi.types.conformance import BASE_CONFORMANCE_CLASSES
3939
from stac_fastapi.types.extension import ApiExtension
40-
from stac_fastapi.types.links import CollectionLinks
4140
from stac_fastapi.types.requests import get_base_url
4241
from stac_fastapi.types.search import BaseSearchPostRequest
4342
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
@@ -731,10 +730,9 @@ async def create_collection(
731730
ConflictError: If the collection already exists.
732731
"""
733732
base_url = str(kwargs["request"].base_url)
734-
collection_links = CollectionLinks(
735-
collection_id=collection["id"], base_url=base_url
736-
).create_links()
737-
collection["links"] = collection_links
733+
collection = self.database.collection_serializer.stac_to_db(
734+
collection, base_url
735+
)
738736
await self.database.create_collection(collection=collection)
739737

740738
return CollectionSerializer.db_to_stac(collection, base_url)
@@ -767,11 +765,9 @@ async def update_collection(
767765
"collection_id", collection["id"]
768766
)
769767

770-
collection_links = CollectionLinks(
771-
collection_id=collection["id"], base_url=base_url
772-
).create_links()
773-
collection["links"] = collection_links
774-
768+
collection = self.database.collection_serializer.stac_to_db(
769+
collection, base_url
770+
)
775771
await self.database.update_collection(
776772
collection_id=collection_id, collection=collection
777773
)

stac_fastapi/core/stac_fastapi/core/serializers.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ def stac_to_db(cls, stac_data: stac_types.Item, base_url: str) -> stac_types.Ite
6060
Returns:
6161
stac_types.Item: The database-ready STAC item object.
6262
"""
63-
item_links = ItemLinks(
64-
collection_id=stac_data["collection"],
65-
item_id=stac_data["id"],
66-
base_url=base_url,
67-
).create_links()
63+
item_links = resolve_links(stac_data.get("links", []), base_url)
6864
stac_data["links"] = item_links
6965

7066
now = now_to_rfc3339_str()
@@ -111,6 +107,24 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
111107
class CollectionSerializer(Serializer):
112108
"""Serialization methods for STAC collections."""
113109

110+
@classmethod
111+
def stac_to_db(
112+
cls, collection: stac_types.Collection, base_url: str
113+
) -> stac_types.Collection:
114+
"""
115+
Transform STAC Collection to database-ready STAC collection.
116+
117+
Args:
118+
stac_data: the STAC Collection object to be transformed
119+
base_url: the base URL for the STAC API
120+
121+
Returns:
122+
stac_types.Collection: The database-ready STAC Collection object.
123+
"""
124+
collection = deepcopy(collection)
125+
collection["links"] = resolve_links(collection.get("links", []), base_url)
126+
return collection
127+
114128
@classmethod
115129
def db_to_stac(cls, collection: dict, base_url: str) -> stac_types.Collection:
116130
"""Transform database model to STAC collection.

stac_fastapi/tests/resources/test_collection.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import uuid
23

34
import pystac
@@ -163,3 +164,18 @@ async def test_pagination_collection(app_client, ctx, txn_client):
163164

164165
# Confirm we have paginated through all collections
165166
assert collection_ids == ids
167+
168+
169+
@pytest.mark.asyncio
170+
async def test_links_collection(app_client, ctx, txn_client):
171+
await delete_collections_and_items(txn_client)
172+
collection = copy.deepcopy(ctx.collection)
173+
collection["links"].append(
174+
{"href": "https://landsat.usgs.gov/", "rel": "license", "type": "text/html"}
175+
)
176+
await create_collection(txn_client, collection=collection)
177+
response = await app_client.get(f"/collections/{collection['id']}")
178+
assert (
179+
len([link for link in response.json()["links"] if link["rel"] == "license"])
180+
== 1
181+
)

0 commit comments

Comments
 (0)