尝试使用的OpenAPI 3.0.2实现多态性。
Type1:
type: object
required:
- dn
- typeOfType
properties:
dn:
description: Description
type: string
typeOfType:
type: string
enum:
- Type2
- Type3
discriminator:
propertyName: typeOfType
Type2:
allOf:
- $ref: '#/components/schemas/Type1'
- type: object
properties:
name:
$ref: '#/components/schemas/name' #abstract object
required:
- name
Type3:
allOf:
- $ref: '#/components/schemas/Type1'
- type: object
properties:
surname:
$ref: '#/components/schemas/surname' #abstract object
required:
- surname
我生成的代码是这样的:
@Validated
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "Discriminator{propertyName='typeOfType', mapping=null}", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = WniosekEkwU1.class, name = "Type1"),
@JsonSubTypes.Type(value = WniosekEkwU1A.class, name = "Type2"),
})
public class Type1 implements Serializable {
private static final long serialVersionUID = 1L;
...
}
综上,多态是行不通的。我不知道如果我的发电机工作正常时,它会创建property = "Discriminator{propertyName='typeOfType', mapping=null}"
。我找不到在哪里产生类似代码的任何实例。当我将这段代码与property = "typeOfType"
一切工作正常。
是我的代码错误或更多的配置是必需的?
我也端点测试这个类。我收到错误:
Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Missing type id when trying to resolve subtype of [simple type, class pl.asdf.asdf.asdf.gen.model.type1]: missing type id property
'Discriminator{propertyName='typeOfType', mapping=null}' (for POJO property 'type1'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException:
Missing type id when trying to resolve subtype of [simple type, class pl.asdf.asdf.asdf.gen.model.Type1]:
missing type id property 'Discriminator{propertyName='typeOfType', mapping=null}' (for POJO property 'type1')
我不明白,当我修改上述属性此错误。我想它是连接。有没有什么解决办法吗?
此错误是固定在当前的3.0.5-快照:https://github.com/swagger-api/swagger-codegen-generators/issues/291
在你的代码的例子,我想提的唯一一件事就是删除枚举定义的属性typeOfType
这应该是足够了:
Type1:
type: object
discriminator:
propertyName: typeOfType
required:
- dn
- typeOfType
properties:
dn:
description: Description
type: string
typeOfType:
type: string