Skip to content

fix(serialization): normalize content-type parameters, whitespace, and casing in JSON factories and registries - #2259

Closed
Hardik K (hardikkaurani) wants to merge 9 commits into
microsoft:mainfrom
hardikkaurani:fix/json-and-registry-content-type-normalization
Closed

Hardik K (hardikkaurani) wants to merge 9 commits into
microsoft:mainfrom
hardikkaurani:fix/json-and-registry-content-type-normalization

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Summary

Completes RFC 2045 / RFC 9110 content-type normalization across JsonParseNodeFactory, JsonSerializationWriterFactory, ParseNodeFactoryRegistry, and SerializationWriterFactoryRegistry, bringing JSON factories to parity with the form (#2239), text (#2236), and multipart (#2229) factories and fixing crash/lookup bugs in both factory registries.

Root Cause

  1. JsonParseNodeFactory and JsonSerializationWriterFactory:
    • Both still validated contentType with strict equality (!contentType.equals(validContentType)), rejecting valid content types carrying MIME parameters (e.g., application/json; charset=utf-8), optional whitespace around ;, or case variations (e.g., APPLICATION/JSON).
    • JsonSerializationWriterFactory(Gson) also had a copy-paste message ("gson contentType cannot be null") in Objects.requireNonNull.
  2. ParseNodeFactoryRegistry:
    • contentType.split(";")[0] threw an uncaught ArrayIndexOutOfBoundsException for delimiter-only inputs such as ";" or ";;" because String.split(";") drops trailing empty strings.
    • Media types were neither trimmed nor matched case-insensitively, so valid headers like "APPLICATION/JSON; charset=utf-8", "application/json ; charset=utf-8", or "Application/Vnd.Example+JSON ; charset=utf-8" failed with ClientException.
  3. SerializationWriterFactoryRegistry:
    • getVendorSpecificContentType did not trim whitespace around ; or handle delimiter-only inputs cleanly.
    • Factory lookups were case-sensitive, failing on uppercase/mixed-case media types and vendor subtypes.
    • getCleanedVendorSpecificContentType(cleanedContentType) was invoked twice redundantly on the fallback path.

Changes

  • JsonParseNodeFactory & JsonSerializationWriterFactory:
    • Strip MIME parameters and optional whitespace (contentType.split(";", 2)[0].trim()) and match case-insensitively (equalsIgnoreCase(validContentType)).
    • Align JsonSerializationWriterFactory(Gson) null-check message with JsonParseNodeFactory(Gson) ("parameter gson cannot be null").
  • ParseNodeFactoryRegistry & SerializationWriterFactoryRegistry:
    • Split with limit 2 and .trim(), throwing ClientException cleanly on empty/delimiter-only media types instead of ArrayIndexOutOfBoundsException.
    • Resolve registered factories via exact match, Locale.ROOT lowercase fast path, and case-insensitive entry fallback, forwarding the matched registered key to the underlying factory (preserving compatibility with custom factories and BackingStoreSerializationWriterProxyFactory).
    • Remove the redundant second getCleanedVendorSpecificContentType call in SerializationWriterFactoryRegistry.
  • Tests:
    • Added JsonParseNodeFactoryTests and JsonSerializationWriterFactoryTests.
    • Extended FactoryRegistryExceptionTest with parameterized case-insensitivity, whitespace, vendor subtype, mixed-case registration, backing store bypass, and delimiter-only regression tests for both registries.

Verification

  • ./gradlew --no-daemon build: BUILD SUCCESSFUL across all components (abstractions, http:okHttp, authentication:azure, serialization:json, serialization:form, serialization:text, serialization:multipart, bundle).
  • Spotless (spotlessCheck), SpotBugs (spotbugsMain, spotbugsTest), Javadoc, JaCoCo, and all JUnit test suites passed.

…ivity in JsonParseNodeFactory

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

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

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

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…y tests for JsonParseNodeFactory

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…y tests for JsonSerializationWriterFactory

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…-case tests for ParseNodeFactoryRegistry

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…vity tests for SerializationWriterFactoryRegistry

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner November 18, 2026 11:44
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

Following up on the content-type normalization updates for multipart (#2229), text (#2236), and form (#2239), JsonParseNodeFactory and JsonSerializationWriterFactory were still using strict equality against application/json, and both ParseNodeFactoryRegistry and SerializationWriterFactoryRegistry had matching gaps:

  • JsonParseNodeFactory and JsonSerializationWriterFactory rejected valid application/json headers with MIME parameters (such as charset=utf-8), whitespace, or uppercase casing.
  • ParseNodeFactoryRegistry threw an uncaught ArrayIndexOutOfBoundsException on delimiter-only inputs like ";" (via contentType.split(";")[0]) and failed to match uppercase or whitespace-padded media types and vendor subtypes.
  • SerializationWriterFactoryRegistry likewise missed case-insensitive and whitespace-padded media types and ran getCleanedVendorSpecificContentType twice on fallback.

This PR brings the JSON factories to parity, fixes both registries while preserving compatibility with custom registered keys and BackingStoreSerializationWriterProxyFactory, and adds regression coverage for all four classes. The full Gradle build (Spotless, SpotBugs main/test, Javadoc, and all module tests) passes locally. Whenever you have a moment, please take a look. Thanks!

Copilot AI balanced review requested due to automatic review settings November 18, 2026 11:44

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 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.

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.

🟢 Approval recommended

No active review issues are reported for the content-type normalization changes.

No open issues 🎉

Review effort: Balanced

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.

🟢 Approval recommended

No active review issues were identified in the content-type normalization changes.

No open issues 🎉

Review effort: Balanced

Comment on lines +84 to +92
for (final Map.Entry<String, ParseNodeFactory> entry :
contentTypeAssociatedFactories.entrySet()) {
final String key = entry.getKey();
final ParseNodeFactory factory = entry.getValue();
if (key != null && factory != null && key.equalsIgnoreCase(contentType)) {
return entry;
}
}
return null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

no, this leads to o(n) lookup on every missing entry instead of o(1). We should normalize upon adding instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. In commit 14966428, we introduced CaseInsensitiveMap<V> extends HashMap<String, V> backing contentTypeAssociatedFactories in both ParseNodeFactoryRegistry and SerializationWriterFactoryRegistry.

Keys are now normalized upon adding (put / putAll strips parameters, trims whitespace, and converts to lowercase). Lookups in getParseNode() and getSerializationWriter() now perform direct O(1) hash map operations (get(vendorSpecificContentType) and get(cleanedContentType)), eliminating the linear scan over entrySet() on cache misses.

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Nov 18, 2026
Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:39

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 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.

…or O(1) lookups

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

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I have updated the PR in 14966428 to address your review feedback:

  • Normalization Upon Adding: Introduced CaseInsensitiveMap<V> extends HashMap<String, V> to back contentTypeAssociatedFactories in both ParseNodeFactoryRegistry and SerializationWriterFactoryRegistry. Keys are now normalized upon registration (stripping parameters, trimming whitespace, and converting to lowercase).
  • O(1) Direct Lookups: Removed the getParseNodeFactoryEntry() and getSerializationWriterFactoryEntry() fallback loops that iterated over entrySet(). Registry lookups in getParseNode() and getSerializationWriter() now perform direct O(1) hash map operations (get(vendorSpecificContentType) and get(cleanedContentType)), ensuring constant-time resolution even on cache misses.
  • Unit Testing: Added dedicated test coverage verifying CaseInsensitiveMap key normalization, insertion, and retrieval behaviors alongside existing registry tests.

The full Gradle build (check covering Spotless, SpotBugs main/test, and all module test suites) passes cleanly with zero errors. Whenever you have a chance, please take another look. Thank you!

@baywet

Copy link
Copy Markdown
Member

Hi Hardik K (@hardikkaurani), thank you for your contribution!

This pull request is being closed automatically because you currently have more than 10 open pull requests across the Kiota repositories. Reviewing takes time, and having this many pull requests open at once overwhelms the reviewers. Please avoid having more than 10 open pull requests at any given time.

The following pull requests remain open:

To move forward, you can either:

  • Wait for your existing pull requests to get merged, and make sure to respond timely to any feedback to help move things along.
  • Close older pull requests that are no longer relevant, and then re-open this one.

@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Nov 20, 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