简体   繁体   中英

How to change "APIClient" name

I am trying to generate classes automatically from swagger-codegen-maven-plugin (version 3.0.8) and (group Id: io.swagger.codegen.v3) like below. Code generation works great however I want to change name of Generated ApiClient to something like PREFIX+ApiClient (EXAMPLE: customApiClient where custom is prefix).

<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>

Above definition generates models, apis specified in YML spec, no issues there. It also generates ApiClient.java which is autowired in all API classes. I want to change name of ApiClient.java to add some prefix or suffix.

Reason is: I do have 2 spec in service and I want to ensure that ApiClient from one service does not override another ApiClient.

Let me know if someone has a luck.

It can be done implementing a custom generator that extends JavaClientCodegen.java .

  • Create a project with this structure (names are up to you). Skeleton can be created with ./gradlew clean openApiMeta (couldn't find a mvn equivalent)
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

  • Download this file to resources/customjava/CustomApiClient.mustache and replace every instance of ApiClient (case sensitive) as shown

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

  • Package and install the project so it appears on mavenLocal repo as eg customjava-openapi-generator-1.0.0.jar .

  • Generate the code , in this case, with gradle.

Add the custom jar to the classpath and the openApiGenerate config to gradle.build. Should be similar for 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 command : /gradlew clean openApiGenerate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM