首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改"APIClient“名称

如何更改"APIClient“名称
EN

Stack Overflow用户
提问于 2021-06-04 22:03:04
回答 1查看 3.7K关注 0票数 1

我正在尝试从swagger-codegen-maven-plugin (版本3.0.8)和(组Id: io.swagger.codegen.v3)自动生成类,如下所示。但是,代码生成工作非常好,但是我希望将生成的ApiClient的名称更改为类似于PREFIX+ApiClient (例如: customApiClient,其中自定义为前缀)。

代码语言:javascript
复制
<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规范中指定的apis,没有任何问题。它还生成ApiClient.java,它在所有API类中都是自动生成的。我想更改ApiClient.java的名称以添加前缀或后缀。

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

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

EN

回答 1

Stack Overflow用户

发布于 2021-06-05 05:52:03

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

  • 创建具有此结构的项目(名称由您决定)。可以使用./gradlew clean openApiMeta创建框架(无法找到等效的mvn )。
代码语言:javascript
复制
src/
└── main
    ├── java
    │   └── org
    │       └── openapitools
    │           └── codegen
    │               └── CustomJavaGenerator.java
    └── resources
        ├── customjava
        │   └── CustomApiClient.mustache
        └── META-INF
            └── services
                └── org.openapitools.codegen.CodegenConfig
  • CustomJavaGenerator.java
代码语言:javascript
复制
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

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

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

  • 包并安装项目,因此它出现在mavenLocal回购上,如customjava-openapi-generator-1.0.0.jar
  • 在本例中,使用gradle生成代码。

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67844463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档