Skip to content

Commit a839f94

Browse files
authored
Merge pull request #516 from HugoPBrito/fix/csv-flags-enum-deserialization
fix(serialization-json): handle CSV string in get_collection_of_enum_values for flags enums
2 parents 6a723f7 + 41977e6 commit a839f94

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/serialization/json/kiota_serialization_json/json_parse_node.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ def get_collection_of_enum_values(self, enum_class: K) -> Optional[list[K]]:
191191
self._json_node
192192
)
193193
)
194+
# Handle flags enums serialized as a comma-separated string by the API
195+
# (e.g. "deviceCodeFlow" or "deviceCodeFlow,authenticationTransfer").
196+
# See https://github.com/microsoft/kiota/issues/3237 for context on
197+
# how flags enums are represented in OpenAPI via x-ms-enum-flags.
198+
if isinstance(self._json_node, str) and self._json_node:
199+
return [
200+
result for value in self._json_node.split(",")
201+
if (result := self._create_new_node(value.strip()).get_enum_value(enum_class)
202+
) is not None
203+
]
194204
return []
195205

196206
def get_enum_value(self, enum_class: K) -> Optional[K]:

packages/serialization/json/tests/unit/test_json_parse_node.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,45 @@ def test_get_collection_of_enum_values():
124124
assert result == [OfficeLocation.Dunhill, OfficeLocation.Oval]
125125

126126

127+
def test_get_collection_of_enum_values_from_csv_string():
128+
"""Flags enums may be serialized as a comma-separated string."""
129+
parse_node = JsonParseNode("dunhill,oval")
130+
result = parse_node.get_collection_of_enum_values(OfficeLocation)
131+
assert isinstance(result, list)
132+
assert result == [OfficeLocation.Dunhill, OfficeLocation.Oval]
133+
134+
135+
def test_get_collection_of_enum_values_from_csv_string_with_spaces():
136+
"""Flags enums may have spaces around the comma separator."""
137+
parse_node = JsonParseNode("dunhill, oval")
138+
result = parse_node.get_collection_of_enum_values(OfficeLocation)
139+
assert isinstance(result, list)
140+
assert result == [OfficeLocation.Dunhill, OfficeLocation.Oval]
141+
142+
143+
def test_get_collection_of_enum_values_from_single_string():
144+
"""A flags enum with a single value is still serialized as a plain string."""
145+
parse_node = JsonParseNode("dunhill")
146+
result = parse_node.get_collection_of_enum_values(OfficeLocation)
147+
assert isinstance(result, list)
148+
assert result == [OfficeLocation.Dunhill]
149+
150+
151+
def test_get_collection_of_enum_values_from_empty_string():
152+
"""An empty string should return an empty list."""
153+
parse_node = JsonParseNode("")
154+
result = parse_node.get_collection_of_enum_values(OfficeLocation)
155+
assert result == []
156+
157+
158+
def test_get_collection_of_enum_values_from_csv_string_with_unknown_member():
159+
"""Unknown members in a CSV string should be silently skipped."""
160+
parse_node = JsonParseNode("dunhill,unknownValue,oval")
161+
result = parse_node.get_collection_of_enum_values(OfficeLocation)
162+
assert isinstance(result, list)
163+
assert result == [OfficeLocation.Dunhill, OfficeLocation.Oval]
164+
165+
127166
def test_get_enum_value():
128167
parse_node = JsonParseNode("dunhill")
129168
result = parse_node.get_enum_value(OfficeLocation)

0 commit comments

Comments
 (0)