From 7b2b19147e033e328fa189a3b9cabac31ef5571d Mon Sep 17 00:00:00 2001 From: Stijn Caerts Date: Wed, 4 Sep 2024 12:57:21 +0200 Subject: [PATCH] Aggregation self link with collection specified (#295) **Related Issue(s):** N/A **Description:** Fixes the `self` link in the response of the `/collections/{collection_id}/aggregations` request. The collection ID segment was missing in the URL because of the relative path URL construction. **PR Checklist:** - [x] Code is formatted and linted (run `pre-commit run --all-files`) - [x] Tests pass (run `make test`) - [x] Documentation has been updated to reflect changes, if applicable - [x] Changes are added to the changelog --- CHANGELOG.md | 1 + .../core/stac_fastapi/core/extensions/aggregation.py | 2 +- stac_fastapi/tests/extensions/test_aggregation.py | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c30ffaa..299d8ba1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added ### Changed +- Fixed the `self` link for the `/collections/{collection_id}/aggregations` endpoint. [#295](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/295) ## [v3.1.0] - 2024-09-02 diff --git a/stac_fastapi/core/stac_fastapi/core/extensions/aggregation.py b/stac_fastapi/core/stac_fastapi/core/extensions/aggregation.py index 241373e7..27f6b458 100644 --- a/stac_fastapi/core/stac_fastapi/core/extensions/aggregation.py +++ b/stac_fastapi/core/stac_fastapi/core/extensions/aggregation.py @@ -143,7 +143,7 @@ async def get_aggregations(self, collection_id: Optional[str] = None, **kwargs): { "rel": "self", "type": "application/json", - "href": urljoin(collection_endpoint, "aggregations"), + "href": urljoin(collection_endpoint + "/", "aggregations"), }, ] ) diff --git a/stac_fastapi/tests/extensions/test_aggregation.py b/stac_fastapi/tests/extensions/test_aggregation.py index dd471159..94a5c247 100644 --- a/stac_fastapi/tests/extensions/test_aggregation.py +++ b/stac_fastapi/tests/extensions/test_aggregation.py @@ -1,4 +1,5 @@ import os +from urllib.parse import urlparse import pytest @@ -68,6 +69,11 @@ async def test_get_collection_aggregations(app_client, ctx, load_test_data): resp = await app_client.get(f"/collections/{test_collection['id']}/aggregations") assert resp.status_code == 200 assert len(resp.json()["aggregations"]) == 15 + rj = resp.json() + href_self = urlparse( + next(link["href"] for link in rj["links"] if link["rel"] == "self") + ) + assert href_self.path == f"/collections/{test_collection['id']}/aggregations" resp = await app_client.delete(f"/collections/{test_collection['id']}") assert resp.status_code == 204 @@ -86,6 +92,11 @@ async def test_post_collection_aggregations(app_client, ctx, load_test_data): resp = await app_client.post(f"/collections/{test_collection['id']}/aggregations") assert resp.status_code == 200 assert len(resp.json()["aggregations"]) == 15 + rj = resp.json() + href_self = urlparse( + next(link["href"] for link in rj["links"] if link["rel"] == "self") + ) + assert href_self.path == f"/collections/{test_collection['id']}/aggregations" resp = await app_client.delete(f"/collections/{test_collection['id']}") assert resp.status_code == 204