swagger-codegen - Swagger Codegen使用现有类

标签 swagger-codegen

我如何才能获得敏捷的代码生成器,以使用现有的类而不是创建新的类?这可能吗?例如,我想使用org.springframework.data.domain.Page而不是大张旗鼓地创建另一个页面类。

最佳答案

您可以使用--import-mappings,因为它解释了here:

Sometimes you don't want a model generated. In this case, you can simply specify an import mapping to tell the codegen what not to create. When doing this, every location that references a specific model will refer back to your classes.



您可以在swagger-codegen-cli generate上调用它,包括的示例将是
--import-mappings Page=org.springframework.data.domain.Page
尽管在常规配置参数here中未包含importMappings,但是如果您查看代码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>

根据该提交中的评论,也应该支持旧版本,但是我没有尝试过任何版本,因此请让我知道它是否有效。

关于swagger-codegen - Swagger Codegen使用现有类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612080/

相关文章:

flutter - 从 Swagger-codegen 生成 DART 包

flutter - 如何在启用 nullsafety 的情况下从 swagger json 生成 dart 客户端?

c# - 自定义生成的模型名称 - Swagger UI

javascript - Swagger 客户一代

api - 在 Open API AKA Swagger 生成的代码中,空数组未被选为定义模型的默认值

java - 来自现有 JSON 的 Springfox Swagger Ui

jmeter - Swagger Codegen JMeter 测试数据模板

java - SwaggerGen 枚举生成空数据类型

java - Swagger-codegen 重命名具有相同名称的方法