Using the Java generator, the generated Model for an Object that references another Object that has "additionalProperties" fails to compile.
This is because the default value of the property is not correctly set, see in generated Bar.java:
private Foo foo = new HashMap<String, Object>();
4.3.0
Bug has already been reported for versions 4.0.0 to 4.0.3 in https://github.com/OpenAPITools/openapi-generator/issues/3456
https://gist.github.com/thomas-bc/549582b87073b8e02971fb623e30cabc
Snippet:
definitions:
Foo:
type: object
properties:
asdf:
type: string
additionalProperties:
type: object
Bar:
type: object
properties:
foo:
$ref: '#/definitions/Foo'
$ java -jar ./openapi-generator-cli-4.3.0.jar generate -g java -i ./swagger.yaml -o ./generated_example
./gradlew assembleThis will throw a compiling error:
in /generated_example/src/main/java/org/openapitools/client/model/Bar.java:35: error: cannot find symbol
private Foo foo = new HashMap<String, Object>();
^
symbol: class HashMap
location: class Bar
1 error
private Foo foo = new Foo();
or no default value:
private Foo foo;
https://github.com/OpenAPITools/openapi-generator/issues/3456
馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
Workaround is to post process the file:
find build/open-api-generated/src/main/java/ -type f -name "*.java" -exec sed -i 's/TypeContainingAdditionalProps foo = new HashMap<>/TypeContainingAdditionalProps foo = new TypeContainingAdditionalProps/g' {} \;
I've opened #6052 to resolve this.
Most helpful comment
I've opened #6052 to resolve this.