[BUG][JAVA] Not compilable code generated when oneOf refs to an array type
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)?
- [ ] 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?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When oneOf refs to an array type, then the generated code is not compilable. java.util.List isn't imported and other compile time errors emerge.
openapi-generator version
6.0.1 maven plugin. I don't know if it's a regression as I've never had same situation with previous versions.
OpenAPI declaration file content or url
oneOf:
- type: string
- $ref: '#/components/schemas/my_array'
my_array:
type: array
items: {}
Generation Details
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/my-api.yaml</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<library>native</library>
<dateLibrary>java8</dateLibrary>
<serializationLibrary>jackson</serializationLibrary>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Steps to reproduce
mvn compile
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/8765 https://github.com/OpenAPITools/openapi-generator/issues/6015 https://github.com/OpenAPITools/openapi-generator/issues/5997
Suggest a fix
The generated code semantically is fine in human POV. Likely a bit change in compiler will make it syntactically fine too.
The issue still exists in openapi-generator 6.2.x (java client generator with library: okhttp-gson, native, jersey)
components:
schemas:
ReturnValue:
oneOf:
- type: string
- $ref: '#/components/schemas/ReturnValueArray'
ReturnValueArray:
type: array
items:
type: string
Issue-1: missing import java.util.List
Issue-2: use List<{DATA_TYPE}> as a class name, such as
deserialized = tree.traverse(jp.getCodec()).readValueAs(List<String>.class);
I am thinking the simple solution is to generate the model class of ReturnValueArray.
Anyone is working on this issue?
@wing328 do you have any concern or suggestion? Many thanks
Still broken in 7.5.0
Switching to jersey3 for the library property fixed it for me (I was using native before). But should still be fixed I'd say, especially in the default library.