You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a child class of ContextAttribute and Overwrite the Vlaue type with a PydanticModel
Create a instance of this model
Try to serialize as json
Expected behavior
To have more convenience it would be nice to allow the definition of Pydantic Models.
Furthermore, currently the DataType validation removes all Pydantic functionality from submodels but Units.
Originally, this was intended to insure compatibility for the standard JSON library that do not define the specific type.
Hovever, the later issue could be simply omitted by standard serialikzing if type is unknown.
Solution (adds support for geojson)
classBaseValueAttribute(BaseModel):
""" Model to add the value property to an BaseAttribute Model. The Model is represented by a JSON object with the following syntax: The attribute value is specified by the value property, whose value may be any JSON datatype. """type: Union[DataType, str] =Field(
default=DataType.TEXT,
description="The attribute type represents the NGSI value type of the ""attribute value. Note that FIWARE NGSI has its own type ""system for attribute values, so NGSI value types are not ""the same as JSON types. Allowed characters ""are the ones in the plain ASCII set, except the following ""ones: control characters, whitespace, &, ?, / and #.",
max_length=256,
min_length=1,
regex=FiwareRegex.string_protect.value, # Make it FIWARE-Safe
)
value: Optional[Any] =Field(
default=None,
title="Attribute value",
description="the actual data"
)
@validator('value')defvalidate_value_type(cls, value, values):
""" Validator for field 'value' The validator will try autocast the value based on the given type. If `DataType.STRUCTUREDVALUE` is used for type it will also serialize pydantic models. With latter operation all additional features of the original pydantic model will be dumped. If the type is unknown it will check json-serializable. """type_=values['type']
validate_escape_character_free(value)
ifvalueisnotNone:
iftype_==DataType.TEXT:
ifisinstance(value, list):
return [str(item) foriteminvalue]
returnstr(value)
iftype_==DataType.BOOLEAN:
ifisinstance(value, list):
return [bool(item) foriteminvalue]
returnbool(value)
iftype_in (DataType.NUMBER, DataType.FLOAT):
ifisinstance(value, list):
return [float(item) foriteminvalue]
returnfloat(value)
iftype_==DataType.INTEGER:
ifisinstance(value, list):
return [int(item) foriteminvalue]
returnint(value)
iftype_==DataType.DATETIME:
returnvalueiftype_==DataType.ARRAY:
ifisinstance(value, list):
returnvalueraiseTypeError(f"{type(value)} does not match "f"{DataType.ARRAY}")
iftype_==DataType.STRUCTUREDVALUE:
ifisinstance(value, dict):
value=json.dumps(value)
returnjson.loads(value)
elifisinstance(value, BaseModel):
value.json()
returnvalueraiseTypeError(
f"{type(value)} does not match "f"{DataType.STRUCTUREDVALUE}"
)
ifisinstance(value, BaseModel):
value.json()
returnvaluevalue=json.dumps(value)
returnjson.loads(value)
@djs0109 I just found that this still must be fixed for metadata. Currently, metadata checks for serialization only. Hence, models fail if a pydanticModel is used, e.g., for Units
Describe the bug
Currently, the Datatype check does not allow good inheritance of ContextAttribute, e.g. to define special scenario specific custom models.
Also support for geo:json ist currently missing.
https://fiware-orion.readthedocs.io/en/master/orion-api.html#geospatial-properties-of-entities
To Reproduce
Steps to reproduce the behavior:
Expected behavior
To have more convenience it would be nice to allow the definition of Pydantic Models.
Furthermore, currently the DataType validation removes all Pydantic functionality from submodels but Units.
Originally, this was intended to insure compatibility for the standard JSON library that do not define the specific type.
Hovever, the later issue could be simply omitted by standard serialikzing if type is unknown.
Solution (adds support for geojson)
custom_attribute.py
main.py
The text was updated successfully, but these errors were encountered: