我有以下简短的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}},b
和c
字段,还有一些额外的。
我原以为:
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
答案 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"