From 18decae286e34a38c1e82d1a82fa0a03b0b3f873 Mon Sep 17 00:00:00 2001 From: tstorek Date: Wed, 7 Dec 2022 11:06:10 +0100 Subject: [PATCH 1/3] fix: disable pydantic autocast for ContextAttributes and added better parsing For #173 --- filip/models/ngsi_v2/base.py | 23 +++++++++++++++-------- filip/models/ngsi_v2/context.py | 2 +- tests/models/test_ngsi_v2_context.py | 2 ++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/filip/models/ngsi_v2/base.py b/filip/models/ngsi_v2/base.py index b667aa91..fea88f63 100644 --- a/filip/models/ngsi_v2/base.py +++ b/filip/models/ngsi_v2/base.py @@ -333,9 +333,7 @@ class BaseValueAttribute(BaseModel): min_length=1, regex=FiwareRegex.string_protect.value, # Make it FIWARE-Safe ) - value: Optional[Union[Union[float, int, bool, str, List, Dict[str, Any]], - List[Union[float, int, bool, str, List, - Dict[str, Any]]]]] = Field( + value: Optional[Any] = Field( default=None, title="Attribute value", description="the actual data" @@ -343,12 +341,19 @@ class BaseValueAttribute(BaseModel): @validator('value') def validate_value_type(cls, value, values): - """validator for field 'value'""" + """ + 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) - if value: + if value is not None: if type_ == DataType.TEXT: if isinstance(value, list): return [str(item) for item in value] @@ -373,10 +378,12 @@ def validate_value_type(cls, value, values): raise TypeError(f"{type(value)} does not match " f"{DataType.ARRAY}") if type_ == DataType.STRUCTUREDVALUE: + if isinstance(value, BaseModel): + return json.loads(value.json()) value = json.dumps(value) return json.loads(value) - else: - value = json.dumps(value) - return json.loads(value) + + value = json.dumps(value) + return json.loads(value) return value diff --git a/filip/models/ngsi_v2/context.py b/filip/models/ngsi_v2/context.py index 35263a2e..b9e313c4 100644 --- a/filip/models/ngsi_v2/context.py +++ b/filip/models/ngsi_v2/context.py @@ -116,7 +116,7 @@ class ContextEntityKeyValues(BaseModel): regex=FiwareRegex.standard.value, # Make it FIWARE-Safe allow_mutation=False ) - type: str = Field( + type: Union[str, Enum] = Field( ..., title="Entity Type", description="Id of an entity in an NGSI context broker. " diff --git a/tests/models/test_ngsi_v2_context.py b/tests/models/test_ngsi_v2_context.py index 704d54c8..51ee7587 100644 --- a/tests/models/test_ngsi_v2_context.py +++ b/tests/models/test_ngsi_v2_context.py @@ -54,8 +54,10 @@ def test_cb_attribute(self) -> None: """ attr = ContextAttribute(**{'value': 20, 'type': 'Text'}) self.assertIsInstance(attr.value, str) + self.assertEqual(attr.value, '20') attr = ContextAttribute(**{'value': 20, 'type': 'Number'}) self.assertIsInstance(attr.value, float) + self.assertEqual(str(attr.value), str(20.0)) attr = ContextAttribute(**{'value': [20, 20], 'type': 'Float'}) self.assertIsInstance(attr.value, list) attr = ContextAttribute(**{'value': [20.0, 20.0], 'type': 'Integer'}) From dbbe1f624c196561b8a7cfd6796afded11c81c50 Mon Sep 17 00:00:00 2001 From: tstorek Date: Wed, 7 Dec 2022 11:08:55 +0100 Subject: [PATCH 2/3] chore: raised version number For #173 --- docs/source/conf.py | 2 +- filip/__init__.py | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index a1ae744d..fddf829c 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -24,7 +24,7 @@ author = 'E.ON ERC - EBC' # The full version, including alpha/beta/rc tags -release = '0.2.3' +release = '0.2.4' # The short X.Y version. version = '.'.join(release.split('.')[0:2]) diff --git a/filip/__init__.py b/filip/__init__.py index 426b64eb..1ec7ef77 100644 --- a/filip/__init__.py +++ b/filip/__init__.py @@ -4,4 +4,4 @@ from filip.config import settings from filip.clients.ngsi_v2 import HttpClient -__version__ = '0.2.3' +__version__ = '0.2.4' diff --git a/setup.py b/setup.py index 64311f72..263b6aa7 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ SETUP_REQUIRES = INSTALL_REQUIRES.copy() -VERSION = '0.2.3' +VERSION = '0.2.4' setuptools.setup( name='filip', From 6149a9664163c2973d0bd7accb4e01260a12fabd Mon Sep 17 00:00:00 2001 From: tstorek Date: Wed, 7 Dec 2022 11:18:22 +0100 Subject: [PATCH 3/3] chore: updated CHANGELOG.md For #173 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0428aee2..02a7f9c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### v.0.2.4 +- fixed ContextAttribute: wrong type conversion for value ([#173](https://github.com/RWTH-EBC/FiLiP/issues/173)) + #### v0.2.3 - added `override_metadata` argument according to new metadata update semantics in orion (https://fiware-orion.readthedocs.io/en/master/user/metadata.html#updating-metadata) ([#157](https://github.com/RWTH-EBC/FiLiP/issues/157)) - fixed test for patch_entity ([#157](https://github.com/RWTH-EBC/FiLiP/issues/157))