【发布时间】:2018-11-17 04:24:32
【问题描述】:
如何让 swagger codegen 使用现有类而不是创建新类?这可能吗?例如,我想使用 org.springframework.data.domain.Page 而不是大摇大摆地创建另一个页面类。
【问题讨论】:
标签: swagger-codegen
如何让 swagger codegen 使用现有类而不是创建新类?这可能吗?例如,我想使用 org.springframework.data.domain.Page 而不是大摇大摆地创建另一个页面类。
【问题讨论】:
标签: swagger-codegen
你可以使用--import-mappings,正如here所解释的那样:
有时您不希望生成模型。在这种情况下,您可以 只需指定一个导入映射来告诉 codegen 什么不要 创造。执行此操作时,每个引用特定位置的位置 模型将引用您的类。
您在swagger-codegen-cli generate 上调用它,您包含的示例将是
--import-mappings Page=org.springframework.data.domain.Page
虽然importMappings 没有包含在通用配置参数here 中,但如果您查看代码here,您可以看到它是List<String>。
我没有将它与 maven 插件一起使用,但查看文档和我猜这应该可以工作的代码:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2-SNAPSHOT</version>
<executions>
<execution>
...
<configuration>
...
<importMappings>
<importMapping>Page=org.springframework.data.domain.Page</importMapping>
</importMappings>
</configuration>
</execution>
</executions>
</plugin>
但这是recently changed,所以如果您使用的是旧版本的插件,可能会有所不同。在此之前,它似乎是这样的:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2-SNAPSHOT</version>
<executions>
<execution>
...
<configuration>
...
<configOptions>
<import-mappings>Page=org.springframework.data.domain.Page;Some=org.example.Some</import-mappings>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
根据该提交中的评论,旧版本也应该受到支持,但我没有尝试过任何这些,所以让我知道它是否有效。
【讨论】:
import org.springframework.data.domain.Page 所以我必须无论如何手动编辑模板文件。你也不能改变你的班级的名字,所以我不能有资源 A 的页面,资源 B 是同一个招摇文件。哈哈,伙计,我要在github上提交一个问题哈哈。
Page?
这里提到的答案都没有谈到要添加到 swagger yaml 文件中的内容, 如果有人感兴趣,这对我有用:
DisplayProperty:
type: object
properties:
name:
type: string
displayName:
$ref: '#/components/schemas/Text'
isRequired:
type: boolean
example: false
Text:
type: object
然后在 pom 中有这个
<importMappings>
<importMapping>Text=com.--.--.--.--.Text</importMapping>
【讨论】:
如果您的映射列表很长,则并不总是可以使用--import-mappings。
(至少在 Windows 的情况下,它在命令提示符下对字符串有大小限制。)
这就是为什么更好的方法:将映射与 swagger 配置文件一起使用。
(并且此选项没有完整记录。)
这样:
java -jar swagger-codegen-cli-2.3.1.jar generate -i myspec.yaml -l java -c myconfig.json
myconfig.json:
{
"hideGenerationTimestamp": true,
"dateLibrary": "java8",
"useRuntimeException": true,
"modelPackage": "org.my.package.model",
"apiPackage": "org.my.package.api",
"importMappings": {
"Page": "org.springframework.data.domain.Page",
"MySuperType": "org.my.SuperType"
}
}
【讨论】: