openapi-processor / openapi-processor/openapi-processor-spring

Inheritance support

Aperta
#159 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Kotlin
Stelle
56
Fork
11
Merge medio
2g 4h
PR unite (30g)
8

Descrizione

Quite impressed with the toolset you created here.

I was curious about inheritance support so I tried it out with a minimal example:

openapi: 3.0.2
info:
  title: edit me
  version: 1.0.0
paths:
  /nothing:
    description: dummy
    get:
      responses:
        '200':
          description: empty
          content:
            'application/json':
              schema:
                $ref: '#/components/schemas/ChildSchema'
components:
  schemas:
    ParentSchema:
      type: object
      properties:
        parentAttr:
          type: string
    ChildSchema:
      allOf:
        - $ref: '#/components/schemas/ParentSchema'
        - type: object
          properties:
            childAttr:
              type: string

This yields a ChildSchema.java consisting of everything from the parent and child model:

public class ChildSchema {

    @JsonProperty("parentAttr")
    private String parentAttr;

    @JsonProperty("childAttr")
    private String childAttr;

    public String getParentAttr() {
        return parentAttr;
    }

    public void setParentAttr(String parentAttr) {
        this.parentAttr = parentAttr;
    }

    public String getChildAttr() {
        return childAttr;
    }

    public void setChildAttr(String childAttr) {
        this.childAttr = childAttr;
    }

}

Now specifying a discriminator results in inheritance in the generated code (at least for the openapi-generator project):

openapi: 3.0.2
info:
  title: edit me
  version: 1.0.0
paths:
  /nothing:
    description: dummy
    get:
      responses:
        '200':
          description: empty
          content:
            'application/json':
              schema:
                $ref: '#/components/schemas/ChildSchema'
components:
  schemas:
    ParentSchema:
      type: object
      discriminator:
        propertyName: className
      properties:
        parentAttr:
          type: string
    ChildSchema:
      allOf:
        - $ref: '#/components/schemas/ParentSchema'
        - type: object
          properties:
            childAttr:
              type: string

For this a special propertyName of className is used. If I run that in your playground with spring 2021.5 it complains about the property name which is not wrong strictly speaking:
The discriminator 'className' is not a property of this schema (code: 134)
If I run this locally with 2023.1.2 then this does not error but does not generate two classes with one inheriting from each other:

public class ChildSchema {

    @JsonProperty("parentAttr")
    private String parentAttr;

    @JsonProperty("childAttr")
    private String childAttr;

    public String getParentAttr() {
        return parentAttr;
    }

    public void setParentAttr(String parentAttr) {
        this.parentAttr = parentAttr;
    }

    public String getChildAttr() {
        return childAttr;
    }

    public void setChildAttr(String childAttr) {
        this.childAttr = childAttr;
    }

}

Expected result would have been:

public class ParentSchema {

    @JsonProperty("parentAttr")
    private String parentAttr;

    public String getParentAttr() {
        return parentAttr;
    }

    public void setParentAttr(String parentAttr) {
        this.parentAttr = parentAttr;
    }

}

public class ChildSchema extends ParentSchema {

    @JsonProperty("childAttr")
    private String childAttr;

    public String getChildAttr() {
        return childAttr;
    }

    public void setChildAttr(String childAttr) {
        this.childAttr = childAttr;
    }

}

Now this expectation could of course be wrong. My question now would be if this inheritance use case is supported and/or if I would have to do something differently to get the desired result. Thanks.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci gli esempi OpenAPI minimi dell’issue nel playground con spring 2021.5 e localmente con 2023.1.2. Confronta l’output generato di ParentSchema e ChildSchema con la gerarchia di classi separate prevista, quindi determina se il supporto all’ereditarietà è previsto e quale comportamento dovrebbe essere documentato o modificato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java, kotlin, spring-boot
Ambito
api, backend, tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
20/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.