尝试使用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')
当我编辑上述属性时,我没有收到此错误。我想它已连接。有什么解决办法吗?
答案 0 :(得分:0)
此错误已在当前的3.0.5-SNAPSHOT中修复: 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