openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [java] Additional Properties unable to handle JSON arrays

Open ashatch opened this issue 3 years ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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));

ashatch avatar Oct 11 '22 15:10 ashatch

PR open: https://github.com/OpenAPITools/openapi-generator/pull/13672

ashatch avatar Oct 12 '22 12:10 ashatch