如何在swagger-maven-plugin 3.1.0中设置覆盖模型

时间:2015-07-17 09:40:29

标签: maven swagger swagger-ui swagger-2.0 swagger-maven-plugin

如何在swagger-maven-plugin 3.1.0和Swagger UI 2.0(或更新版本)中设置覆盖模型?

最近我们将Swagger UI从1.2升级到2.0,并将swagger-maven-plugin从2.3升级到3.1.0。

看来,swagger-maven-plugin 3.1.0版缺失了 版本2.3中提供的overridingModels选项。

该选项使我们能够自定义某些数据类型的架构描述,如:https://github.com/swagger-api/swagger-core/wiki/overriding-models中所述。

1 个答案:

答案 0 :(得分:0)

以下是我最终解决问题的方法(我使用的是swagger-maven-plugin 3.1.1版):

假设您要将java.util.Date映射为此任意结构:

{
  "year": "string",
  "month": "string",
  "day": "string"
}

步骤1:创建一个描述上述模型的类:

package com.example;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel
public class MyDate {

    @ApiModelProperty
    public String year;

    @ApiModelProperty
    public String month;

    @ApiModelProperty
    public String day;
}

注意:类名和包名是任意的,它们只需要在类路径上,因此该类对于插件是可见的,就像模型的其余部分一样。

第2步:在pom.xml中将此行添加到swagger-maven-plugin的配置中:

<plugin>
  <groupId>com.github.kongchen</groupId>
  <artifactId>swagger-maven-plugin</artifactId>
  <version>3.1.1</version>
  <configuration>
    <apiSources>
      <apiSource>
        ...
        <modelSubstitute>/model-substitute.csv</modelSubstitute>
      </apiSource>
    </apiSources>
  </configuration>
  ...
</plugin>

步骤3:使用以下映射创建文件model-substitute.csv

java.util.Date : com.example.MyDate

将文件放在与插件相同的maven模块中,位于目录src/main/resources中 注意:文件的名称是任意的,但它应该与pom.xml中的值匹配。