[BUG] [java] Additional Properties unable to handle JSON arrays
Created by: ashatch
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
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
pojo.moustache
doesn't handle additionalProperties
in the case where a property value is a JSON array. It fails with e.g:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $[0]
The generated model object should support deserialization.
OpenAPI declaration file content or url
Example fragment from a specification:
components:
schemas:
SomeModel:
type: object
additionalProperties: true
Steps to reproduce
Using the above YAML specification, generate a java client, then test with an API that produces a property with an array value:
{
"foo": ["value1"]
}
This yields a runtime failure:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $[0]
Suggest a fix
I'm happy to raise a PR for a fix, which adds support for JSON arrays in pojo.moustache
. The problem is the assumption that all properties that are not primitive are always objects:
} else { // non-primitive type
instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class));