Swagger Codegen Maven Plugin - Custom ClientConfiguration name - spring-boot

I'm using the Swagger Codegen Maven Plugin (v3) to generate spring-cloud client from yaml file with openapi 3 definition.
My plugin is configured like that
<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>
Among the generated files there are 2 files for the authentification
├───config
│ ApiKeyRequestInterceptor.class
│ ClientConfiguration.class
The problem is that I don't know how to customize the name of those files.
I need to do that because I have multiple clients to generate. So when I'm injecting the clients into my Spring Boot application it tells me there is a conflict between beans with the same name.
I'd like to have files like MyFirstClientApiKeyInterceptor.class MyFirstClientConfiguration.class and MySecondClientApiKeyInterceptor.class MySecondClientConfiguration.class
Or is there a solution for leaving it like that and being able to inject the clients without the error ?

Related

swagger maven plgin executions do not run independant

I use the latest swagger-maven-plugin from the io.swagger.core.v3 to generate my static swagger api documentation.
In my project, I have to separate apis so I want to get a json and yml representation for each api within one package process.
<plugin>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>2.2.6</version>
<configuration>
<outputPath>${basedir}/target/</outputPath>
<outputFormat>JSONANDYAML</outputFormat>
<prettyPrint>true</prettyPrint>
</configuration>
<executions>
<execution>
<id>1</id>
<goals>
<goal>resolve</goal>
</goals>
<configuration>
<resourcePackages>
<resourcePackage>de.test.rest</resourcePackage>
</resourcePackages>
<outputFileName>swagger</outputFileName>
<configurationFilePath>${basedir}/src/main/resources/openApiConfig.yml</configurationFilePath>
</configuration>
</execution>
<execution>
<id>2</id>
<goals>
<goal>resolve</goal>
</goals>
<configuration>
<resourcePackages>
<resourcePackage>de.test.secondAPI</resourcePackage>
</resourcePackages>
<outputFileName>secondAPI</outputFileName>
<configurationFilePath>${basedir}/src/main/resources/secondOpenApiConfig.yml</configurationFilePath>
</configuration>
</execution>
</executions>
</plugin>
PROBLEM:
the execution creates the expected json and yml files for each execution
swagger.yml
swagger.json
secondAPI.yml
secondAPI.json
The problem is, that the seconAPI files are a copy of the swagger files.
I've read the documentation and I thought that configuration in the plugin root is shared between multiple executions. Configurations within the execution tag are individually used per execution.
Is there a way to run the executions in parallel with individual configuration?
Or is it a problem with the plugin itself?
EDIT:
Each execution works as expected when there is only one execution defined in the executions tag.

Spring Boot: OpenApi Generator Plugin creates unwanted test file

I use OpenApi 3.0 and the maven plugin openapi-generator-maven-plugin to generate my api + objects.
This is my maven config:
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/BookingService.yaml</inputSpec>
<generatorName>spring</generatorName>
<modelPackage>${clientPackage}.model</modelPackage>
<invokerPackage>${clientPackage}.invoker</invokerPackage>
<apiPackage>${clientPackage}.api</apiPackage>
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<configOptions>
<delegatePattern>true</delegatePattern>
</configOptions>
</configuration>
</execution>
It works as expected, however its also generating tests that I do not want. As you can see in my config i disabled the tests for Api tests + Model tests..
The compilation of these tests fail bc it "Cannot resolve symbol 'SpringBootTest'" in the build target folder...
These tests do not have any sense, how can I disable them?
Note this is a workaround. It would be better to get a property to not generate this testcase, but for now this seems to work...
Add this maven plugin to your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- remove the unwanted generated testcases by the spring generator of openapi -->
<delete dir="${project.build.directory}/generated-sources/openapi/src/test" />
</target>
</configuration>
</execution>
</executions>
</plugin>
It will delete that entire test package after the sources have been generated in the process-sources phase
We can skip the generation by enabling <interfaceOnly>true</interfaceOnly> but delegatePattern will not consider.
Here is the source code from openapi-generator...
Source Code
if (!interfaceOnly) {
if (SPRING_BOOT.equals(library)) {
if (useSwaggerUI && selectedDocumentationProviderRequiresSwaggerUiBootstrap()) {
supportingFiles.add(new SupportingFile("swagger-ui.mustache", "src/main/resources/static", "swagger-ui.html"));
}
// rename template to SpringBootApplication.mustache
supportingFiles.add(new SupportingFile("openapi2SpringBoot.mustache",
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
"OpenApiGeneratorApplication.java"));
supportingFiles.add(new SupportingFile("SpringBootTest.mustache",
(testFolder + File.separator + basePackage).replace(".", java.io.File.separator),
"OpenApiGeneratorApplicationTests.java"));
supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache",
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
"RFC3339DateFormat.java"));
}
....
You can always exclude the generated test package in Intellij IDE for local compilation issue.

