OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.49k stars 6.5k forks source link

[BUG][JAVA] Not compilable code generated when oneOf refs to an array type #12911

Open yasserzamani opened 2 years ago

yasserzamani commented 2 years ago

Bug Report Checklist

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.

bgong-mdsol commented 1 year ago

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

reinkrul commented 4 months ago

Still broken in 7.5.0

reinkrul commented 4 months ago

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.