1

我正在尝试从 swagger-codegen-maven-plugin(版本 3.0.8)和(组 ID:io.swagger.codegen.v3)自动生成类,如下所示。代码生成效果很好,但是我想将 Generated ApiClient 的名称更改为 PREFIX+ApiClient 之类的名称(例如:customApiClient,其中 custom 是前缀)。

<build>
        <finalName>cdm-customer-servicing-api-client</finalName>
        <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}/src/main/resources/spec.json</inputSpec>
                            <language>java</language>
                            <apiPackage>*****.client.api</apiPackage>
                            <modelPackage>*******. client.model</modelPackage>

                            <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>
                            <additionalProperties>
                                <property></property>
                            </additionalProperties>
                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateModelDocumentation>false</generateModelDocumentation>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

上面的定义生成模型,在 YML 规范中指定的 api,没有问题。它还生成在所有 API 类中自动装配的 ApiClient.java。我想更改 ApiClient.java 的名称以添加一些前缀或后缀。

原因是:我确实有 2 个服务规范,我想确保来自一个服务的 ApiClient 不会覆盖另一个 ApiClient。

让我知道是否有人有运气。

4

1 回答 1

0

可以实现一个扩展的自定义生成器JavaClientCodegen.java

  • 创建一个具有这种结构的项目(名称由您决定)。可以使用创建骨架./gradlew clean openApiMeta(找不到 mvn 等效项)
src/
└── main
    ├── java
    │   └── org
    │       └── openapitools
    │           └── codegen
    │               └── CustomJavaGenerator.java
    └── resources
        ├── customjava
        │   └── CustomApiClient.mustache
        └── META-INF
            └── services
                └── org.openapitools.codegen.CodegenConfig
  • CustomJavaGenerator.java
public class CustomJavaGenerator extends JavaClientCodegen implements CodegenConfig {

    private final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
    private static final String CUSTOM_API_CLIENT_PREFIX = "customApiClientPrefix";

    @Override
    public void processOpts() {
        super.processOpts();

        final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
    
        String customPrefix = "DefPref";
        if (additionalProperties.containsKey(CUSTOM_API_CLIENT_PREFIX)) {
            customPrefix = additionalProperties.get(CUSTOM_API_CLIENT_PREFIX).toString();
            LOGGER.warn("client custom name: " + customPrefix + "ApiClient.java" );
        }
        supportingFiles.removeIf(e -> "ApiClient.java".equals(e.getDestinationFilename()));
        supportingFiles.add(new SupportingFile("CustomApiClient.mustache", invokerFolder, customPrefix + "ApiClient.java"));
        
    }

    @Override
    public String getName() {
        return "javaCustom";
    }

}
  • 资源/META-INF/services/org.openapitools.codegen.CodegenConfig

org.openapitools.codegen.CustomJavaGenerator

  • 下载 此文件并替换(区分大小写)resources/customjava/CustomApiClient.mustache的每个实例,如图所示ApiClient

public class {{customApiClientPrefix}}ApiClient { ...

  • 打包并安装项目,使其在 mavenLocal 存储库中显示为例如customjava-openapi-generator-1.0.0.jar.

  • 在这种情况下,使用 gradle生成代码。

将自定义 jar 添加到类路径,并将openApiGenerate配置添加到 gradle.build。maven应该是类似的。

buildscript {
    repositories {
        mavenLocal()
        jcenter()
        maven { url "https://repo1.maven.org/maven2" }
    }
    dependencies {
        // other dependencies
        
        // custom generator
        classpath "org.openapitools:customjava-openapi-generator:1.0.0"
        classpath "org.openapitools:openapi-generator-gradle-plugin:5.1.0"
    }
}

repositories {
    jcenter()
}

apply plugin: 'org.openapi.generator'

openApiGenerate {
    //verbose = true
    generatorName = "javaCustom"
    inputSpec = "$rootDir/generate/proj-swa01-1.0.0-resolved.yaml".toString()
    outputDir = "$buildDir/generated".toString()
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"
    
    templateDir = "customjava"
    configOptions = [
        dateLibrary: "java8"
    ]
    additionalProperties = [
        customApiClientPrefix: "Swa"
    ]
}
  • 摇篮命令/gradlew clean openApiGenerate
于 2021-06-05T05:52:03.687 回答