SpringCloud - Add a prefix for configuration files generated

未关闭
#1,059 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
java, spring
领域
api, backend

调研方向

从生成 ApiKeyRequestInterceptor 和 ClientConfiguration 的 spring-cloud 生成器模板开始,以 issue 中的 Maven 配置和生成的类名作为切入点。追踪对这些类的每个生成引用,然后验证可配置的前缀是否会重命名文件和使用位置,从而使多个客户端不再创建相互冲突的 clientConfiguration bean。

由索引模型根据 Issue 内容生成。

描述

When generating a Spring Boot client with

<plugin>
     <groupId>io.swagger.codegen.v3</groupId>
     <artifactId>swagger-codegen-maven-plugin</artifactId>
     <version>3.0.34</version>
     <executions>
         <execution>
             <goals>
                 <goal>generate</goal>
             </goals>
             <configuration>
                 <inputSpec>${project.basedir}/src/main/resources/myapi.yaml</inputSpec>
                 <output>${project.build.directory}/generated-sources/client</output>
                 <language>spring</language>
                 <library>spring-cloud</library>
                 <generateModels>true</generateModels>
                 <modelPackage>${clientBasePackage}.model</modelPackage>
                 <generateApis>true</generateApis>
                 <apiPackage>${clientBasePackage}.api</apiPackage>
                 <generateModelTests>false</generateModelTests>
                 <generateModelDocumentation>false</generateModelDocumentation>
                 <generateApiTests>false</generateApiTests>
                 <generateApiDocumentation>false</generateApiDocumentation>
                 <configOptions>
                     <configPackage>${clientBasePackage}.config</configPackage>
                     <dateLibrary>java11</dateLibrary>
                     <title>${api-name}</title>
                 </configOptions>
             </configuration>
         </execution>
     </executions>
 </plugin>

the generated files in the package config are always

├───config
│       ApiKeyRequestInterceptor.class
│       ClientConfiguration.class

It is a problem when I have multiple clients injected in the same Spring Boot application. In that case the application is not starting and complains about a conflict between beans with the same name (clientConfiguration).

Is it possible to add an option like MyApi allowing to have

├───config
│       MyApiApiKeyRequestInterceptor.class
│       MyApiClientConfiguration.class

And of course renaming all the usages within other generated files.

The only solution I found is to add

<plugin>
    <groupId>com.coderplus.maven.plugins</groupId>
    <artifactId>copy-rename-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution>
            <id>rename-ApiKeyRequestInterceptor</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>rename</goal>
            </goals>
            <configuration>
                <sourceFile>${project.build.directory}/generated-sources/client/src/main/java/.../config/ApiKeyRequestInterceptor.java</sourceFile>
                <destinationFile>${project.build.directory}/generated-sources/client/src/main/java/.../config/MyApiApiClientApiKeyRequestInterceptor.java</destinationFile>
            </configuration>
        </execution>
        <execution>
            <id>rename-ClientConfiguration</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>rename</goal>
            </goals>
            <configuration>
                <sourceFile>${project.build.directory}/generated-sources/client/src/main/java/.../config/ClientConfiguration.java</sourceFile>
                <destinationFile>${project.build.directory}/generated-sources/client/src/main/java/.../config/MyApiApiClientConfiguration.java</destinationFile>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <executions>
        <execution>
            <id>replace-for-ApiKeyRequestInterceptor</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <basedir>${project.build.directory}/generated-sources/client/src/main/java/.../config</basedir>
                <includes>
                    <include>**/*.java</include>
                </includes>
                <replacements>
                    <replacement>
                        <token>ApiKeyRequestInterceptor</token>
                        <value>MyApiApiClientApiKeyRequestInterceptor</value>
                    </replacement>
                </replacements>
            </configuration>
        </execution>
        <execution>
            <id>replace-for-ClientConfiguration</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <basedir>${project.build.directory}/generated-sources/client/src/main/java/...</basedir>
                <includes>
                    <include>**/*.java</include>
                </includes>
                <replacements>
                    <replacement>
                        <token>ClientConfiguration</token>
                        <value>MyApiApiClientConfiguration</value>
                    </replacement>
                </replacements>
            </configuration>
        </execution>
    </executions>
</plugin>

But it's really boring...

主要语言
Mustache
星标
299
派生
439
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

swagger-api/swagger-codegen-generators 的其他 Issue

查看 swagger-api/swagger-codegen-generators 的全部 Issue

相似的 Issue

更多 Backend & API Design Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。