------------------------
Notification
|
------------------------
| |
SmsNotification EmailNotification
该 Notification
包含一个枚举 notificationType
含有 SMS
或 EMAIL
. 现在我有一个 Inbox
类,其中包含一个 Notification
.
这在swagger yml中是这样指定的(删除了一些不相关的代码)
definitions:
Notification:
type: "object"
discriminator: "notificationType"
properties:
notificationType:
type: "string"
description: "Type of notification"
enum:
- "EMAIL"
- "SMS"
SmsNotification:
allOf:
- $ref: "#/definitions/Notification"
- type: "object"
EmailNotification
allOf:
- $ref: "#/definitions/Notification"
- type: "object"
Inbox:
type: "object"
properties:
notification:
description: "Latest received notification"
$ref: "#/definitions/Notification"
我用以下方法生成我的代码 swagger-codegen v2
(也试过v3 & openapi-generator),配置如下。
<build>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>notifications</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/notifications/swagger.yaml</inputSpec>
<language>java</language>
<library>jersey2</library>
<generateSupportingFiles>false</generateSupportingFiles>
<modelPackage>${generated.package}</modelPackage>
<generateApis>false</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
现在发生的情况是 jersey2
库将产生 JsonSubType
注解。
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.Property, property="notificationType", visible=true)
@JsonSubTypes({
@JsonSubTypes.Type(value=SmsNotification.class, name="SmsNotification"),
@JsonSubTypes.Type(value=EmailNotification.class, name="EmailNotification")
})
public class Notification {
...
}
这里的问题是,如果我现在尝试反序列化序列化一个包含收件箱的Json字符串,并使用 notificationType=EMAIL
异常,它将抛出一个异常,因为没有已知的子类型名称为 'EMAIL'
.
矿化器期望 JsonSubType
注解是这样指定的:(附注,这也是生成swagger yaml的代码的样子)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.Property, property="notificationType", visible=true)
@JsonSubTypes({
@JsonSubTypes.Type(value=SmsNotification.class, name="SMS"),
@JsonSubTypes.Type(value=EmailNotification.class, name="EMAIL")
})
public class Notification {
...
}
有谁知道如何生成 JsonSubTypes
注解,而不是当前的行为?