Skip to content

Don't overwrite links before persisting Item/Collection in database #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)