Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validation error of ngsipayloadattr #351

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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