From 8309a99a024596ea1159813d54d4b90b1a5c9853 Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Fri, 15 Nov 2024 13:59:48 +0100 Subject: [PATCH 1/2] fix: validation error of ngsipayloadattr --- filip/models/ngsi_v2/subscriptions.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/filip/models/ngsi_v2/subscriptions.py b/filip/models/ngsi_v2/subscriptions.py index a7bc1bd9..1e94b777 100644 --- a/filip/models/ngsi_v2/subscriptions.py +++ b/filip/models/ngsi_v2/subscriptions.py @@ -19,7 +19,7 @@ from filip.models.ngsi_v2.base import ( EntityPattern, Expression, - BaseValueAttribute + DataType ) from filip.custom_types import AnyMqttUrl import warnings @@ -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 @@ -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): From 4e7d32efa380a12a1bf32f848c19329249000011 Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Tue, 19 Nov 2024 16:40:56 +0100 Subject: [PATCH 2/2] chore: add test for attribute renaming in custom ngsi notification --- tests/clients/test_ngsi_v2_cb.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/clients/test_ngsi_v2_cb.py b/tests/clients/test_ngsi_v2_cb.py index df8f3598..f44d25a2 100644 --- a/tests/clients/test_ngsi_v2_cb.py +++ b/tests/clients/test_ngsi_v2_cb.py @@ -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), @@ -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)