OpenAPITools / OpenAPITools/openapi-generator

[BUG][Spring] oneOf keyword generates wrong subtype annotations.

Open
#15,274 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

When generating interfaces using the oneOf discriminator keyword, the generator produces both the correct subtype annotations, and also incorrect annotations that incorrectly match the names of the openapi spec schemas, not the generated objects. I’m think this is because of a bug, but it could be because we’ve incorrectly written the spec. I have run this through the spec validator successfully, however.

What is produced:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "petType", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = Cat.class, name = "Cat"),
  @JsonSubTypes.Type(value = Dog.class, name = "Dog"),
  @JsonSubTypes.Type(value = Lizard.class, name = "Lizard"),
  @JsonSubTypes.Type(value = Cat.class, name = "cat"),
  @JsonSubTypes.Type(value = Dog.class, name = "dog"),
  @JsonSubTypes.Type(value = Lizard.class, name = "lizard")
})

What is expected:

@JsonSubTypes({
  @JsonSubTypes.Type(value = Cat.class, name = "cat"),
  @JsonSubTypes.Type(value = Dog.class, name = "dog"),
  @JsonSubTypes.Type(value = Lizard.class, name = "lizard")
})

When useOneOfInterfaces is false, the annotations are correct, but Pet is not generated as an interface.

Removing discriminator mapping removes the correct subtypes, leaving only the incorrect subtypes.

openapi-generator version

Latest/6.5

OpenAPI declaration file content or url

See here for the example yml:

example_petstore.yml

Command line used for generation

docker run --rm -v "${PWD}:/input" -v "${PWD}:/output" openapitools/openapi-generator-cli:latest generate `
    -g spring `
    --library spring-boot `
    -i /input/example_petstore.yml `
    -o /output `
    --model-package com.petstore.spec `
    --additional-properties=delegatePattern=true,interfaceOnly=false,defaultInterfaces=false,java8=true,dateLibrary=java8,hideGenerationTimestamp=true,useOneOfInterfaces=true

Steps to reproduce

Download or clone the above yml file into a clean directory. Run the docker command above while in that directory.

Related issues/PRs

Some related issues and PRs I have noted that are on the Java generator:

[BUG][JAVA] AllOf with mapping generates wrong JsonSubTypes annotations · Issue #14917 · OpenAPITools/openapi-generator

allow to specify the useOneOfInterfaces option for java by robbertvanwaveren · Pull Request #15042 · OpenAPITools/openapi-generator

Do not add schema / class name mapping where custom mapping exists #14984

Suggest a fix/enhancement

I am able to spend some time fixing this at work, but would need some guidance from someone more familiar with the project on a good place to start

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 linked example_petstore.yml and reproduce the output using the Spring generator command with useOneOfInterfaces=true. Compare the generated @JsonSubTypes annotations with the expected output, then verify that the generated Pet type remains an interface and that the duplicate schema-name annotations are removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
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.