OpenAPITools / OpenAPITools/openapi-generator

[BUG] [SPRING] OpenAPI Generator generates enums that does not implement the interface for model with "OneOf" in the schema

Open
#17,347 3 comments 5 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
openapi-generator version

7.1.0

OpenAPI declaration file content or url

      "DefaultPublicationType": {
        "type": "string",
        "enum": [
          "FLIGHT_DATA",
          "ARRIVAL_SEQUENCE_DATA",
          "AIRPORT_CONFIGURATION_DATA"
        ]
      },
      "OtherPublicationType": {
        "type": "string",
        "pattern": "^OTHER:[A-Z_][A-Z0-9_]*$"
      },
      "Publication": {
        "description": "A SWIM Publication identifies a topic together with its content type, formal content type definition, and supported filters.",
        "oneOf": [
          {
            "$ref": "#/components/schemas/DefaultPublicationType"
          },
          {
            "$ref": "#/components/schemas/OtherPublicationType"
          }
        ]
      },
Generation Details
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public interface Publication {
}
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public enum DefaultPublicationType {
  
  FLIGHT_DATA("FLIGHT_DATA"),
  
  ARRIVAL_SEQUENCE_DATA("ARRIVAL_SEQUENCE_DATA"),
  
  AIRPORT_CONFIGURATION_DATA("AIRPORT_CONFIGURATION_DATA");

  private String value;

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

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

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

  @JsonCreator
  public static DefaultPublicationType fromValue(String value) {
    for (DefaultPublicationType b : DefaultPublicationType.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    throw new IllegalArgumentException("Unexpected value '" + value + "'");
  }
}
Steps to reproduce
 <configuration>
                            <!-- see: https://openapi-generator.tech/docs/generators/spring/-->
                            <skipValidateSpec>true</skipValidateSpec>
                            <inputSpec>${project.basedir}/src/main/resources/openapi.json</inputSpec>
                            <generatorName>spring</generatorName>
                            <configOptions>
                                <useSpringBoot3>true</useSpringBoot3>
                                <useOneOfInterfaces>true</useOneOfInterfaces>
                                <useOptional>true</useOptional>
                                <annotationLibrary>swagger2</annotationLibrary>
                                <delegatePattern>true</delegatePattern>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                                <hateoas>true</hateoas>
                                <useBeanValidation>true</useBeanValidation>
                                <performBeanValidation>true</performBeanValidation>
                            </configOptions>
                        </configuration>
Related issues/PRs

#16672 #14807 #11715 #15

Suggest a fix

The generated enumeration should implement the interface.

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 Spring generator's useOneOfInterfaces option and reproduce the issue using the provided OpenAPI schemas and XML configuration. Compare the generated Publication interface with DefaultPublicationType; done means the generated enum implements the interface, with the existing related issues used for context.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.