Skip to content

Commit 989894d

Browse files
committed
chore: fix broken-tests
For #225
1 parent e4c8d0c commit 989894d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

filip/models/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ class DataType(str, Enum):
5858
"hh:mm:ss[Z|(+|-)hh:mm] (see XML schema for details).",
5959
)
6060
RELATIONSHIP = "Relationship", "Reference to another context entity"
61-
STRUCTUREDVALUE = "StructuredValue", ("Structured datatype must be "
61+
STRUCTUREDVALUE = "StructuredValue", ("Structured datatype must be an "
62+
"array or object"
6263
"serializable")
6364
ARRAY = "Array", "Array of the types above"
65+
OBJECT = "Object", "JSON-Object of the types above, i.e. a dictionary"
6466
COMMAND = "command", "A command for IoT Devices"
6567
COMMAND_RESULT = (
6668
"commandResult",
@@ -74,6 +76,7 @@ class DataType(str, Enum):
7476
"contains an autogenerated attribute "
7577
"of this type",
7678
)
79+
GEOJSON = "geo:json", "GeoJson object"
7780

7881

7982
class PaginationMethod(str, Enum):

filip/models/ngsi_v2/base.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,14 @@ def validate_value_type(cls, value, info: ValidationInfo):
420420
return int(value)
421421
if type_ == DataType.DATETIME:
422422
return value
423+
# allows list
423424
if type_ == DataType.ARRAY:
424425
if isinstance(value, list):
425426
return value
426427
raise TypeError(f"{type(value)} does not match "
427428
f"{DataType.ARRAY}")
428-
if type_ == DataType.STRUCTUREDVALUE:
429+
# allows dict and BaseModel as object
430+
if type_ == DataType.OBJECT:
429431
if isinstance(value, dict):
430432
value = json.dumps(value)
431433
return json.loads(value)
@@ -435,11 +437,25 @@ def validate_value_type(cls, value, info: ValidationInfo):
435437
raise TypeError(
436438
f"{type(value)} does not match " f"{DataType.STRUCTUREDVALUE}"
437439
)
440+
# allows list, dict and BaseModel as structured value
441+
if type_ == DataType.STRUCTUREDVALUE:
442+
if isinstance(value, (dict, list)):
443+
value = json.dumps(value)
444+
return json.loads(value)
445+
elif isinstance(value, BaseModel):
446+
value.model_dump_json()
447+
return value
448+
raise TypeError(
449+
f"{type(value)} does not match " f"{DataType.STRUCTUREDVALUE}"
450+
)
438451

452+
# if none of the above, check if it is a pydantic model
439453
if isinstance(value, BaseModel):
440454
value.model_dump_json()
441455
return value
442456

457+
# if none of the above, check if serializable. Hence, no further
458+
# type check is performed
443459
value = json.dumps(value)
444460
return json.loads(value)
445461

0 commit comments

Comments
 (0)