Skip to content

fix: serialize Gson elements in additional data - #2188

Merged
Vincent Biret (baywet) merged 1 commit into
microsoft:mainfrom
Shubham-Padkonde:fix/json-element-additional-data
Sep 21, 2026
Merged

Vincent Biret (baywet) merged 1 commit into
microsoft:mainfrom
Shubham-Padkonde:fix/json-element-additional-data

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown
Contributor

Fixes #1388.

Gson JsonObject values in additional data currently reach the reflection fallback, which writes a property name without a value and then fails with IllegalStateException: Dangling name. JsonPrimitive and JsonNull values fail similarly; JsonArray takes the Iterable path, where its nonempty elements encounter the same problem. This prevents serializing custom security attributes as described in microsoftgraph/msgraph-sdk-java#2063.

Recognize JsonElement before the generic Iterable/reflection paths and write it through the writer's existing Gson instance and public JsonElement adapter. This covers objects, arrays, primitives, and JSON null without reflecting over Gson implementation classes or converting numbers through double.

Eight regression cases exercise additional-data serialization, including nested objects/arrays/nulls, escaped strings, a large integer, and empty containers. Seven fail on the original implementation; the empty-array control already passed. All pass with the fix.

Validation: JDK 21 and Gradle 9.7.1 (the repository wrapper version):

  • JSON package spotlessApply and build: 38 tests pass, including Spotless and SpotBugs checks.
  • Full build -Pjava8=true with a Java 8 toolchain supplied for compatibility projects.

No live Microsoft Graph calls or Android build were run.

AI assistance: Codex assisted with the reproduction, implementation, and local validation. No separate human review is claimed.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The change is narrowly scoped, addresses the reported failure mode directly, and includes targeted regression tests covering key JSON element variants.

Review effort: Lite
Findings: None

What changed in this PR

This PR fixes serialization of Gson JsonElement values stored in additionalData by handling them explicitly in JsonSerializationWriter before the generic Iterable/reflection fallback paths, preventing invalid JSON output and IllegalStateException: Dangling name.

Changes:

  • Add a dedicated JsonElement serialization path in JsonSerializationWriter.writeAnyValue.
  • Add parameterized regression coverage for JsonObject, JsonArray, primitives, null, large integers, escaping, and empty containers in additional-data serialization.
File Description
components/​serialization/​json/​src/​main/​java/​com/​microsoft/​kiota/​serialization/​JsonSerializationWriter.java Recognizes JsonElement in writeAnyValue and serializes it via Gson’s JsonElement adapter to avoid reflection/iterable fallbacks.
components/​serialization/​json/​src/​test/​java/​com/​microsoft/​kiota/​serialization/​JsonSerializationWriterTests.java Adds a parameterized regression test validating correct round-trip serialization of JsonElement values placed in additionalData.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Sep 21, 2026
@baywet
Vincent Biret (baywet) merged commit a61c76c into microsoft:main Sep 21, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Sep 21, 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.

Json serilializer leaves out object of JsonObject type in additionalData

3 participants