Skip to content

JSON serializer replaces falsy root values with an empty object #742

Description

Describe the bug

On current main (aaad20d), writing a root JSON value of false, zero, an empty string or an empty list produces {} instead of that value. This also affects RequestInformation.set_content_from_scalar, so a generated client can send a different JSON type and value than the caller supplied.

Reproduction

from kiota_serialization_json.json_serialization_writer import JsonSerializationWriter

for value in [False, 0, 0.0, "", []]:
    writer = JsonSerializationWriter()
    writer.write_any_value(None, value)
    print(repr(value), writer.get_serialized_content())

All five outputs are b'{}'. Expected outputs are b'false', b'0', b'0.0', b'""' and b'[]', respectively.

get_serialized_content() checks the truthiness of self.value instead of whether a root value has been set. The same check also lets a falsy root value bypass the existing rejection of mixed root values and named properties.

Tested with Python 3.12 on Windows using the local JSON and abstractions packages. A two-condition fix and regression tests are prepared. This is separate from #741: that concerns empty property names, whereas this occurs with key=None and affects root values. Investigation and tests are AI-assisted.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Needs Triage 🔍

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions