Skip to content

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

Description

@cnelson00

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions