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

Inheritance support

Đang mở
#159 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Kotlin
Star
56
Fork
11
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
8

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện các ví dụ OpenAPI tối thiểu từ issue trong playground với spring 2021.5 và cục bộ với 2023.1.2. So sánh đầu ra được tạo của ParentSchema và ChildSchema với hệ thống phân cấp lớp riêng biệt như mong đợi, sau đó xác định liệu hỗ trợ kế thừa có được dự định hay không và hành vi nào nên được ghi lại trong tài liệu hoặc thay đổi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
java, kotlin, spring-boot
Lĩnh vực
api, backend, tooling
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.