Skip to content

Commit

Permalink
Merge pull request #211 from StijnCaerts/#210-links
Browse files Browse the repository at this point in the history
Don't overwrite links before persisting Item/Collection in database
  • Loading branch information
jonhealy1 authored Mar 15, 2024
2 parents 0c7cfcd + 98a2186 commit f34e249
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
16 changes: 6 additions & 10 deletions stac_fastapi/core/stac_fastapi/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down
24 changes: 19 additions & 5 deletions stac_fastapi/core/stac_fastapi/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions stac_fastapi/tests/resources/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import uuid

import pystac
Expand Down Expand Up @@ -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
)

0 comments on commit f34e249

Please sign in to comment.