Skip to content

fix(serialization-text): preserve empty string and bytes values and handle malformed inputs - #840

Open
Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/text-empty-content-and-scalar-parsing
Open

Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/text-empty-content-and-scalar-parsing

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Overview

Allow empty strings ("") and empty byte arrays (b"") to be serialized and deserialized in TextSerializationWriter and TextParseNode, prevent duplicate write overwrites when existing writer content is empty, and safely handle invalid/malformed scalar inputs without uncaught exceptions.

Changes

  1. Empty string serialization & duplicate protection: TextSerializationWriter.write_str_value and get_serialized_content previously checked the truthiness of self._writer (if self._writer:) instead of is not None. As a result, writing an empty string "" caused get_serialized_content() to raise No content was written to the serialization writer, while writing a second value after "" bypassed duplicate write rejection and silently overwrote the payload.
  2. Empty bytes serialization: TextSerializationWriter.write_bytes_value now checks value is not None and not key, properly base64-encoding and serializing b"" and raising NO_STRUCTURED_DATA_MESSAGE if a key is passed with empty bytes.
  3. Empty string deserialization: TextParseNode.get_str_value previously returned None for TextParseNode("") due to truthiness evaluation (if self._text:). It now checks self._text is not None, correctly returning "".
  4. Safe scalar parsing: TextParseNode now returns None instead of raising unhandled exceptions (ValueError, binascii.Error, KeyError) when encountering malformed primitives (int, float, UUID, datetime, date, time, timedelta), invalid base64 byte sequences, or unknown enum members, aligning with JsonParseNode and FormParseNode.

Related Issue

None (discovered during code audit of text serialization).

Testing Instructions

  • From packages/serialization/text:
    • pytest: 72 passed (58 existing + 14 new regression tests).
    • mypy kiota_serialization_text: Clean (strict mode).
    • isort --check-only kiota_serialization_text: Clean.
    • yapf -dr kiota_serialization_text: Clean.
    • pylint kiota_serialization_text --disable=W --rcfile=.pylintrc: Rated 10.00/10.
  • From packages/abstractions:
    • pytest: 194 passed.

…andle malformed inputs

Allow empty strings and byte arrays to be serialized and deserialized in TextSerializationWriter and TextParseNode. Prevent silent duplicate value overwrites when the existing writer value is empty, and safely return None for malformed scalar primitives, invalid base64 bytes, and unknown enum members.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…med scalars

Add unit tests verifying serialization and deserialization of empty strings and empty bytes, assertion of duplicate write prevention on empty string, and safe None returns on malformed scalar primitives and unknown enums.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant