chi*_*uan 5 java json jackson swagger openapi
我有 2 个应用程序,其中应用程序 A 公开 API 供应用程序 B 使用。
应用程序 A 正在为应用程序 B 生成 swagger.json,应用程序 B 正在使用 swagger-codegen 生成模型代码。
在我的应用程序 A 中,它使用 Jackson 和 WRAPPER_OBJECT 进行多态处理
@JsonTypeInfo (use = JsonTypeInfo.Id.NAME, include =
JsonTypeInfo.As.WRAPPER_OBJECT, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = Dog.class, name = "doggy"),
@JsonSubTypes.Type(value = Cat.class, name = "kitty")})
@ApiModel(subTypes = {Dog.class, Cat.class}, discriminator = "type")
public abstract class Animal {
}
@ApiModel(value = "kitty", parent = Animal.class)
public class Cat extends Animal {
public int lives = 9;
}
@ApiModel(value = "doggy", parent = Animal.class)
public class Dog extends Animal {
public double barkVolume = 100d;
}
Run Code Online (Sandbox Code Playgroud)
期望 json 格式被包装。
{
"kitty": {
"lives": 9
}
}
Run Code Online (Sandbox Code Playgroud)
所以当应用程序A使用springfox生成swagger.json时,它生成如下。
"Animal": {
"type": "object",
"discriminator": "type",
"title": "Animal"
},
"doggy": {
"title": "doggy",
"allOf": [
{
"$ref": "#/definitions/Animal"
},
{
"type": "object",
"properties": {
"barkVolume": {
"type": "number",
"format": "double"
}
},
"title": "doggy"
}
]
},
"kitty": {
"title": "kitty",
"allOf": [
{
"$ref": "#/definitions/Animal"
},
{
"type": "object",
"properties": {
"lives": {
"type": "integer",
"format": "int32"
}
},
"title": "kitty"
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是当 swagger.json 给应用程序 B 生成代码时,问题就出现了,生成的代码没有包装。
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-08-28T12:54:58.555+08:00")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes({@JsonSubTypes.Type(value = Kitty.class, name = "kitty"), @JsonSubTypes.Type(value = Doggy.class, name = "doggy"),})
public class Animal {
}
Run Code Online (Sandbox Code Playgroud)
当 json 生成时,如下所示。
{
"type": "kitty",
"lives": 9
}
Run Code Online (Sandbox Code Playgroud)
我尝试检查 swagger-codegen 代码,似乎代码总是将其生成为 JsonTypeInfo.As.PROPERTY。
我只是想知道 swagger.json 中可以定义 jackson 对象的任何选项是否可以生成为 WARPPER_OBJECT?
| 归档时间: |
|
| 查看次数: |
1631 次 |
| 最近记录: |