Skip to content

Commit

Permalink
Merge pull request #174 from RWTH-EBC/173-ContextAttribute-wrong-type…
Browse files Browse the repository at this point in the history
…-conversion-for-value

fix: 173 context attribute wrong type conversion for value
  • Loading branch information
tstorek authored Dec 7, 2022
2 parents 526f55d + 6149a96 commit f992474
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion filip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from filip.config import settings
from filip.clients.ngsi_v2 import HttpClient

__version__ = '0.2.3'
__version__ = '0.2.4'
23 changes: 15 additions & 8 deletions filip/models/ngsi_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,27 @@ 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"
)

@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]
Expand All @@ -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
2 changes: 1 addition & 1 deletion filip/models/ngsi_v2/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

SETUP_REQUIRES = INSTALL_REQUIRES.copy()

VERSION = '0.2.3'
VERSION = '0.2.4'

setuptools.setup(
name='filip',
Expand Down
2 changes: 2 additions & 0 deletions tests/models/test_ngsi_v2_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down

0 comments on commit f992474

Please sign in to comment.