Generate API and Data Model java files from Open API Generator with maven plugin

I am using maven plugin of openapi generator (not swagger code generator) but the java files are not getting generated as yaml file has $ref relative paths.
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.1.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
C:/Users/xxxxx/Documents/Docs/Project/xxxxyy/workspace1/MnS-Rel-16/MnS-Rel-16/OpenAPI/provMnS.yaml
</inputSpec>
<generatorName>spring</generatorName>
<modelPackage>com.xxx.xxx.dto.etsi.moi</modelPackage>
</configuration>
</execution>
</executions>
</plugin>
My yaml file has $ref in many places. Example below.
schema:
$ref: 'comDefs.yaml#/components/schemas/ErrorResponse'
Getting below exception:
[WARNING] Exception while resolving:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring (String.java:1967)
at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema (ExternalRefProcessor.java:113)
at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefSchema (ExternalRefProcessor.java:921)
at io.swagger.v3.parser.processors.ExternalRefProcessor.processSchema (ExternalRefProcessor.java:200)
So how to generate the server code (spring) with Open API Generator with yaml file having $ref?

How to fetch Swagger definition from a password-protected remote server in Maven

Rather than copy a Swagger definition from another project, I want to fetch the current version direct from Gitlab. I thought that I might be able to link to the server settings in settings.xml but that doesn't seem to work.
How do I pass the server credentials to Maven in order to access the yaml? Or is this not the right way to do it?
<execution>
<goals>
<goal>java</goal>
</goals>
<id>generate-swagger-attrstore</id>
<phase>generate-sources</phase>
<configuration>
<server>gitlab</server>
<mainClass>io.swagger.codegen.SwaggerCodegen</mainClass>
<includePluginDependencies>true</includePluginDependencies>
<arguments>
<argument>generate</argument>
<argument>-l</argument>
<argument>java</argument>
<argument>-c</argument>
<argument>${swagger.dir}/attrstore-codegen-config.json</argument>
<argument>-i</argument>
<argument>${swagger.attrstore}</argument>
<argument>-o</argument>
<argument>${basedir}</argument>
<argument>--library</argument>
<argument>okhttp-gson</argument>
</arguments>
</configuration>
</execution>

jaxb2-maven-plugin with external XSD dependencies

We have a common set of XSDs (datatypes, vocabulary, etc.) we're generating with the jaxb2-maven-plugin in its own Maven project. In a second project, I need to refer to one or more of those XSDs at compile time but don't want them included in the resulting artifact. I've created a catalog file, which works fine except I get everything in it.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<target>2.1</target>
<catalog>catalog.cat</catalog>
</configuration>
I've pored over the plugin docs, but they're woefully light on detail. Is there any way to get reuse out of common schemas without every project having to take a copy of them?
Thanks
This is something my maven-jaxb2-plugin can do:
Compiling schema from Maven Artifact
The documentation site is currently very unstable so here's snippets of the documentation.
<configuration>
<forceRegenerate>true</forceRegenerate>
<schemas>
<schema>
<dependencyResource>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin-tests-po</artifactId>
<!-- Can be defined in project dependencies or dependency management -->
<version>${project.version}</version>
<resource>purchaseorder.xsd</resource>
</dependencyResource>
</schema>
</schemas>
</configuration>
Here's a sample project.

Resources