更新 Swagger code-gen 生成的代码 [英] Update code generated by Swagger code-gen

查看:51
本文介绍了更新 Swagger code-gen 生成的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 spring 中使用 swagger code-gen 从 swagger.yaml 文件生成了代码.

I have generated the code from swagger.yaml file using swagger code-gen in spring.

现在我已经为我的 API 更新了 swagger.yaml 文件并添加了更多的 HTTP 操作.

Now I have updated the swagger.yaml file for my API and added few more HTTP operations.

是否可以在不手动合并的情况下自动更新之前自动生成的现有代码?

Will it be possible to update the existing code generated previously automatically without merging it manually?

推荐答案

我猜你在谈论由 codegen 生成的控制器,然后你已经实现了.它们在每一代之后都会被覆盖,这意味着您每次都必须手动合并代码以添加更改......这真的很烦.

I guess you are talking about the Controllers generated by codegen, that you have then implemented. They are overwritten after each generation, which means you will have to manually merge the code to add the changes every time... which is really annoying.

我能找到的最好的工作流程是使用 interfaceOnly 选项只在目标目录中生成模型和接口类,然后手动创建实现这些接口的控制器.

Well the best workflow I could find was to use the interfaceOnly option to generate only the model and interface classes in the target directory, and then manually create the controllers that implement those interfaces.

假设您使用另外一个 GET 操作更新您的 API 规范文件,使用该新操作重新生成界面,您只需调整控制器以实现该新方法(使用现代 IDE 非常快速和简单),一切其他保持不变,您可以更好地控制代码(将控制器拆分到不同的文件夹中……等等……).

Lets say you update your API specification file with one more GET operation, the interface is regenerated with that new operation and you will just have to adjust your controller to implement that new method (super quick and easy with modern IDE), everything else remain the same and you have more control over your code (splitting controllers in different folders...etc...).

这是我用于插件的配置:

Here is config I used for the plugin:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>./api-contract/petstore.yml</inputSpec>
                <language>spring</language>
                <configOptions>
                    <sourceFolder>swagger</sourceFolder>
                    <java8>true</java8>
                    <interfaceOnly>true</interfaceOnly>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

您可以使用带有 swagger-codegen-maven-plugin 的 Spring Boot 检查完整的示例项目 此处.

You can check a complete example project using Spring Boot with swagger-codegen-maven-plugin here.

干杯

这篇关于更新 Swagger code-gen 生成的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