Skip to content

[BUG] [Java] schema with oneOfs will generated model-code where first entry will match all others #16710

@skwakman

Description

@skwakman

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

Given an OpenAPI schema containing a component with several oneOf's, in which each oneOf entry has a unique 'type' value. Here's a partial example:

{
  "components": {
    "schemas": {
      "ResponseContent": {
        "discriminator": {
          "propertyName": "type"
        },
        "oneOf": [
          {
            "$ref": "#/components/schemas/Title"
          },
          {
            "$ref": "#/components/schemas/Subtitle"
          },
          {
            "$ref": "#/components/schemas/Paragraph"
          }
        ]
      },
      "Title": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "title"
            ]
          }
        }
      },
      "Subtitle": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "subtitle"
            ]
          }
        }
      },
      "Paragraph": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "paragraph"
            ]
          }
        }
      }
    }
  }
}

Using the 7.0.1 java generator, the above schema will generate this Title model:

public class Title {
  public enum TypeEnum {
    TITLE("title"),
    
    SUBTITLE("subtitle"),
    
    PARAGRAPH("paragraph");

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

This indicates that the 7.0.1 java generator generates model code in which the first entry in the oneOf will match all the other entries. In the above case, the generated Title class matches with the typeEnum of title, subtitle and paragraph. This will cause the model to never be able to deserialize properly, as there are multiple options to deserialize json with type: 'subtitle' (because both Title and Subtitle classes match will it).

This did work properly in openapi-generator 6.6.0.

openapi-generator version

Issue observed in 7.0.1. It worked properly in 6.6.0.

OpenAPI declaration file content or url

See https://github.com/skwakman/openapi-generator-java-issue/blob/main/src/main/resources/example-schema.json .

Generation Details

See https://github.com/skwakman/openapi-generator-java-issue/blob/main/build.gradle.kts

Steps to reproduce

I've made an example project illustrating the issue.

  1. Check out https://github.com/skwakman/openapi-generator-java-issue
  2. run ./gradlew openApiGenerate
  3. See the generated code for Title in build/generate-resources/main/src/main/java/com/example/api/model/Title.java. Its 'TypeEnum' matches with all the type values of the other components. The code for the other components (for instance in build/generate-resources/main/src/main/java/com/example/api/model/Subtitle.java) show that they only match for their specific type value.
  4. Run the project's Main class in src/main/java to see what problem this causes: Jackson is not able to deserialize a Subtitle, because both Title and Subtitle classes match.

Change the version of openapi-generator in build.gradle.kts to 6.6.0, run ./gradlew clean and run steps 2-4 again. The Title class will contain the correct TypeEnum values.

Suggest a fix

There's probably a bug somewhere in the java generator, introduced since 7.0.1.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions