OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] oneOf, anyOf support for List inside DTO

Open
#8,022 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Spring openapi-generator-maven-plugin doesn't support oneOf.
изображение

openapi-generator version

4.3.1

pom.xml

 <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.3.1</version>
                <executions>
                    <execution>
                        <id>client</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <inputSpec>api.yml</inputSpec>
                            <output>codegen/client</output>
                            <generatorName>java</generatorName>
                            <configOptions>
                                <library>resttemplate</library>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                                <serializableModel>false</serializableModel>
                                <bigDecimalAsString>false</bigDecimalAsString>
                                <dateLibrary>java8</dateLibrary>
                                <java8>true</java8>
                                <interfaceOnly>true</interfaceOnly>
                            </configOptions>
                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>
                        </configuration>
                    </execution>
                    <execution>
                        <id>server</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <inputSpec>api.yml</inputSpec>
                            <output>codegen/server</output>
                            <generatorName>spring</generatorName>
                            <configOptions>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                                <serializableModel>false</serializableModel>
                                <bigDecimalAsString>false</bigDecimalAsString>
                                <dateLibrary>java8</dateLibrary>
                                <java8>true</java8>
                                <interfaceOnly>true</interfaceOnly>
                                <useBeanValidation>true</useBeanValidation>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
OpenAPI declaration file content or url

I tried to generate:

api.yml

openapi: "3.0.0"
info:
  version: "1.0"
  title: "Main"

paths:
  /class:
    get:
      responses:
        200:
          description: Ok
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/MainClass"

components:
  schemas:
    MainClass:
      type: object
      properties:
        fieldName:
          type: array
          items:
            oneOf:
              - $ref: "#/components/schemas/ObjectA"
              - $ref: "#/components/schemas/ObjectB"

    ObjectA:
      type: object
      properties:
        name:
          type: string
    ObjectB:
      type: object
      properties:
        name:
          type: string
Generation Details

When I use

...
<generatorName>java</generatorName>
   <configOptions>
        <library>resttemplate</library>
...

I get MainClass

...
public class MainClass {
  public static final String JSON_PROPERTY_FIELD_NAME = "fieldName";
  private List<MainClassFieldNameOneOf> fieldName = null;
...

which contains list of interfaces MainClassFieldNameOneOf. And ObjectA and ObjectB implement this interface.
Things are good.

BUT need SPRING and when use

...
<generatorName>spring</generatorName>
...

the interface OneOfObjectAObjectB is not generated

Steps to reproduce
  • create a pom.xml with openapi-generator-maven-plugin 4.3.1
  • create api.yml file above
  • mvn clean generate-sources
  • see broken code under target directory codegen\server\src\main\java\org\openapitools\model -> MainClass
Related issues/PRs
Suggest a fix

This bug is related to the Server side Java code (SpringBoot)
https://github.com/OpenAPITools/openapi-generator/issues/2906

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied pom.xml and api.yml, then run mvn clean generate-sources to compare the Java and Spring outputs under target/codegen. Inspect the generated MainClass and model sources; done means the Spring generation includes the required oneOf interface and produces valid server code for the List field.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.