Skip to content

Commit 0b3db4e

Browse files
authored
Fix serialization of payloads that contain collection of multiple elements (#637)
Serialization of a payload that contains a collection of items fails. Only the last item of the collection appears in the serialized output. All other collections are not available. Fixes #635
1 parent 52e33d2 commit 0b3db4e

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

ocpp/charge_point.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,18 @@ class StatusInfoType:
130130
serialized = asdict(dataclass)
131131

132132
for field in dataclass.__dataclass_fields__.values():
133-
134133
value = getattr(dataclass, field.name)
135134
if _is_dataclass_instance(value):
136135
serialized[field.name] = serialize_as_dict(value)
137136
continue
138137

139138
if isinstance(value, list):
139+
serialized[field.name] = []
140140
for item in value:
141141
if _is_dataclass_instance(item):
142-
serialized[field.name] = [serialize_as_dict(item)]
142+
serialized[field.name].append(serialize_as_dict(item))
143+
else:
144+
serialized[field.name].append(item)
143145

144146
return serialized
145147

tests/test_charge_point.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ocpp.v16.datatypes import MeterValue, SampledValue
1717
from ocpp.v16.enums import Action, RegistrationStatus
1818
from ocpp.v201 import ChargePoint as cp_201
19+
from ocpp.v201 import call_result, datatypes, enums
1920
from ocpp.v201.call import GetVariables as v201GetVariables
2021
from ocpp.v201.call import SetNetworkProfile as v201SetNetworkProfile
2122
from ocpp.v201.datatypes import (
@@ -312,6 +313,67 @@ def test_serialize_as_dict():
312313
assert serialize_as_dict(payload) == expected
313314

314315

316+
def test_serialization_of_collection_of_multiple_elements():
317+
"""This test validates that bug #635 is fixed.
318+
That bug incorrectly serialized payloads that contain a collection
319+
of elements.
320+
321+
This test serializes a call_result.SetVariables that contains 2
322+
SetVariableResultTypes.
323+
324+
https://github.com/mobilityhouse/ocpp/issues/635
325+
"""
326+
payload = call_result.SetVariables(
327+
set_variable_result=[
328+
datatypes.SetVariableResultType(
329+
attribute_status=enums.SetVariableStatusType.accepted,
330+
component={
331+
"name": "TemperatureSensor",
332+
"instance": "First",
333+
"evse": {"id": 1, "connector_id": 1},
334+
},
335+
variable={"name": "DisplayUnit", "instance": "Main"},
336+
attribute_type="Actual",
337+
attribute_status_info=None,
338+
),
339+
datatypes.SetVariableResultType(
340+
attribute_status="Accepted",
341+
component={"name": "TxCtrlr"},
342+
variable={"name": "TxStopPoint"},
343+
attribute_type="Actual",
344+
attribute_status_info=None,
345+
),
346+
],
347+
custom_data=None,
348+
)
349+
350+
expected = {
351+
"custom_data": None,
352+
"set_variable_result": [
353+
{
354+
"attribute_status": "Accepted",
355+
"attribute_status_info": None,
356+
"attribute_type": "Actual",
357+
"component": {
358+
"evse": {"connector_id": 1, "id": 1},
359+
"instance": "First",
360+
"name": "TemperatureSensor",
361+
},
362+
"variable": {"instance": "Main", "name": "DisplayUnit"},
363+
},
364+
{
365+
"attribute_status": "Accepted",
366+
"attribute_status_info": None,
367+
"attribute_type": "Actual",
368+
"component": {"name": "TxCtrlr"},
369+
"variable": {"name": "TxStopPoint"},
370+
},
371+
],
372+
}
373+
# Execute / Assert
374+
assert serialize_as_dict(payload) == expected
375+
376+
315377
@pytest.mark.asyncio
316378
async def test_call_unique_id_added_to_handler_args_correctly(connection):
317379
"""

0 commit comments

Comments
 (0)