Skip to content

fix(serialization-text): defensive byte array and timespan parsing in TextParseNode - #855

Merged
Vincent Biret (baywet) merged 8 commits into
microsoft:mainfrom
hardikkaurani:fix/text-parse-node-defensive-primitives
Nov 9, 2026
Merged

Vincent Biret (baywet) merged 8 commits into
microsoft:mainfrom
hardikkaurani:fix/text-parse-node-defensive-primitives

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

This PR enhances defensive parsing behavior in TextParseNode (Microsoft.Kiota.Serialization.Text) to align with other Kiota parse node implementations (such as JsonParseNode and FormParseNode):

  1. Defensive Byte Array Parsing (GetByteArrayValue):

    • Previously, calling GetByteArrayValue() on an unparseable or malformed Base-64 string threw an unhandled FormatException.
    • In comparison, other primitive getters in TextParseNode and peer parse nodes defensively catch format exceptions and return null.
    • Wrapped Convert.FromBase64String with a try-catch for FormatException to safely return null on invalid Base-64 payloads.
  2. Standard TimeSpan Format Support & Defensive Parsing (GetTimeSpanValue):

    • Extended GetTimeSpanValue() to support standard constant format ("c", e.g., "01:02:03" / "1.02:03:04") via TimeSpan.TryParseExact(..., "c", ...) in addition to ISO 8601 duration format (XmlConvert.ToTimeSpan).
    • Added try-catch handling for FormatException and OverflowException thrown by XmlConvert.ToTimeSpan on malformed or out-of-range strings, returning null consistently.
    • Handled null or whitespace text strings defensively.

Changes

  • src/serialization/text/TextParseNode.cs:
    • Added try-catch on Convert.FromBase64String in GetByteArrayValue().
    • Added null/empty check, constant "c" format parsing, and exception guards (FormatException, OverflowException) in GetTimeSpanValue().
    • Added XML documentation detailing defensive null returns on unparseable inputs.
  • tests/serialization/text/TextParseNodeTests.cs:
    • Added ReturnsNullOnInvalidByteArray test covering invalid Base-64 strings and empty strings.
    • Added ParsesConstantTimeSpan test covering standard TimeSpan string formats ("01:02:03", "1.02:03:04", "-00:05:00").
    • Added ReturnsNullOnMalformedOrOutOfRangeTimeSpan test covering malformed durations and out-of-range durations.

Verification

  • dotnet test passed 100% across all target frameworks (net10.0, net8.0, net472).
  • dotnet format --verify-no-changes passed with 0 violations.

…ode.GetByteArrayValue

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
… in TextParseNode

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…eNode.GetTimeSpanValue

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

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…erflowException in GetTimeSpanValue

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…-of-range TimeSpan durations

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…eArray and TimeSpan in TextParseNode

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner November 7, 2026 09:20
Copilot AI balanced review requested due to automatic review settings November 7, 2026 09:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

While working with TextParseNode, I noticed a couple of edge cases in primitive parsing that differed from the defensive conventions used across other Kiota parse nodes (like JsonParseNode and FormParseNode):

  1. GetByteArrayValue: Unparseable or malformed Base-64 strings caused an unhandled FormatException rather than returning
    ull like other primitive accessors.
  2. GetTimeSpanValue: Calling XmlConvert.ToTimeSpan threw unhandled FormatException or OverflowException on malformed durations or values exceeding standard TimeSpan bounds. Additionally, standard constant TimeSpan strings (c, e.g. 01:02:03 / 1.02:03:04) weren't recognized.

This PR adds:

  • ry-catch guards for FormatException in GetByteArrayValue and FormatException / OverflowException in GetTimeSpanValue, consistently returning
    ull on unparseable inputs.
  • Constant TimeSpan format parsing via TimeSpan.TryParseExact(..., c, ...).
  • Full unit test coverage for these paths (ReturnsNullOnInvalidByteArray, ParsesConstantTimeSpan, and ReturnsNullOnMalformedOrOutOfRangeTimeSpan).

All tests pass locally across all target frameworks (
et10.0,
et8.0,
et472), and formatting checks pass with zero violations. Whenever you get a moment, I'd appreciate your review!

@baywet
Vincent Biret (baywet) requested a balanced review from Copilot November 9, 2026 18:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

2 open issues

Review effort: Lite

Comment thread src/serialization/text/TextParseNode.cs Resolved
Comment thread src/serialization/text/TextParseNode.cs Resolved
… null check in GetTimeSpanValue

- Remove special-case check for 'null' string so unparseable strings follow normal parsing flow.

- Catch FormatException and OverflowException directly rather than filtering Exception with when clause.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 19:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Hardik K (hardikkaurani) commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet), pushed an update addressing the review comments:

  • Parsing consistency: Removed the ad-hoc null string check from TextParseNode.GetTimeSpanValue(). Unparseable strings now follow the standard TimeSpan parsing path and safely return null through format exception handling.
  • Explicit exception handling: Replaced the general exception filter with separate catch (FormatException) and catch (OverflowException) blocks.

All tests across target frameworks and formatting checks continue to pass cleanly. Ready for another look!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the defensive text-parsing changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Nov 9, 2026
@baywet
Vincent Biret (baywet) added this pull request to the merge queue Nov 9, 2026
Merged via the queue into microsoft:main with commit 3833f1b Nov 9, 2026
8 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Nov 9, 2026
This was referenced Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done ✔️

Development

Successfully merging this pull request may close these issues.

3 participants