swagger-api / swagger-api/swagger-codegen

[JAVA] Inheritence without discriminator, is it possible ?

Open
#6,127 4 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I've tried doing inheritance without discriminator and I think something changed in the latest version as I find "example" online which work the way I wanted it, but it's not what I'm generating with the 2.2.3 SNAPSHOT.

From reading #2096 it feels like it is now impossible to generate inheritence without a discriminator.

    MyParent:
      type: object
      properties:
        code:
          type: string

    MyChild:
      type: object
      allOf:
        - $ref: '#/definitions/MyParent'
        - properties:
            label:
              type: string

public class MyParentApiBean   {
  @JsonProperty("code")
  private String code = null;
}

public class MyChildApiBean   {
  @JsonProperty("code")
  private String code = null;

  @JsonProperty("label")
  private String label = null;
}

If I add a discriminator I do get inheritance but also an extra field (the discriminator field)
In my use case I do not want discriminator as the 2 objects will be used on different API path so there is never the need to know which type it at de-serialization.
And I do not want to force people to add an extra attribut in their json.

The use case I've is I want to be able to define a method taking a MyParentApiBean and working with the code field be it MyParentApiBean or MyChildApiBean.
Right now I've to declare the attribut as an Object and do instanceof or duplicate code.

Swagger-codegen version

2.2.3-SNAPSHOT

Suggest a fix/enhancement

I understand the issue with multiple allOf inheritance, but that could be fixed with interface as suggested #5275
Or if there is a single allOf inheritance could be done still with inheritance.

Again thanks for your work.
Hope to have the time to give a hand soon.

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 by reviewing the inheritance behavior discussed in issues #2096 and #5275, then reproduce the supplied MyParent/MyChild schema with the 2.2.3-SNAPSHOT generator. Trace the Java model generation path and compare output with and without a discriminator. Done means the single-allOf case supports the requested parent-child relationship without requiring an extra discriminator property, with regression coverage for the generated models.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.