OpenAPITools / OpenAPITools/openapi-generator

[BUG] Description

Open
#16,672 2 comments 3 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

the java generator on openapi-generator-maven-plugin 7.0.1 doesn't compile with an openapi which contain an oneOf instructions with descriptor of string type, but the implementations have enum for descriptor

The interfaces and implementation are generated, but the compilation is failed due to wrong implementation of getter method. Interface has discriminator of type String, while implementations have discriminator of enum type

The compilation error (name of the classes are changed due to requirements from the company):
getAtType() in .<implementation class name 1> cannot implement getAtType() in .

Interface class:

@JsonIgnoreProperties(
  value = "@type", // ignore manually set @type, it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the @type to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value =<first class>.class, name = "<first class name>"),
  @JsonSubTypes.Type(value =<second class>.class, name = "<second class name>"),
})

public interface<interface name> extends Serializable {
    public String getAtType();
}

Implementation classes:

@JsonPropertyOrder({

})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2023-09-26T12:17:27.817531+03:00[Europe/Athens]")
public class <first class> implements Serializable, <interface name> {
  private static final long serialVersionUID = 1L;
  //other code
  /**
   * When sub-classing, this defines the sub-class entity name
   */
  public enum AtTypeEnum {
    <first class name>("<first class name>");

    private String value;

    AtTypeEnum(String value) {
      this.value = value;
    }

    @JsonValue
    public String getValue() {
      return value;
    }

    @Override
    public String toString() {
      return String.valueOf(value);
    }

    @JsonCreator
    public static AtTypeEnum fromValue(String value) {
      for (AtTypeEnum b : AtTypeEnum.values()) {
        if (b.value.equals(value)) {
          return b;
        }
      }
      throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
  }

//other code
openapi-generator version

version: 7.0.1

OpenAPI declaration file content or url

Yaml file provided partly due to security reasons

        <name>:
          type: "array"
          items:
            oneOf:
              - $ref: "#/components/schemas/<second class>"
              - $ref: "#/components/schemas/<first class>"
            discriminator:
              propertyName: "@type"
              mapping:
                <second class name>: "#/components/schemas/<second class>"
                <first class name>: "#/components/schemas/<first class>"
          description: "..."
<second class>:
      type: "object"
      description: "..."
      required:
        - "@type"
        - "id"
      allOf:
        - $ref: "..."
      properties:
        '@type':
          type: "string"
          description: "When sub-classing, this defines the sub-class entity name"
          enum:
            - "<second class name>"
Generation Details

pom file:
`
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<org.openapitools.version>7.0.1</org.openapitools.version>

org.openapitools openapi-generator ${org.openapitools.version} org.slf4j slf4j-simple org.slf4j slf4j-ext org.slf4j slf4j-api org.openapitools openapi-generator-maven-plugin ${org.openapitools.version} id generate ${project.basedir}/src/main/resources/interfaces/out/yaml_name.yaml java package.model package.api package.invoker false false false false ${project.basedir}/.openapi-generator-ignore . java8 false true true true webclient true true true
    </plugins>`
Steps to reproduce

Run maven compile stage

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 Java generator configuration using useOneOfInterfaces and the supplied OpenAPI oneOf/discriminator example, then reproduce the failure with the Maven compile stage. Compare the generated interface getter with the implementation enum getter; done means the generated project compiles for this schema and the regression is covered by a focused generator test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.