swagger codegen正在覆盖生成文件中的自定义代码

swagger codegen正在覆盖生成文件中的自定义代码,swagger,codegen,Swagger,Codegen,我使用swagger codegen生成jaxrs服务器端类和客户端java类 这是我用来生成类的命令 java -jar modules/swagger-codegen-distribution/target/swagger-codegen-distribution-2.1.2-M1.jar -i /Users/me/Workspace/swagger-codegen/samples/yaml/echo.yaml -l jaxrs -o samples/server/echo/ja

我使用swagger codegen生成jaxrs服务器端类和客户端java类

这是我用来生成类的命令

java -jar modules/swagger-codegen-distribution/target/swagger-codegen-distribution-2.1.2-M1.jar   -i /Users/me/Workspace/swagger-codegen/samples/yaml/echo.yaml   -l jaxrs   -o samples/server/echo/java
生成的服务器代码有一个占位符来写我的“魔术”

我在“echo”方法中添加了“magic”并重新生成了代码,结果却发现它被删除了。避免丢失自定义代码的一种方法是修改codegen模板以生成接口而不是类。然后我可以在实现的类中拥有所有自定义代码

我想知道是否有一种方法可以保存这种“魔法”即使在重新生成代码之后,或者如果有更好的方法处理这种情况,而不是更改模板以生成接口而不是类。

的最新主控程序允许您在代码生成过程中指定要在.swagger codegen ignore(类似于.gitignore)中覆盖的文件而不是

请拉最新的大师大摇大摆Codegen给它一个尝试


更新:2018年5月,大约50名Swagger Codegen的顶级贡献者和模板创建者决定使用Swagger Codegen来维护一个名为的社区驱动版本。有关详细信息,请参阅。

您可以在
文件中指定要忽略的文件。swagger codegen ignore
文件

这是
的示例自我解释自动生成的代码。swagger codegen ignore
文件

# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
您可以在下面添加一些行以忽略,例如,我想忽略文件夹impl中的所有文件,因此我添加了下面的行来完成此操作

**/impl/*

你好,
也许四年后,答案来得有点晚,但最好是晚一点,而不是永远

如果您有一个正确的招摇过市文件(不仅仅是一个片段),如下所示

openapi: "3.0.0"
:
paths:
  /example:
    get:
      operationId: showIt
:
        String className = this.getClass().getSimpleName();
        System.out.println("Entered REST endpoint: path=|" + className.substring(0, className.length() - 14) + "| operationId=|showId|");
在本说明中,您运行了一个代码生成,用于jaxsjersey服务器,没有任何特定于代码生成的配置值(您可以从 ),您会得到大量java类,如下所示:

io.swagger.api.          ExampleApi
io.swagger.api.          ExampleApiService
io.swagger.api.factories.ExampleApiServicefactory
io.swagger.api.impl.     ExampleApiServiceImpl
package io.swagger.api.impl;

:
import ... ;
:

@javax.annotation.Generated(...)
public
class   ExampleApiServiceImpl
extends ExampleApiService
{
    // ...
    @Override
    public
    Response showIt( /* additional parameters , */ SecurityContext securityContext)
    throws NotFoundException
    {
        // do some magic!
        return Response.ok()
                       .entity(new ApiResponseMessage( ApiResponseMessage.OK
                                                     , "magic!"
                                                     )
                              )
                       .build();
    }
    // ...
}
        <init-param>
            <param-name>ExampleApi.implementation</param-name>
            <param-value>my.swagger.api.MyExample</param-value>
        </init-param>
在REST端点实现示例ApiserviceImpl中,您或多或少会看到以下内容:

io.swagger.api.          ExampleApi
io.swagger.api.          ExampleApiService
io.swagger.api.factories.ExampleApiServicefactory
io.swagger.api.impl.     ExampleApiServiceImpl
package io.swagger.api.impl;

:
import ... ;
:

