OpenAPITools / OpenAPITools/openapi-generator

[REQ][java] Share common fields between Discriminator fields

Open
#20,725 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is your feature request related to a problem? Please describe.

We are using discriminator method in the interface for an openapi based spring code generator based on which we are generating the classes to be shared across.

Discriminator interface

@Schema(
        discriminatorMapping = {
                @DiscriminatorMapping(value = "type1", schema = Type1.class),
                @DiscriminatorMapping(value = "type2", schema = Type2.class),
                @DiscriminatorMapping(value = "type3", schema = Type3.class),
        },
        discriminatorProperty = "type",
        oneOf = {Type1.class,
                Type2.class,
                Type3.class
        }
)
public interface MainInterface {
        @Schema(
                requiredMode = Schema.RequiredMode.REQUIRED,
                example = "type1"
        )
        Type getType();

}

There properties that are shared across these Type child interfaces. So we want to add the shared getters in the MainInterface itself.

SharedProperties.java

public interface SharedProperties {
        String getName();
        String getDescription();
}

The generator currently generates main interface as

@JsonIgnoreProperties(
    value = {"type"},
    allowSetters = true
)
@JsonTypeInfo(
    use = Id.NAME,
    include = As.PROPERTY,
    property = "type",
    visible = true
)
@JsonSubTypes({@Type(
    value = Type1.class,
    name = "type1"
), @Type(
    value = Type2.class,
    name = "type2"
), @Type(
    value = Type3.class,
    name = "type3"
)})
public interface MainInterface {
    Type getType();
}

Describe the solution you'd like

We want to the Type interfaces to have SharedProperties methods. But we are not able to add it properly. Is there a way to implement this with keeping Type interfaces consistent to extend the Discriminator properties.

Describe alternatives you've considered

Tried add it as anyOf to MainInterface but discriminators are messing up and now Type classes doesn't implement MainInterface but it has all the properties from SharedProperties.

Additional context

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 docs/generators/spring.md and trace how the Spring generator handles discriminator interfaces, oneOf, and generated Type interfaces. Done means shared getter methods are available on each Type interface while the types still implement MainInterface and retain discriminator behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.