Swagger Codegen:继承和组合不按预期工作

时间:2016-09-19 17:18:41

标签: java inheritance swagger swagger-codegen

我有以下简短的YAML:

# Transaction Request object with minimal information that we need
  Parent:
    required:
  - a
  - b
  - c
properties:
  a:
    type: number
    format: double
  b:
    type: string
  c:
    type: string

# Full transaction
Child:
required:
  - a 
  - b
  - c
allOf:
  - $ref: "#/definitions/Parent"
properties:    
  date: 
    type: string 
    format: date-time
  state: 
    type: string 
    enum: 
      - 1
      - 2
      - 3

在Swagger UI和编辑器中,这些对象按照我的意愿显示:Child继承a的{​​{1}},bc字段,还有一些额外的。

我原以为:

Parent

public class Parent {

  private Double a;
  private String b;
  private String c;

  ...}

但是,虽然 public class Child extends Parent { // Superclass fields as well as: private Date date; private enum State {...}; ...} 课程看起来符合预期,但我的Parent课程包含以下内容:

Child

甚至缺少public class Child { @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Child child = (Child) o; return true; } ... } 。当使用extends它有效时,我并不真正想要多态 - 只是简单的继承。如何使用Swagger Codegen实现这一目标?

相关的discriminator条目:

pom.xml

1 个答案:

答案 0 :(得分:1)

Cat:
  allOf:
  - $ref: "#/definitions/Animal"
  - type: "object"
    properties:
      declawed:
        type: "boolean"
Animal:
  type: "object"
  required:
  - "className"
  discriminator: "className"
  properties:
    className:
      type: "string"
    color:
      type: "string"
      default: "red"

您需要在父类

添加代码
required:
    - "className"