Skip to content

Commit

Permalink
Merge pull request #351 from RWTH-EBC/350-NgsiPayloadAttr-model-in-su…
Browse files Browse the repository at this point in the history
…bcriptions-should-not-validate-value-type

fix: validation error of ngsipayloadattr
  • Loading branch information
djs0109 authored Nov 20, 2024
2 parents ee798b4 + 4e7d32e commit 71404cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 16 additions & 2 deletions filip/models/ngsi_v2/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from filip.models.ngsi_v2.base import (
EntityPattern,
Expression,
BaseValueAttribute
DataType
)
from filip.custom_types import AnyMqttUrl
import warnings
Expand All @@ -31,7 +31,7 @@
message='Field name "json" shadows an attribute in parent "Mqtt"')


class NgsiPayloadAttr(BaseValueAttribute):
class NgsiPayloadAttr(BaseModel):
"""
Model for NGSI V2 type payload in httpCustom/mqttCustom notifications.
The difference between this model and the usual BaseValueAttribute model is that
Expand All @@ -41,6 +41,20 @@ class NgsiPayloadAttr(BaseValueAttribute):
model.
"""
model_config = ConfigDict(extra="forbid")
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,
)
value: Optional[Any] = Field(
default=None, title="Attribute value", description="the actual data"
)


class NgsiPayload(BaseModel):
Expand Down
8 changes: 6 additions & 2 deletions tests/clients/test_ngsi_v2_cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,13 @@ def test_notification(self):
"value": 123,
"type": "Number"
},
"co2_new": {
"value": "${co2}",
"type": "Number"
}

}
},
"attrs": ["temperature", "humidity", "co2"],
"onlyChangedAttrs": False
},
"expires": datetime.now() + timedelta(days=1),
Expand Down Expand Up @@ -1326,10 +1329,11 @@ def on_disconnect(client, userdata, flags, reasonCode, properties=None):
sub_6 = client.get_subscription(sub_id_6)
sub_message = Message.model_validate_json(custom_sub_message)
self.assertEqual(sub_6.notification.timesSent, 1)
self.assertEqual(len(sub_message.data[0].get_attributes()), 3)
self.assertEqual(len(sub_message.data[0].get_attributes()), 4)
self.assertEqual(sub_message.data[0].id, "prefix:Test:001")
self.assertEqual(sub_message.data[0].type, "newType")
self.assertEqual(sub_message.data[0].get_attribute("co2").value, 78)
self.assertEqual(sub_message.data[0].get_attribute("co2_new").value, 78)
self.assertEqual(sub_message.data[0].get_attribute("temperature").value, 123)
client.delete_subscription(sub_id_6)

Expand Down

0 comments on commit 71404cc

Please sign in to comment.