OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Generated class with no fields instead of a primitive String

Open
#21,469 3 comments 0 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?
  • 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?
    Actual output:
  public class Result  {
   @ApiModelProperty(required = true, value = "This is the result string ")
   /**
     * This is the result string 
    **/
    private String resultId;

    @ApiModelProperty(value = "")
    @Valid
    private ResultBban bban;

    // ... accessors etc.
  }

  public class ResultBban  {
    // no fields, only the default methods
    public boolean equals(Object o)...
    public int hashCode()...
    public String toString()...
    private static String toIndentedString(Object o)...
  }

Expected output:

  public class Result  {
   @ApiModelProperty(required = true, value = "This is the result string ")
   /**
     * This is the result string 
    **/
    private String resultId;

  @ApiModelProperty(example = "BARC12345612345678", value = "Basic Bank Account Number (BBAN) Identifier. ")
   /**
     * Basic Bank Account Number (BBAN) Identifier. 
    **/
    private String bban;


    // ... accessors, pattern annotation, etc.
  }
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using anyOf property with description and ref (which is of type string), the plugin up to 7.10.0 properly generated a model class with a String field. Since 7.11.0 (including current 7.14.0 and master) it instead generates a field whose type is a separate generated model class that itself has no fields.

openapi-generator version

7.11.0-7.14.0
(it's a regression - 7.10.0 was ok)

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  version: 1.0.0
  title: Test
paths:
  /api/test:
    get:
      operationId: getTest
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Result'
components:
  schemas:
    Result:
      description: |
        The test result
      type: object
      required:
        - resultId
      properties:
        resultId:
          description: |
              This is the result string
          type: string
        bban:
          anyOf:
            - description: |
                This is the BBAN string
            - $ref: '#/components/schemas/BBAN'
    BBAN:
      description: |
        Basic Bank Account Number (BBAN) Identifier.
      type: string
      pattern: "[a-zA-Z0-9]{1,30}"
      example: "BARC12345612345678"
Generation Details
       <plugin>
         <groupId>org.openapitools</groupId>
         <artifactId>openapi-generator-maven-plugin</artifactId>
         <version>7.10.0</version>
         <configuration>
             <generateModelTests>false</generateModelTests>
             <generateApiTests>false</generateApiTests>
             <generateModelDocumentation>false</generateModelDocumentation>
             <generateApiDocumentation>false</generateApiDocumentation>
             <skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
             <addTestCompileSourceRoot>false</addTestCompileSourceRoot>
             <addCompileSourceRoot>true</addCompileSourceRoot>
             <configHelp>false</configHelp>
             <configOptions>
                 <java8>true</java8>
                 <interfaceOnly>true</interfaceOnly>
                 <dateLibrary>java8</dateLibrary>
                 <openApiNullable>false</openApiNullable>
                 <containerDefaultToNull>true</containerDefaultToNull>
                 <serializationLibrary>jackson</serializationLibrary>
                 <sourceFolder>src/main/java</sourceFolder>
                 <legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
                 <inputSpec>${project.basedir}/src/main/resources/test.yaml</inputSpec>
             </configOptions>
         </configuration>
         <executions>
           <execution>
             <id>generate-test</id>
               <goals>
                   <goal>generate</goal>
               </goals>
               <configuration>
                 <generatorName>jaxrs-cxf</generatorName>
                 <inputSpec>${project.basedir}/src/main/resources/test.yaml</inputSpec>
                 <output>${project.build.directory}/generated</output>
               </configuration>
           </execution>
         </executions>
      </plugin>
Steps to reproduce

run a maven build with the above plugin configuration in the pom and the above yaml in the resources dir.

Related issues/PRs

The symptoms seem the same as #20630, but that one metnions allOf while here we have anyOf, and in addition that issue is marked as closed before 7.14.0 while I'm still experiencing it on current 7.14.0/master.

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 by running the Maven configuration with the supplied resources/test.yaml and jaxrs-cxf generator, comparing generation on 7.10.0 with 7.11.0 or later. Trace how the anyOf property combines its description with the BBAN string reference; done means bban is generated as a String with its pattern and example, without an empty BBAN model class.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.