From 395fa07b7ce28f25805e571c7b9eba548d3ec91a Mon Sep 17 00:00:00 2001 From: Christian Sandberg Date: Thu, 17 Oct 2024 20:07:47 +0200 Subject: [PATCH] Fix nested sdfThing --- onedm/sdf/definitions.py | 15 +++----- onedm/sdf/document.py | 6 ++- tests/sdf/test_document.py | 77 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 tests/sdf/test_document.py diff --git a/onedm/sdf/definitions.py b/onedm/sdf/definitions.py index ffd8631..ff5f2c7 100644 --- a/onedm/sdf/definitions.py +++ b/onedm/sdf/definitions.py @@ -137,7 +137,11 @@ class Object(CommonQualities): class Thing(CommonQualities): - things: Things + things: dict[str, Thing] = Field( + default_factory=dict, + alias="sdfThing", + description="Definition of models for complex devices", + ) objects: Objects properties: Properties actions: Actions @@ -149,11 +153,4 @@ class Thing(CommonQualities): max_items: NonNegativeInt | None = None -Things = Annotated[ - dict[str, Thing], - Field( - default_factory=dict, - alias="sdfThing", - description="Definition of models for complex devices", - ), -] +Thing.model_rebuild() diff --git a/onedm/sdf/document.py b/onedm/sdf/document.py index ec155f0..ac00588 100644 --- a/onedm/sdf/document.py +++ b/onedm/sdf/document.py @@ -74,7 +74,11 @@ class Document(BaseModel): ), ), ] = None - things: definitions.Things + things: dict[str, definitions.Thing] = Field( + default_factory=dict, + alias="sdfThing", + description="Definition of models for complex devices", + ) objects: definitions.Objects properties: definitions.Properties actions: definitions.Actions diff --git a/tests/sdf/test_document.py b/tests/sdf/test_document.py new file mode 100644 index 0000000..096de5a --- /dev/null +++ b/tests/sdf/test_document.py @@ -0,0 +1,77 @@ +from onedm import sdf + + +def test_document_generation(): + doc = sdf.Document( + info=sdf.Information( + title="Test title", + ), + things={ + "MyThing": sdf.Thing( + things={ + "MySubThing": sdf.Thing( + objects={ + "MyObject": sdf.Object( + properties={ + "MyProperty": sdf.ArrayProperty( + items=sdf.AnyData( + ref="#/sdfData/MyEnum", + # Put a temporary definition here + ), + definitions={ + "MyEnum": sdf.IntegerData( + choices={ + "ONE": sdf.IntegerData( + const=1 + ) + } + ) + } + ) + } + ) + } + ) + } + ) + } + ) + + dump = doc.model_dump(mode="json", by_alias=True, exclude_defaults=True) + + assert dump == { + "info": { + "title": "Test title" + }, + "sdfThing": { + "MyThing": { + "sdfThing": { + "MySubThing": { + "sdfObject": { + "MyObject": { + "sdfProperty": { + "MyProperty": { + "type": "array", + "items": { + "sdfRef": "#/sdfData/MyEnum" + } + } + } + } + } + } + } + } + }, + "sdfData": { + "MyEnum": { + "type": "integer", + "sdfChoice": { + "ONE": { + "type": "integer", + "const": 1 + } + } + } + } + }