From a5aadeb612c9261d471a9ba92a418f0431d4e2f2 Mon Sep 17 00:00:00 2001 From: Joshua Benning Date: Sat, 21 Nov 2026 15:33:24 +0100 Subject: [PATCH] server: Align endpoints with specification Previously two endpoints of the discovery API deviated from the specification. - `GET /lookup/shells/{aasIdentifier}` returned `200` with an empty list if no asset links were stored for the AAS, although the Open API file lists `404` for `GetAllAssetLinksById`. (Part 2: https://industrialdigitaltwin.io/aas-specifications/IDTA-01002/v3.1.1/specification/interfaces.html#_operation_getallassetlinksbyid, Open API: https://app.swaggerhub.com/apis/Plattform_i40/DiscoveryServiceSpecification/V3.1.1_SSP-001#/Asset%20Administration%20Shell%20Basic%20Discovery%20API/GetAllAssetLinksById) - `POST /lookup/shells/{aasIdentifier}` returned an object mapping the AAS identifier to its asset links, although the Open API file defines the `201` body of `PostAllAssetLinksById` as a plain array of `SpecificAssetId`. (Part 2: https://industrialdigitaltwin.io/aas-specifications/IDTA-01002/v3.1.1/specification/interfaces.html#_operation_postallassetlinksbyid, Open API: https://app.swaggerhub.com/apis/Plattform_i40/DiscoveryServiceSpecification/V3.1.1_SSP-001#/Asset%20Administration%20Shell%20Basic%20Discovery%20API/PostAllAssetLinksById). The Part 2 specification defines the output `payload` with cardinality `1`, while the Open API file defines an array. This change follows the latter, because `POST` accepts and replaces a list of asset links, so the response lists all of them. The tests were adapted accordingly. Fixes #690 --- server/app/interfaces/discovery.py | 5 +-- .../test_docker_integration_discovery.py | 7 +++-- server/test/interfaces/test_discovery.py | 31 +++++++------------ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/server/app/interfaces/discovery.py b/server/app/interfaces/discovery.py index dddc6ec4..b22d2f9d 100644 --- a/server/app/interfaces/discovery.py +++ b/server/app/interfaces/discovery.py @@ -200,6 +200,8 @@ def get_all_specific_asset_ids_by_aas_id( ) -> Response: aas_identifier = str(url_args["aas_id"]) asset_ids = self.persistent_store.get_all_specific_asset_ids_by_aas_id(aas_identifier) + if not asset_ids: + raise werkzeug.exceptions.NotFound(f"AAS {aas_identifier!r} has no asset links!") return response_t(asset_ids) def post_all_asset_links_by_id( @@ -209,8 +211,7 @@ def post_all_asset_links_by_id( specific_asset_ids = HTTPApiDecoder.request_body_list(request, model.SpecificAssetId, False) self.persistent_store.delete_specific_asset_ids_by_aas_id(aas_identifier) self.persistent_store.add_specific_asset_ids_to_aas(aas_identifier, specific_asset_ids) - updated = {aas_identifier: self.persistent_store.get_all_specific_asset_ids_by_aas_id(aas_identifier)} - return response_t(updated, status=201) + return response_t(self.persistent_store.get_all_specific_asset_ids_by_aas_id(aas_identifier), status=201) def delete_all_asset_links_by_id( self, request: Request, url_args: dict, response_t: Type[APIResponse], **_kwargs diff --git a/server/test/docker_integration/test_docker_integration_discovery.py b/server/test/docker_integration/test_docker_integration_discovery.py index 72959eed..6830d8a2 100644 --- a/server/test/docker_integration/test_docker_integration_discovery.py +++ b/server/test/docker_integration/test_docker_integration_discovery.py @@ -73,6 +73,7 @@ def test_asset_link_roundtrip(self): ) with urllib.request.urlopen(post_request) as response: self.assertEqual(201, response.status) + self.assertEqual([self.ASSET_LINK], json.loads(response.read())) with urllib.request.urlopen(aas_asset_links_path) as response: self.assertEqual(200, response.status) @@ -83,6 +84,6 @@ def test_asset_link_roundtrip(self): with urllib.request.urlopen(delete_request) as response: self.assertEqual(204, response.status) - with urllib.request.urlopen(aas_asset_links_path) as response: - self.assertEqual(200, response.status) - self.assertEqual([], json.loads(response.read())) + with self.assertRaises(urllib.error.HTTPError) as context: + urllib.request.urlopen(aas_asset_links_path) + self.assertEqual(404, context.exception.code) diff --git a/server/test/interfaces/test_discovery.py b/server/test/interfaces/test_discovery.py index 056d0b48..702b7b24 100644 --- a/server/test/interfaces/test_discovery.py +++ b/server/test/interfaces/test_discovery.py @@ -9,14 +9,13 @@ Endpoint tests for :class:`~app.interfaces.discovery.DiscoveryAPI`. The routes follow the *Discovery Service Specification* (SSP-001, "full" profile) from ``aas-specs-api``. -Where this server deviates from the spec's documented status codes, the test asserts the *implemented* -behavior and the deviation is called out in a comment. Requests and responses go through the shared :class:`~..format_utils.JsonFormatClient` (as in -``test/interfaces/repository/test_shells.py``). Only ``GET /lookup/shells/{aasIdentifier}`` returns +``test/interfaces/repository/test_shells.py``). ``GET /lookup/shells/{aasIdentifier}`` returns AAS model objects (``SpecificAssetId``), so it is additionally run against the XML -:class:`~..format_utils.FormatClient`; every other route returns plain strings / JSON objects that the -SDK XML serializer cannot render, so those are JSON only. +:class:`~..format_utils.FormatClient`. ``POST /lookup/shells/{aasIdentifier}`` also returns +``SpecificAssetId`` objects but is tested with JSON only; every other route returns plain strings / JSON +objects that the SDK XML serializer cannot render, so those are JSON only. """ import json @@ -216,12 +215,8 @@ def test_get_returns_stored_asset_ids(self, format_client: FormatClient) -> None @with_json_client @with_xml_client - def test_get_unknown_aas_returns_empty(self, format_client: FormatClient) -> None: - # Spec allows 404 here; this server returns 200 with an empty collection instead. - response = format_client.get(f"/lookup/shells/{base64url_encode(self.UNKNOWN_ID)}") - - self.assertEqual(200, response.status_code, msg=response.get_data(as_text=True)) - self.assertEqual([], format_client.parse_collection(response)) + def test_get_unknown_aas_returns_404(self, format_client: FormatClient) -> None: + self.assert_error(format_client.get(f"/lookup/shells/{base64url_encode(self.UNKNOWN_ID)}"), 404) # ------------------------------------------------------------------ POST @@ -232,8 +227,9 @@ def test_post_creates_asset_links(self) -> None: ) self.assertEqual(201, response.status_code, msg=response.get_data(as_text=True)) - body = self.format_client.parse_object(response) - self.assertEqual([("serial", "123")], [(a["name"], a["value"]) for a in body[self.AAS_ID]]) + self.assertEqual( + [("serial", "123")], [(a["name"], a["value"]) for a in self.format_client.parse_collection(response)] + ) self.assertEqual( [self.AAS_ID], self.format_client.parse_collection( @@ -249,7 +245,7 @@ def test_post_accepts_single_object_body(self) -> None: self.assertEqual(201, response.status_code, msg=response.get_data(as_text=True)) self.assertEqual( [("serial", "123")], - [(a["name"], a["value"]) for a in self.format_client.parse_object(response)[self.AAS_ID]], + [(a["name"], a["value"]) for a in self.format_client.parse_collection(response)], ) def test_post_replaces_existing_asset_links(self) -> None: @@ -262,7 +258,7 @@ def test_post_replaces_existing_asset_links(self) -> None: ) self.assertEqual(201, response.status_code, msg=response.get_data(as_text=True)) - stored = {(a["name"], a["value"]) for a in self.format_client.parse_object(response)[self.AAS_ID]} + stored = {(a["name"], a["value"]) for a in self.format_client.parse_collection(response)} self.assertEqual({("batch", "xyz")}, stored) self.assertEqual( [], @@ -286,10 +282,7 @@ def test_delete_removes_asset_links(self) -> None: response = self.format_client.delete(f"/lookup/shells/{base64url_encode(self.AAS_ID)}") self.assertEqual(204, response.status_code, msg=response.get_data(as_text=True)) - self.assertEqual( - [], - self.format_client.parse_object(self.format_client.get(f"/lookup/shells/{base64url_encode(self.AAS_ID)}")), - ) + self.assert_error(self.format_client.get(f"/lookup/shells/{base64url_encode(self.AAS_ID)}"), 404) self.assertEqual( [], self.format_client.parse_collection(