Repository navigation
fix(json): preserve falsy root values in serialized content #743
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 4 commits into
microsoft:main
from
Shubham-Padkonde:fix/json-falsy-root-values
Sep 21, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a738612
fix(json): preserve falsy root values in serialized content
Shubham-Padkonde 92bd907
Distinguish explicit root null from an unset JSON writer
Shubham-Padkonde eaca9fa
Keep root-value tracking internal to the JSON writer
Shubham-Padkonde 2ff42c0
Merge branch 'main' into fix/json-falsy-root-values
baywet 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
50 changes: 50 additions & 0 deletions
50
packages/serialization/json/tests/unit/test_root_values.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,50 @@ | ||
| import json | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
| from kiota_abstractions.request_information import RequestInformation | ||
|
|
||
| from kiota_serialization_json.json_serialization_writer import JsonSerializationWriter | ||
| from kiota_serialization_json.json_serialization_writer_factory import ( | ||
| JsonSerializationWriterFactory, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [None, False, 0, 0.0, "", [], {}, True, 1, "text", [1]]) | ||
| def test_root_value_round_trip_and_reset(value): | ||
| writer = JsonSerializationWriter() | ||
| writer.write_any_value(None, value) | ||
| result = json.loads(writer.get_serialized_content()) | ||
| assert type(result) is type(value) | ||
| assert result == value | ||
| # A named property after serialization verifies that root state was reset. | ||
| writer.write_str_value("name", "next") | ||
| assert json.loads(writer.get_serialized_content()) == {"name": "next"} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [None, False, 0, 0.0, "", [], {}, True, 1, "text", [1]]) | ||
| def test_rejects_mixed_root_and_property_values(value): | ||
| writer = JsonSerializationWriter() | ||
| writer.write_any_value(None, value) | ||
| writer.write_str_value("name", "property") | ||
| with pytest.raises(ValueError, match="Invalid Json output"): | ||
| writer.get_serialized_content() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [False, 0, 0.0, "", []]) | ||
| def test_request_content_preserves_falsy_scalar_values(value): | ||
| adapter = Mock() | ||
| adapter.get_serialization_writer_factory.return_value = JsonSerializationWriterFactory() | ||
| request = RequestInformation() | ||
| request.set_content_from_scalar(adapter, "application/json", value) | ||
| result = json.loads(request.content) | ||
| assert type(result) is type(value) | ||
| assert result == value | ||
|
|
||
|
|
||
| def test_composed_object_can_serialize_root_null(): | ||
| model = Mock() | ||
| model.serialize.side_effect = lambda output: output.write_null_value(None) | ||
| writer = JsonSerializationWriter() | ||
| writer.write_object_value("wrapped", model) | ||
| assert json.loads(writer.get_serialized_content()) == {"wrapped": None} | ||
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.