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

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

Open yasserzamani opened this issue 3 years ago • 4 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)?
  • [ ] 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.

yasserzamani avatar Jul 18 '22 14:07 yasserzamani

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

bgong-mdsol avatar Oct 11 '22 21:10 bgong-mdsol

Still broken in 7.5.0

reinkrul avatar Jun 05 '24 04:06 reinkrul

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.

reinkrul avatar Jun 08 '24 06:06 reinkrul