Skip to content

OneOf-Generation supports only two options #1275

@fkellner

Description

@fkellner

Tell us the extension you're using

Client

I tried this:

I tried to generate a client from an OpenAPI Schema which used oneOf with more than three schemas listed.

This happened:

Only the fields from the alphabetically last two schemas were included in the generated model class.

I expected this:

The fields from all listed schemas to be included in the generated model class.

Is there a workaround?

OneOf-Nesting maybe? But ultimately anything we need to manually change in a downloaded OpenAPI Yaml file makes the process brittle and error-prone (since it's >10000 lines of Yaml).

How can we try to reproduce the issue?

I have forked the project and written a short Test demonstrating the problem: https://github.com/fkellner/quarkus-openapi-generator/blob/bugreport/one-of-three/client/deployment/src/test/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapperTest.java

Here is the yaml:

---
openapi: 3.0.3
info:
  title: Block API
  version: "1.0"
paths:
  /random-animals:
    get:
      tags:
        - Random Animals Resource
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/RandomAnimalsResponse'
components:
  schemas:
    RandomAnimalsResponse:
      type: object
      properties:
        animals:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/Dog'
              - $ref: '#/components/schemas/Cat'
              - $ref: '#/components/schemas/Mouse'
      required:
        - animals
    Dog:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            dogId:
              type: string
      required:
        - dogId
    Animal:
      discriminator:
        propertyName: species
        mapping:
          Dog: '#/components/schemas/Dog'
          Cat: '#/components/schemas/Cat'
          Mouse: '#/components/schemas/Mouse'
      properties:
        species:
          type: string
      required:
        - species
    Cat:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            catId:
              type: string
      required:
        - catId
    Mouse:
      allOf:
        - $ref: '#/components/schemas/Mouse'
        - type: object
          properties:
            mouseId:
              type: string
      required:
        - mouseId

Here is the code of the new, failing Test:

    /**
     * Fields from all three schemas should be included in the generated class
     */
    @Test
    void verifyOneOfWithMoreThanTwoOptions() throws java.net.URISyntaxException, FileNotFoundException {
        OpenApiClientGeneratorWrapper generatorWrapper = createGeneratorWrapper("one-of-three.yaml");
        final List<File> generatedFiles = generatorWrapper.generate("org.oneofthree");

        assertNotNull(generatedFiles);
        assertFalse(generatedFiles.isEmpty());

        final Optional<File> arrayElementsClass = generatedFiles.stream()
                .filter(f -> f.getName().endsWith("RandomAnimalsResponseAnimalsInner.java")).findFirst();
        assertThat(arrayElementsClass).isPresent();

        final CompilationUnit compilationUnit = StaticJavaParser.parse(arrayElementsClass.orElseThrow());
        Set<String> methodNames = compilationUnit.findAll(MethodDeclaration.class).stream()
                        .map(md -> md.getName().asString())
                                .collect(Collectors.toSet());
        assertThat(methodNames).containsAll(Set.of("getCatId", "getDogId", "getMouseId"));
    }

Anything else?

I would love to be able to fix this myself and create a pull request, especially since this feature is essential for a project.
If I understand more about the structure, I might also be able to change the rather verbose naming scheme, which already caused problems in an earlier iteration.
However, I lack some information:

  • is there an overview of the structure of code generation/the steps it goes through/what options need to be supported? can somebody give me a short summary, so that I know where to look?
  • is this even a bug in this project, or is it a bug in an upstream library?

Output of uname -a or ver

windows32 NB1511 2.6.2 9200 i686-pc Intel unknown MinGW

Output of java -version

openjdk version "21.0.3" 2024-04-16 LTS OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode, sharing)

Quarkus OpenApi version or git rev

70a3fce

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256) Maven home: C:\ProgramData\chocolatey\lib\maven\apache-maven-3.9.8 Java version: 21.0.2, vendor: GraalVM Community, runtime: C:\Program Files\GraalVM\graalvm-community-openjdk-21.0.2+13.1 Default locale: en_GB, platform encoding: UTF-8 OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"

Additional information

No response

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.name: Bug Report 🐞

Metadata

Metadata

Assignees

Labels

area:clientThis item is related to the client extension

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions