使用maven从源代码生成兼容jakarta swagger-ui的JSON

jdzmm42g  于 2023-04-30  发布在  Maven
关注(0)|答案(1)|浏览(121)

bounty明天到期。回答此问题可获得+100声望奖励。Nephilim正在寻找此问题的最新答案:给了我一个关于如何从已有代码生成预期的openAPI/swaggerjson的连贯计划

这里有很多类似的,但过时的答案。我最近一直在迁移一个旧项目的JavaEE -〉Jakarta。注意到我所有的swagger相关配置文件都停止生成预期的JSON/YAML。
所以,它以前是如何工作的,就是它会使用com。github.kongchen -〉swagger-maven-plugin.它会施展魔法,生成一个swagger-ui可以轻松绘制的json。

<plugin>
                    <groupId>com.github.kongchen</groupId>
                    <artifactId>swagger-maven-plugin</artifactId>
                    <version>${swagger.maven-plugin.version}</version>
                    <configuration>
                        <apiSources>
                            <apiSource>
                                <springmvc>false</springmvc>
                                <locations>
                                    <location>path.to.source</location>
                                </locations>
                                <schemes>
                                    <sheme>http</sheme>
                                </schemes>
                                <host>${swagger.api.host}</host>
                                <basePath>${swagger.api.basePath}</basePath>
                                <info>
                                    <title>Specs</title>
                                    <version>v1</version>
                                    <description>${swagger.api.header.description}</description>                                  
                                </info>                            
                                <templatePath>${apisource.templatePath}</templatePath>
                                <outputPath>${apisource.outputPath}</outputPath>
                                <swaggerDirectory>${apisource.swaggerDirectory}</swaggerDirectory>                                    
                            </apiSource>
                        </apiSources>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

它生成的JSON看起来像这样:

{
  "swagger" : "2.0",
  "info" : {
    "description" : "some description"
    "version" : "v1",
    "title" : "Title"
  },
  "host" : "localhost:8080",
  "basePath" : "/path/rest",
  "tags" : [ {
    "name" : "Service 1"
  }, {
    "name" : "Service 2"
    ...

请求、响应模型等。都是正常生成的。基本上我想做的是,有这个,但与Jakarta命名空间兼容。
迁移这个,我不得不使用一个不同的插件,因为kongchen不支持jakarta。
生成的配置文件如下所示

<profile>
        <id>new-generate-api-doc</id>
        <dependencies>
            <dependency>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-jaxrs2-jakarta</artifactId>
                <version>2.2.8</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>swagger-ui</artifactId>
                <version>${swagger-ui.version}</version>
            </dependency>
            <dependency>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-integration-jakarta</artifactId>
                <version>2.2.8</version>
            </dependency>
            <dependency>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-annotations-jakarta</artifactId>
                <version>2.2.8</version>
            </dependency>

        </dependencies>
        <properties>
            <swagger.api.version>v1</swagger.api.version>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>io.swagger.core.v3</groupId>
                    <artifactId>swagger-maven-plugin-jakarta</artifactId>
                    <version>2.2.8</version>

                    <configuration>
                        <outputFileName>swagger</outputFileName>
                        <outputPath>${apisource.swaggerDirectory}</outputPath>
                        <outputFormat>JSONANDYAML</outputFormat>
                        <resourcePackages>
                            <package>com.packages.source</package>
                        </resourcePackages>
                        <prettyPrint>true</prettyPrint>
                        <ignoredRoutes></ignoredRoutes>
                        <configurationFilePath>${apisource.swaggerDirectory}/openapi-configuration.yaml</configurationFilePath>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>resolve</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

有人会问,这是openapi-configuration。亚姆勒:

resourcePackages:
- io.swagger.sample.resource
prettyPrint: true
cacheTTL: 0
openAPI:
  info:
    version: '1.0'
    title: API services specification
    description: Desc
    termsOfService: http://swagger.io/terms/

但是解析后的JSON看起来与之前的完全不同。没有标签,没有回应。很明显我做错了什么。
示例:

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "services specification",
    "termsOfService" : "http://swagger.io/terms/",
    "version" : "1.0"
  },
  "paths" : {
    "/path/ann" : {
      "get" : {
        "operationId" : "getAnn",
        "parameters" : [ {
          "name" : "type",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "orderby",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        } ],
        **"responses" : {
          "default" : {
            "description" : "default response",
            "content" : {
              "application/json" : { }
            }
          }**
        }
      },
...
    "/api/v1/asset" : {
      "get" : {
        "operationId" : "getAllAssetsFromAssetIndex",
        "parameters" : [ {
          "name" : "regnumber",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "vinnumber",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        }, {
          "name" : "orderby",
          "in" : "query",
          "schema" : {
            "type" : "string"
          }
        } ],
        "responses" : {
          "default" : {
            "description" : "default response",
            "content" : {
              "application/json" : { }
            }
          }
        }
      },

请注意空的“内容”字段,没有标签等。在本例中代码保持不变,因此使用了与以前相同的注解。
请求被正确地生成,名称空间也是如此。但值得注意的是,我正在生成一个完全不同的JSON,然后在显示中反映出来。
服务注解的示例:

@Stateless
@Path(AssetServiceApiV1.BASE_URL)
@Api(value = "/" + AssetServiceApiV1.BASE_URL, tags = {AssetServiceApiV1.ASSET_SERVICE_TAG})
public class AssetServiceApiV1 extends RestServiceBaseApiV1 {

    public static final String BASE_URL = "api/v1/asset";
    public static final String ASSET_SERVICE_TAG = "Asset service";

    @GET
    @ApiOperation(
            httpMethod = "GET",
            value = "Get assets" + BEGIN_PARENS + APIFunctionalities.API_ASSET_READ + END_PARENS,
            notes = "Returns active assets with basic data. Various optional parameters are available.",
            response = AssetListV1Dto.class,
            responseContainer = OBJECT,
            produces = MediaType.APPLICATION_JSON,
            tags = {ASSET_SERVICE_TAG},
            code = 207
    )
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAllAssetsFromAssetIndex(
            @ApiParam(value = "Registration number") @QueryParam("regnumber") String registrationNumber,
            @ApiParam(value = "...") @QueryParam("...") String ...,
            @ApiParam(value = "Available order by parameters are id, ..., ...") @QueryParam("orderby") String orderBy) {
...
    }

下面是响应对象:

@ApiModel(value = "Asset List Resource", description = "Asset list resource representation")
    public static class AssetListV1Dto {

        @ApiModelProperty(value = "List of assets", required = true)
        public List<AssetV1Dto> assetList;
        @ApiModelProperty(value = "List size", required = true)
        public int size;
...

    }

我可能离正确的解决方案只有几英里远,并且正在生成完全不同的东西。什么都行。这里不使用Spring,BTW。
但我完全卡住了,我不知道用什么来生成一个类似于旧插件生成的json,我一直在使用云上的swagger-editor来验证输出。

rjee0c15

rjee0c151#

https://www.david-merrick.com/2017/11/15/useful-regexes-for-transitioning-swagger-2-0-to-3-0-annotations/
结果发现还有一些东西仍然包含旧的注解,所以我的IDE和maven根本没有抱怨。
奇怪。这是一个很大的工作,但我认为,在这个问题的范围内,是解决方案。

相关问题