Skip to content

Commit f2ef2c1

Browse files
authored
server: raise BadRequest 400 on malformed assetIds instead of propagating KeyError as 500 (#511)
1 parent 23557dd commit f2ef2c1

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

server/app/interfaces/repository.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,11 @@ def _get_shells(self, request: Request) -> Tuple[Iterator[model.AssetAdministrat
445445
for asset_id in asset_ids:
446446
asset_id_json = base64url_decode(asset_id)
447447
asset_dict = json.loads(asset_id_json)
448-
name = asset_dict["name"]
449-
value = asset_dict["value"]
448+
try:
449+
name = asset_dict["name"]
450+
value = asset_dict["value"]
451+
except KeyError as e:
452+
raise BadRequest(f"Invalid assetId format: missing field {e}") from e
450453

451454
if name == "specificAssetId":
452455
decoded_specific_id = HTTPApiDecoder.json_list(value, model.SpecificAssetId, False, True)[0]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (c) 2026 the Eclipse BaSyx Authors
2+
#
3+
# This program and the accompanying materials are made available under the terms of the MIT License, available in
4+
# the LICENSE file of this project.
5+
#
6+
# SPDX-License-Identifier: MIT
7+
8+
import base64
9+
import json
10+
import unittest
11+
12+
from basyx.aas import model
13+
from basyx.aas.adapter.aasx import DictSupplementaryFileContainer
14+
from basyx.aas.examples.data.example_aas import create_full_example
15+
from werkzeug.test import Client
16+
17+
from app.interfaces.repository import WSGIApp
18+
19+
20+
def _encode_asset_id(name: str, value: str) -> str:
21+
payload = json.dumps({"name": name, "value": value})
22+
return base64.urlsafe_b64encode(payload.encode()).decode()
23+
24+
25+
class ShellsAssetIdsTest(unittest.TestCase):
26+
def setUp(self) -> None:
27+
app = WSGIApp(create_full_example(), DictSupplementaryFileContainer())
28+
self.client = Client(app)
29+
30+
def test_malformed_asset_id_missing_field_returns_400(self) -> None:
31+
bad_payload = base64.urlsafe_b64encode(b'{"name": "globalAssetId"}').decode()
32+
response = self.client.get(f"/api/v3.1/shells?assetIds={bad_payload}")
33+
self.assertEqual(400, response.status_code)

0 commit comments

Comments
 (0)