Skip to content

[java] fix CustomTypeAdapterFactory NPE-style crash on null additionalProperties value#24395

Open
chetan26 wants to merge 2 commits into
OpenAPITools:masterfrom
chetan26:chet-fix/issue-24393
Open

[java] fix CustomTypeAdapterFactory NPE-style crash on null additionalProperties value#24395
chetan26 wants to merge 2 commits into
OpenAPITools:masterfrom
chetan26:chet-fix/issue-24393

Conversation

@chetan26

@chetan26 chetan26 commented Jul 23, 2026

Copy link
Copy Markdown

Title:

[java] fix CustomTypeAdapterFactory NPE-style crash on null additionalProperties value
Body:

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work.
  • Run the following to build the project and update samples:
    ./mvnw clean package
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh

Commit all changed files.

  • File the PR against the correct branch: master
  • If your PR is targeting a particular programming language, @mention the technical committee members for that language.

Description

Follow-up to #14404 / #17812.

That fix made pojo.mustache's generated CustomTypeAdapterFactory.write() handle JSON arrays in additionalProperties, but the else branch still calls getAsJsonObject() unconditionally:

JsonElement jsonElement = gson.toJsonTree(entry.getValue());
if (jsonElement.isJsonArray()) {
obj.add(entry.getKey(), jsonElement.getAsJsonArray());
} else {
obj.add(entry.getKey(), jsonElement.getAsJsonObject());
}

If an additionalProperties: true model has an undeclared-property entry whose value is null (e.g. an unknown field the server returned as JSON null), gson.toJsonTree(null) returns JsonNull.INSTANCE, and .getAsJsonObject() on that throws:

java.lang.IllegalStateException: Not a JSON Object: null at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90)

This adds an isJsonNull() check ahead of the array/object branches, mirroring the same pattern #17812 used for arrays:

JsonElement jsonElement = gson.toJsonTree(entry.getValue());
if (jsonElement.isJsonNull()) {
  obj.add(entry.getKey(), JsonNull.INSTANCE);
} else if (jsonElement.isJsonArray()) {
  obj.add(entry.getKey(), jsonElement.getAsJsonArray());
} else {
  obj.add(entry.getKey(), jsonElement.getAsJsonObject());
}

Fixes #24393

How to validate

AdditionalPropertiesClass model = new AdditionalPropertiesClass();
model.putAdditionalProperty("unknownField", null);
model.toJson(); // previously threw IllegalStateException: Not a JSON Object: null

Regenerated all java-okhttp-gson* sample configs (bin/configs/java-okhttp*.yaml) — diff is uniform and confined to the additionalProperties block (new JsonNull import + the isJsonNull() branch) in every model that has additionalProperties: true.
Compiled the regenerated samples/client/petstore/java/okhttp-gson project (mvn compile) — builds clean.
Ran the existing JavaClientCodegenTest suite: 263/263 passed, no regressions.
Verified end-to-end against the regenerated AdditionalPropertiesClass: toJson() no longer throws for a null-valued additional property.


Summary by cubic

Fix crash in generated CustomTypeAdapterFactory.write() for Java okhttp-gson models when additionalProperties contains null. Nulls are now serialized as JsonNull to avoid IllegalStateException.

  • Bug Fixes
    • Add isJsonNull() branch and write JsonNull.INSTANCE before array/object handling in additionalProperties in pojo.mustache.
    • Regenerate java-okhttp-gson* samples (adds JsonNull import and null branch); remove unused imports; samples compile and existing tests pass.

Written for commit a5d0787. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 248 files

Note: This PR contains a large number of files. cubic only reviews up to 200 files per PR, so some files may not have been reviewed. cubic prioritizes the most important files to review.

Re-trigger cubic

@chetan26
chetan26 force-pushed the chet-fix/issue-24393 branch from e706e24 to a5d0787 Compare July 23, 2026 17:30
else {
JsonElement jsonElement = gson.toJsonTree(entry.getValue());
if (jsonElement.isJsonArray()) {
if (jsonElement.isJsonNull()) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@cubic-dev-ai review

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.

@chetan26 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 146 files

Re-trigger cubic

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][JAVA][gson] CustomTypeAdapterFactory throws "Not a JSON Object:null" for null additionalProperties values

1 participant