Repository navigation
fix(json): preserve empty property names during serialization #741
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
Merged
Vincent Biret (baywet)
merged 3 commits into
microsoft:main
from
Shubham-Padkonde:fix/json-empty-property-names
Sep 21, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/serialization/json/tests/unit/test_empty_property_names.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import json | ||
| from datetime import date, datetime, time, timedelta | ||
| from uuid import UUID | ||
|
|
||
| import pytest | ||
|
|
||
| from kiota_serialization_json.json_serialization_writer import JsonSerializationWriter | ||
|
|
||
| from ..helpers import OfficeLocation, User | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("key", ["", "named", None]) | ||
| @pytest.mark.parametrize("method,value,expected", [ | ||
| ("write_str_value", "text", "text"), | ||
| ("write_bool_value", True, True), | ||
| ("write_int_value", 42, 42), | ||
| ("write_float_value", 1.5, 1.5), | ||
| ("write_uuid_value", UUID(int=1), str(UUID(int=1))), | ||
| ("write_datetime_value", datetime(2026, 1, 2, 3, 4), "2026-01-02T03:04:00"), | ||
| ("write_date_value", date(2026, 1, 2), "2026-01-02"), | ||
| ("write_time_value", time(3, 4), "03:04:00"), | ||
| ("write_timedelta_value", timedelta(seconds=1), "0:00:01"), | ||
| ("write_bytes_value", b"hello", "aGVsbG8="), | ||
| ("write_collection_of_primitive_values", [1, 2], [1, 2]), | ||
| ("write_enum_value", OfficeLocation.Oval, "oval"), | ||
| ("write_collection_of_enum_values", [OfficeLocation.Oval], ["oval"]), | ||
| ]) | ||
| def test_property_name_is_distinct_from_root(key, method, value, expected): | ||
| writer = JsonSerializationWriter() | ||
| getattr(writer, method)(key, value) | ||
| assert json.loads(writer.get_serialized_content()) == ( | ||
| expected if key is None else {key: expected} | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [None, "", False, 0, {}, {"": {}}, [{"": "value"}]]) | ||
| def test_nested_additional_data_preserves_empty_names(value): | ||
| writer = JsonSerializationWriter() | ||
| writer.write_additional_data_value({"properties": {"": value}}) | ||
| assert json.loads(writer.get_serialized_content()) == {"properties": {"": value}} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("collection", [False, True]) | ||
| def test_model_with_empty_property_name(collection): | ||
| user = User() | ||
| user.mobile_phone = "123" | ||
| writer = JsonSerializationWriter() | ||
| if collection: | ||
| writer.write_collection_of_object_values("", [user]) | ||
| else: | ||
| writer.write_object_value("", user) | ||
| expected = {"mobile_phone": "123"} | ||
| assert json.loads(writer.get_serialized_content()) == {"": [expected] if collection else expected} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.