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

Inheritance support

Open
#159 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
56
Forks
11
Avg merge
2d 4h
Merged PRs (30d)
8

Description

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.

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

Reproduce the minimal OpenAPI examples from the issue in the playground with spring 2021.5 and locally with 2023.1.2. Compare the generated ParentSchema and ChildSchema output with the expected separate class hierarchy, then determine whether inheritance support is intended and what behavior should be documented or changed.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.