用于 OpenAPI 3.0 的 Swagger Codegen(带有 maven 插件)

Swagger Codegen (with maven plugin) for OpenAPI 3.0

我想对 OpenAPI 3.0 YAML 文件使用 Swagger Codegen。我看到 Swagger Codegen 3.0.0-rc0 可用。但是当我尝试使用它时,我 运行 遇到了问题。详情如下:

我的 pom.xml 带有 swagger-codegen 插件的文件:

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.0-rc0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${basedir}/src/main/resources/mySpec.yaml</inputSpec>
        <output>target/generated-sources</output>
        <language>spring</language>
        <generateApis>false</generateApis>
        <modelPackage>com.kj.model</modelPackage>
        <apiPackage>com.kj</apiPackage>
        <configOptions>
          <sourceFolder>swagger</sourceFolder>
          <library>spring-mvc</library>
          <interfaceOnly>true</interfaceOnly>
          <useBeanValidation>true</useBeanValidation>
          <dateLibrary>java8</dateLibrary>
          <java8>true</java8>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

当我 运行 maven 构建时使用上面的插件,我得到了这个 ServiceConfigurationError,这是堆栈跟踪:

Exception in thread "main" java.util.ServiceConfigurationError: io.swagger.codegen.CodegenConfig: Provider io.swagger.codegen.languages.java.JavaClientCodegen not found
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access0(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader.next(ServiceLoader.java:480)
    at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:19)
    at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:392)
    at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:512)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)

为了解决这个问题,我在 pom 文件的 Maven 插件 部分添加了 swagger-codegen-generators 依赖项

<dependencies>
  <dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-generators</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </dependency>
</dependencies>

所以前面提到的这个问题已经解决了,但现在我看到了这个 NPE

java.lang.NullPointerException
    at io.swagger.codegen.languages.SpringCodegen.preprocessOpenAPI(SpringCodegen.java:429)
    at io.swagger.codegen.DefaultGenerator.configureGeneratorProperties(DefaultGenerator.java:199)
    at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:716)
    at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:534)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)

正如您已经注意到的,我正在使用 <language>spring</language><library>spring-mvc</library>。请让我知道 codegen 是否为具有这些配置的 3.0.0-rc0 的某人工作。

注:我看了这个旧的post,很相似,但当时3.0.0-rc0没有。

要将 Swagger Codegen 与适用于 OpenAPI 3.0.0 规范的 Maven 插件一起使用,您可以考虑改用 OpenAPI Generator(这是 Swagger Codegen 的社区驱动版本)。

<dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>3.3.4</version>
</dependency>

参考:https://github.com/OpenAPITools/openapi-generator#12---artifacts-on-maven-central

(请参阅 Q&A 为什么我们分叉 Swagger Codegen)

2019 年 4 月发布的 v3 swagger codegen maven 插件根据 OpenAPI 3.0 规范生成工作 Java 客户端库,我正在使用此 Maven pom.xml 插件配置:

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.8</version>
    <executions>
    ..

所有其余的配置和 configOptions 条目与版本 2.4.5 相比没有变化。我必须用以下内容替换旧的注释依赖项,以便客户端代码可以编译:

    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.0.8</version>
    </dependency>

应下面@kozla13 的请求,我添加了一个完整的 POM 示例。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- This groupId will NOT allow deployment in LF -->
    <groupId>org.example.swaggerapi.client</groupId>
    <artifactId>swagger-client</artifactId>
    <name>Example</name>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <client.base.package.name>org.example.so</client.base.package.name>
    </properties>
    <dependencies>
        <!-- Required for Java 9 and later -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
        <!-- HTTP client: Spring RestTemplate -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
        <!-- JSON processing: jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.9.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.9</version>
        </dependency>
        <!-- test dependencies -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <!-- This 2019 version is required for OpenAPI 3 -->
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/ric-plt-a1/a1/openapi.yaml</inputSpec>
                            <language>java</language>
                            <packageName>${client.base.package.name}</packageName>
                            <modelPackage>${client.base.package.name}.model</modelPackage>
                            <apiPackage>${client.base.package.name}.api</apiPackage>
                            <invokerPackage>${client.base.package.name}.invoker</invokerPackage>
                            <configOptions>
                                <groupId>${project.groupId}</groupId>
                                <artifactId>${project.artifactId}</artifactId>
                                <artifactVersion>${project.version}</artifactVersion>
                                <library>resttemplate</library>
                                <java8>true</java8>
                                <dateLibrary>java8</dateLibrary>
                                <licenseName>Apache 2.0</licenseName>
                                <licenseUrl>https://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>