Swagger根据枚举值生成子类型

时间:2019-11-28 11:20:56

标签: java swagger swagger-codegen openapi-generator

我具有以下结构

             Notification
                  |
        ------------------------
        |                      |
  SmsNotification         EmailNotification

Notification包含一个枚举notificationType,其中包含SMSEMAIL。现在,我有一个Inbox类,其中包含一个Notification

这在svagger 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 {
  ...
}

这里的问题是,如果我现在尝试反序列化/序列化包含带有notificationType=EMAIL的收件箱的Json字符串,则它将抛出异常,因为没有名称为'EMAIL'的已知子类型。

seralizer期望像这样指定JsonSubType批注: (旁注,这也是生成摇摇欲坠的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注释吗?

1 个答案:

答案 0 :(得分:0)

对于有相同问题的任何人,对我有用的是用上面建议的插件(openapi-generator-maven-plugin)更改原始插件(swagger-codegen-maven-plugin)

相关问题