OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA][gson] CustomTypeAdapterFactory throws "Not a JSON Object:null" for null additionalProperties values
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
Description
This is a follow-up to #14404/ #17812. The fix handled isJsonArray() in the generated CustomTypeAdapterFactory.write() method (pojo.mustache), 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 a model has additionalProperties: true and an entry in the undeclared-properties map has a null value (e.g. an unknown field deserialized from a server response that was JSON null), gson.toJsonTree(null) returns JsonNull.INSTANCE. Calling .getAsJsonObject() on a JsonNull throws:
java.lang.IllegalStateException: Not a JSON Object: null at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90)
openapi-generator version
master (7.25.0-SNAPSHOT), library=okhttp-gson, generator java
Steps to reproduce
This reproduces with any generated model that has additionalProperties: true, e.g. AdditionalPropertiesClass from the petstore spec:
AdditionalPropertiesClass model = new AdditionalPropertiesClass();
model.putAdditionalProperty("unknownField", null);
model.toJson(); // throws IllegalStateException: Not a JSON Object: null
Related issues/PRs
#14404
#17812
Suggest a fix
Add an isJsonNull() check before the array/object branches in pojo.mustache:
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());
}
plus the corresponding import com.google.gson.JsonNull;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in pojo.mustache, specifically the generated CustomTypeAdapterFactory.write() method and its handling of gson.toJsonTree(entry.getValue()). Use AdditionalPropertiesClass with an unknownField mapped to null and call model.toJson() to reproduce the failure. Done means null additional properties serialize without IllegalStateException while the existing array and object cases continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 84/100