如何使用 openapi-generator-maven-plugin 生成 OpenApi 客户端?

dur*_*dur 5 maven openapi openapi-generator

我有一个OpenAPI(版本 2)规范文件,我想生成一个带有openapi-generator-maven-plugin. 不幸的是,它没有生成所有必需的类,因此无法编译生成的源。

聚甲醛

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.1.0</version>
    <executions>
        <execution>
            <id>generate-client</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
                <inputSpec>${project.basedir}/src/main/openapi/api.json</inputSpec>
                <generatorName>java</generatorName>
                <generateApiDocumentation>false</generateApiDocumentation>
                <generateApiTests>false</generateApiTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <configOptions>
                    <sourceFolder>/</sourceFolder>
                </configOptions>
                <library>resttemplate</library>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

开放API

{
    "swagger": "2.0",
    "info": {
        "title": "Test"
    },
    "host": "localhost:8080",
    "basePath": "/",
    "tags": [
        {
            "name": "test-controller"
        }
    ],
    "paths": {
        "/test": {
            "get": {
                "tags": [
                    "test-controller"
                ],
                "operationId": "test",
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

生成的源

该目录下只生成子包的target/generated-sources/openapi文件。未生成该类。TestControllerApiapiorg.openapitools.client.ApiClient

目录

日志

[INFO] --- openapi-generator-maven-plugin:5.1.0:generate (generate-client) @ test ---
[INFO] Generating with dryRun=false
[INFO] No .openapi-generator-ignore file found.
[INFO] OpenAPI Generator: java (client)
[INFO] Generator 'java' is considered stable.
[INFO] Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE="/usr/local/bin/clang-format -i"' (Linux/Mac)
[INFO] NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[INFO] Processing operation test
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[ERROR] Missing required field info version. Default appVersion set to 1.0.0
[ERROR] Missing required field info version. Default version set to 1.0.0
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[INFO] writing file D:\tmp\workspace\test\target\generated-sources\openapi\org\openapitools\client\api\TestControllerApi.java
[INFO] Skipped D:\tmp\workspace\test\target\generated-sources\openapi\src\test\java\org\openapitools\client\api\TestControllerApiTest.java (Skipped by apiTests options supplied by user.)
[INFO] Skipped D:\tmp\workspace\test\target\generated-sources\openapi\docs\TestControllerApi.md (Skipped by apiDocs options supplied by user.)
[WARNING] 'scheme' not defined in the spec (2.0). Default to [http] for server URL [http://localhost:8080/]
[INFO] Skipping generation of supporting files.
[...]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 51 source files to D:\tmp\workspace\test\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /D:/tmp/workspace/test/target/generated-sources/openapi/org/openapitools/client/api/TestControllerApi.java:[3,31] cannot find symbol
  symbol:   class ApiClient
  location: package org.openapitools.client
Run Code Online (Sandbox Code Playgroud)

研究

我发现了一个相关问题:10048,但它不包含解决方案。

问题

如何配置插件来生成所有必需的类?

小智 3

generateSupportingFiles应该true

  • 我关闭这个标志,因为我不想生成诸如:`build.gradle`、`git_push.sh`之类的文件,...它不是必需的,而且它也位于生成源下的错误位置。仅当我生成整个项目,但我想将客户端包含到现有项目中时,这才有意义。 (2认同)