@javax.annotation.Generated(...)
public
class   ExampleApiServiceImpl
extends ExampleApiService
{
    // ...
    @Override
    public
    Response showIt( /* additional parameters , */ SecurityContext securityContext)
    throws NotFoundException
    {
        // do some magic!
        return Response.ok()
                       .entity(new ApiResponseMessage( ApiResponseMessage.OK
                                                     , "magic!"
                                                     )
                              )
                       .build();
    }
    // ...
}
        <init-param>
            <param-name>ExampleApi.implementation</param-name>
            <param-value>my.swagger.api.MyExample</param-value>
        </init-param>
你现在交换魔法评论了吗

        // do some magic!
也许通过以下途径

openapi: "3.0.0"
:
paths:
  /example:
    get:
      operationId: showIt
:
        String className = this.getClass().getSimpleName();
        System.out.println("Entered REST endpoint: path=|" + className.substring(0, className.length() - 14) + "| operationId=|showId|");
如果在完成
mvn clean package jetty:run
后从浏览器调用端点,则应该会看到一条日志消息。但正如你所意识到的那样,这不是一个好主意,因为在下一代人之后,你的改变就消失了

在这种情况下,手动更改生成的代码从来都不是一个好主意,因为这必须有很好的文档记录,以至于未来的同事(可能在几个月或几年后甚至是你)甚至在周一晚上的周日半睡不着,在下一代代码生成后再次进行更改。但我20多年使用不同代码生成器的经验只能说明一件事:算了吧出于同样的原因,阻止第一代之后的下一代并不是真正的目标导向,因为这也需要大量的文档记录。否则,一小时一小时的调试可能会找出新功能无法工作的原因

但这些都不是必需的。
在生成的类io.swagger.api.ExampleApi中,您会发现一个如下所示的构造函数(好的,那是2019-05-17的状态。我不知道它是否与四年前相同(或相似)

导入的代码是
servletContext.getInitParameter(“…”)
。若您现在在servlet配置中指定了一个名为
ExampleApi.implementation
的键,该键具有从
ExampleApi服务
派生的完整限定java类名,那个么您已经实现了自己的端点代码,该代码在以后的代码生成中可以安全地覆盖

为了完成这个示例,这个规范将在(额外生成的,oouuch,对不起,您不能拥有一切)
web.xml
文件中创建。此文件包含以下内容:

    <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        ...
        <load-on-startup>1</load-on-startup>
    </servlet>

运动衫
org.glassfish.jersey.servlet.ServletContainer
...
1.
在此xml片段中,必须在句点(代表其他servlet配置设置)之后插入以下内容:

io.swagger.api.          ExampleApi
io.swagger.api.          ExampleApiService
io.swagger.api.factories.ExampleApiServicefactory
io.swagger.api.impl.     ExampleApiServiceImpl
package io.swagger.api.impl;

:
import ... ;
:

@javax.annotation.Generated(...)
public
class   ExampleApiServiceImpl
extends ExampleApiService
{
    // ...
    @Override
    public
    Response showIt( /* additional parameters , */ SecurityContext securityContext)
    throws NotFoundException
    {
        // do some magic!
        return Response.ok()
                       .entity(new ApiResponseMessage( ApiResponseMessage.OK
                                                     , "magic!"
                                                     )
                              )
                       .build();
    }
    // ...
}
        <init-param>
            <param-name>ExampleApi.implementation</param-name>
            <param-value>my.swagger.api.MyExample</param-value>
        </init-param>

示例API.1实现
my.swagger.api.MyExample
好看,

不管你现在是什么

另请参见将文件添加到.swagger codegen ignore后合并新端点样板代码的工作流程。它确实应该生成一个包含方法的类,而不仅仅是函数。然后我们可以干净地将其子类化。请您就我的问题提供一些意见,这实际上也适用于maven openapi codegen插件!.openapi生成器ignore将在target/generated sources目录中生成,并且可以/不应该更新,因此这是mvn用户的最佳解决方案。