[BUG][JAVA][okhttp-gson] add regression tests for null additionalProperties values
Fixes #24393
Context
#24393 reports that the okhttp-gson CustomTypeAdapterFactory in pojo.mustache throws
java.lang.IllegalStateException: Not a JSON Object: null when a model with additionalProperties: true
serializes an undeclared property whose value is null (e.g. an unknown field deserialized from a
server response that was JSON null): gson.toJsonTree(null) returns JsonNull.INSTANCE and the
else branch called getAsJsonObject() on it.
The template guard proposed in the issue is already on master: it was merged in #24573 (based on
#24395), which added the jsonElement.isJsonNull() check writing JsonNull.INSTANCE plus the
com.google.gson.JsonNull import, and regenerated the samples. That PR did not reference #24393
and did not add tests, which left the issue open without regression coverage.
This PR closes that gap with tests only, so the issue can be closed on merge. No template or
generated-sample changes are needed.
Changes (tests only)
modules/openapi-generator/src/test/resources/3_0/java/okhttp-gson-null-additional-properties.yaml:
minimal spec with anadditionalProperties: truemodel.modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaOkHttpGsonNullAdditionalPropertiesTest.java:
generates that model with thejava/okhttp-gsongenerator, compiles the generated
CustomTypeAdapterFactoryagainst the real Gson runtime (same fixture approach as
JavaOkHttpGsonOneOfPrimitiveArrayTest), and asserts:- serializing a null undeclared-property value writes JSON null instead of throwing
Not a JSON Object: null, both at JsonElement level and in full text output; - deserializing JSON null undeclared properties stores a null-valued entry in
additionalProperties; - serialize -> deserialize -> serialize round trip preserves the null entry;
- the existing String/array/object branches keep their behavior (follow-up to #14404/#17812).
- serializing a null undeclared-property value writes JSON null instead of throwing
samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java:
newtestAdditionalPropertiesNullValuereproducing the issue's exact repro with the generated
petstoreAdditionalPropertiesClass(putAdditionalProperty("unknownField", null)+toJson()).
Regenerated samples
None required. This PR contains no template changes; the okhttp-gson samples already carry the
JsonNull guard since #24573, and re-running ./bin/generate-samples.sh bin/configs/java-okhttp-gson.yaml
on this branch produces no diff in samples/client/petstore/java/okhttp-gson (verified).
Verification
./mvnw -pl modules/openapi-generator -am test -Dtest=JavaOkHttpGsonNullAdditionalPropertiesTestpasses.mvn test -Dtest=JSONTestinsamples/client/petstore/java/okhttp-gsonpasses (34 tests, including the new one).
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
Tests-only PR: the project modules and CLI jar were built with./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit./mvnw package -DskipTests,
and the targeted regen./bin/generate-samples.sh bin/configs/java-okhttp-gson.yamlproduces
no diff on this branch (see "Regenerated samples" above). All changed files are committed. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
(@mentions intentionally omitted)
Summary by cubic
Adds regression tests for the already-merged fix to #24393, where the okhttp-gson CustomTypeAdapterFactory threw IllegalStateException: Not a JSON Object: null when serializing a null undeclared-property value. The fix shipped in #24573 without test coverage; this tests-only PR closes that gap so the issue can be closed. No template changes or sample regeneration needed.
- Generator-level test generates an
additionalProperties: truemodel plus the client's JSON factory, compiles both against the real Gson runtime, and verifies a serialize → deserialize → serialize round trip with a null value through the generatedJSON.getGson(), plus unchanged behavior for string/array/object values. Addsgson-fireandokio(pinned to 3.2.0 so its Kotlin metadata stays readable by the module's test compiler) as test-scoped dependencies;jsr305stays transitive fromguavaso the main compile classpath is unaffected. - Sample test reproduces the issue's exact repro using the petstore
AdditionalPropertiesClass. - Updates the sample test fingerprint record for
JSONTest.javato match the modified file.