如何更改“APIClient”名称

时间:2021-06-04 22:03:04

标签: openapi swagger-codegen openapi-generator apiclient

我正在尝试从 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。

如果有人运气好,请告诉我。

1 个答案:

答案 0 :(得分: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";
    }

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

org.openapitools.codegen.CustomJavaGenerator

  • 下载 this fileresources/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"
    ]
}
  • Gradle 命令/gradlew clean openApiGenerate