Skip to content

Commit a287576

Browse files
committed
aas.adapter.json: deserialize ModelReferences with last key type
1 parent 34dcb07 commit a287576

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

sdk/basyx/aas/adapter/json/json_deserialization.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,15 @@ def _construct_model_reference(cls, dct: Dict[str, object], type_: Type[T], obje
340340
if reference_type is not model.ModelReference:
341341
raise ValueError(f"Expected a reference of type {model.ModelReference}, got {reference_type}!")
342342
keys = [cls._construct_key(key_data) for key_data in _get_ts(dct, "keys", list)]
343-
if keys and not issubclass(KEY_TYPES_CLASSES_INVERSE.get(keys[-1].type, type(None)), type_):
343+
last_key_type = KEY_TYPES_CLASSES_INVERSE.get(keys[-1].type, type(None))
344+
if keys and not issubclass(last_key_type, type_):
344345
logger.warning("type %s of last key of reference to %s does not match reference type %s",
345346
keys[-1].type.name, " / ".join(str(k) for k in keys), type_.__name__)
346-
return object_class(tuple(keys), type_, cls._construct_reference(_get_ts(dct, 'referredSemanticId', dict))
347+
# Infer type the model refence points to using `last_key_type` instead of `type_`.
348+
# `type_` is often a `model.Referable`, which is more abstract than e.g. a `model.ConceptDescription`,
349+
# leading to information loss while deserializing.
350+
return object_class(tuple(keys), last_key_type,
351+
cls._construct_reference(_get_ts(dct, 'referredSemanticId', dict))
347352
if 'referredSemanticId' in dct else None)
348353

349354
@classmethod

0 commit comments

Comments
 (0)