how to ignore api or property in swagger codegen - swagger-codegen

We need the possibility to add some annotation/extension, on API level and on parameter level, which marks the swagger to ignore it and not generate it (not to generate in the java outputs).
Any ideas how can it done?
paths:
/productOffering:
get:
galit-ignore:
tags:
- "ProductOffering"
description: |
This service retrieves product offering details .
operationId: "getProductOfferingsDetails"
parameters:
- name: "productOfferingId"
in: "query"….
I want that the swagger won't generate the productOffering.
Thanks, Galit

You could try using the <generateApis>false</generateApis> in your configuration for code-gen. Take a look at the snippet below
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${swagger-codegen-maven-plugin.version}</version>
<executions>
<execution>
<id>codegen</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generateApis>false</generateApis>
<!-- other config here -->
</configuration>
</execution>
</executions>
</plugin>

The best way I can think is to preprocess the OpenAPI/Swagger spec using a parser such as Swagger Parser and remove those operations, properties before passing it to Swagger Codegen.

Related

springdoc-openapi-maven-plugin not generating output file when used with spring.webflux.base-path

I have a maven Spring Boot project that uses Webflux. I generate the open api spec into an output file using springdoc-openapi-maven-plugin in my pom:
<plugin>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<apiDocsUrl>http://localhost:8080/v3/api-docs</apiDocsUrl>
<outputFileName>openapi.json</outputFileName>
<outputDir>${project.basedir}/target</outputDir>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
I also added the springdoc-openapi-webflux-ui dependency in my pom:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webflux-ui</artifactId>
<version>1.4.8</version>
</dependency>
The above setup was working all fine and a new openapi.json file was generated each time I compiled.
However, I had a need to add a prefix for all my API paths. So I ended up adding a spring.webflux.base-path property in my application.yml file, which worked as expected.
spring:
webflux:
base-path: "/myproject"
Once I did this, open api was not able to generate the output file (openapi.json) anymore. Is this expected? Is there a way to debug open api related errors?

Annotation needed from swagger codegen

I need a way to annotate my openapi 3 specification so that swagger-codegen will add an annotation to my java class, such as #JsonIgnoreProperties(ignoreUnknown = true)
Is that possible?
TIA!
It seems like you could take advantage of mustache templates. Extract mustache files for required language from code-gen jar file and edit the needed class template file. Then, generate client code with -t pathToTemplates flag like so:
java -jar swagger-codegen-cli-2.3.1.jar generate
-t C:\SwaggerTemplates
-i C:\Swagger.json
-l csharp
-o C:\Output
Yes, it is possible.
Here are the steps that worked for me -
Download the swagger-code-gen source code either from github or get the zip. I have version 2.3.1 and downloaded from swagger-code-gen 2.3.1
For Java, update AbstractJavaCodegen.java file and add the 2 lines in processOpts() method:
importMapping.put("JsonIgnoreProperties","com.fasterxml.jackson.annotation.JsonIgnoreProperties");
// import JsonIgnoreProperties if ApiModel is imported importMapping.put("io.swagger.annotations.ApiModel","com.fasterxml.jackson.annotation.JsonIgnoreProperties");
Save the file and mvn clean install to generate the swagger-code-gen-cli-2.3.1 in target directory
Now extract the "pojo.mustache" and any other required file from the above cli jar (located in this path - "swagger-codegen-2.3.1\modules\swagger-codegen-cli\target\swagger-codegen-cli-2.3.1.jar\Java\") to a directory (e.g. spring_template)
Add the line "#JsonIgnoreProperties(ignoreUnknown = true)" below #ApiModel in the "pojo.mustache" file
Now execute the command with the built cli jar and spring_template in path:
java -jar swagger-codegen-cli-2.3.1.jar generate --additional-properties apiPackage=com.xxx.xxx.api,modelPackage=com.xxx.xxx.model,delegatePattern=true,useTags=true,configPackage=com.xxx.xxx.configuration,basePackage=com.xxx.xxx -o app -l spring -i your_swagger_yaml_file.yaml -t spring_template
This should build and generate pojo/ model classes with #JsonIgnoreProperties(ignoreUnknown = true) and with import com.fasterxml.jackson.annotation.JsonIgnoreProperties in the class.
maven has plugin for swagger
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<executions>
<execution>
<id>java-codegen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<language>jaxrs-spec</language>
<templateDirectory>${project.build.directory}/swagger-templates</templateDirectory>
<inputSpec>${basedir}/src/main/resources/v1/swagger.yaml</inputSpec>
<output>${project.build.directory}/swagger-codegen</output>
<apiPackage>com.example.api.v1</apiPackage>
<modelPackage>com.example.api.v1.dto</modelPackage>
<modelNameSuffix>DTO</modelNameSuffix>
<configOptions>
<additional-properties>generateModelBuilders=true,useJackson=true,sortParamsByRequiredFlag=false,useJacksonJsonIgnoreUnknownProperties=true</additional-properties>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
if we are using swagger-codegen-maven-plugin to generate code then
<additional-properties>generateModelBuilders=true,useJackson=true,sortParamsByRequiredFlag=false,useJacksonJsonIgnoreUnknownProperties=true</additional-properties>
will generate DTO with #JsonIgnoreProperties(ignoreUnknown = true)
Using swagger-codegen v2.4.20 or higher, you can achieve it adding the following configuration to your pom.xml
<additional-properties>ignoreUnknownJacksonAnnotation=true</additional-properties>
Example
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.20</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<language>java</language>
<inputSpec>${sourceFile}</inputSpec>
<output>${outputFolder}</output>
<modelPackage>${model.package}</modelPackage>
<generateApis>false</generateApis>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<configOptions>
<additional-properties>ignoreUnknownJacksonAnnotation=true</additional-properties>
<sourceFolder>.</sourceFolder>
<java8>true</java8>
<dateLibrary>legacy</dateLibrary>
<serializableModel>true</serializableModel>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
The other solutions didn't work for me for whatever reason, but this did:
<configuration>
<configOptions>
<additionalModelTypeAnnotations>#com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>
</configOptions>
</configuration>
Credit to this thread: https://github.com/OpenAPITools/openapi-generator/issues/3438

Swagger Codegen use existing class

How can I get the swagger codegen to use an existing class instead of creating a new class? Is this possible? For instance I want to use org.springframework.data.domain.Page instead of swagger creating another page class.
You could use --import-mappings, as it is explained here:
Sometimes you don't want a model generated. In this case, you can
simply specify an import mapping to tell the codegen what not to
create. When doing this, every location that references a specific
model will refer back to your classes.
You call this on swagger-codegen-cli generate, with the example you included it would be
--import-mappings Page=org.springframework.data.domain.Page
Although importMappings hasn't been included in the general configuration parameters here if you look at the code here you can see it's a List<String>.
I haven't used it with the maven plugin but looking at the doc and the code I'm guessing this should work:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2-SNAPSHOT</version>
<executions>
<execution>
...
<configuration>
...
<importMappings>
<importMapping>Page=org.springframework.data.domain.Page</importMapping>
</importMappings>
</configuration>
</execution>
</executions>
</plugin>
But this was recently changed, so it might be different if you're using an older version of the plugin. Before that changed it seems to be like this:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2-SNAPSHOT</version>
<executions>
<execution>
...
<configuration>
...
<configOptions>
<import-mappings>Page=org.springframework.data.domain.Page;Some=org.example.Some</import-mappings>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
According to the comment in that commit the old version should be supported too, but I haven't tried any of this so let me know if it works.
None of the answers mentioned here talked about what to add to the swagger yaml file,
in case someone is interested this is something that worked for me:
DisplayProperty:
type: object
properties:
name:
type: string
displayName:
$ref: '#/components/schemas/Text'
isRequired:
type: boolean
example: false
Text:
type: object
and then have this in the pom
<importMappings>
<importMapping>Text=com.--.--.--.--.Text</importMapping>
It's not always possible to use --import-mappings if you have long list of mappings.
(At least in case of Windows, which has size-limit for string in command prompt.)
That is why better way do it: use mapping with swagger configuration file.
(And this option is not fully documented.)
Like that:
java -jar swagger-codegen-cli-2.3.1.jar generate -i myspec.yaml -l java -c myconfig.json
myconfig.json:
{
"hideGenerationTimestamp": true,
"dateLibrary": "java8",
"useRuntimeException": true,
"modelPackage": "org.my.package.model",
"apiPackage": "org.my.package.api",
"importMappings": {
"Page": "org.springframework.data.domain.Page",
"MySuperType": "org.my.SuperType"
}
}

How to display dependency conflicts in 'mvn site'

I can easily see if there are conflicts between (transitive) dependency versions using:
mvn dependency:tree -Dverbose=true
... this will show the full resolution tree, including which elements were omitted (for duplicate or conflict or whatever). What I would like to do is to add the full tree to the 'mvn site' report.
Currently, the site report includes the dependency tree but only as resolved, i.e., without any conflicts. I see in the project-info-reports plugin that there is not currently any way to do what I want using the standard report.
I tried adding a section to the pom to include the maven-dependency-plugin 'tree' goal with the outputFile specified, but it wasn't included when I ran 'mvn site'. It was something like this:
<reporting>
....
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<reportSets>
<reportSet>
<id>deptree</id>
<reports>
<report>tree</report>
</reports>
<configuration>
<verbose>true</verbose>
<outputFile>${project.reporting.outputDirectory}/deptree.txt</outputFile>
</configuration>
Of course, the 'tree' goal is explicitly identified as not a report, but I was hoping to at least be able to produce a file that I could link to from the generated site. No dice.
Is there any way to force an arbitrary plugin's goal to execute during site generation? Am I totally out of luck here? Obviously I could write my own reporting plugin to do this, and/or submit a patch for the project-info-reports plugin, but I want to make sure I've exhausted all the built-in maven options.
(I'm using maven 2.1.0, but I didn't see anything about a change to this functionality in the release notes for later versions.)
Is there any way to force an arbitrary plugin's goal to execute during site generation? Am I totally out of luck here?
Just to answer your question, you can bind a mojo to the pre-site phase of the Site Lifecycle:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>tree</id>
<phase>pre-site</phase>
<goals>
<goal>tree</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
...
</reporting>
If you then run mvn site, dependency:tree will run.

Maven maven-exec-plugin multiple execution configurations

Is it possible to invoke a maven-exec-plugin (or any other plugin's) execution by its id from the command line?
Let's say my pom.xml file looks like this:
<project>
[...]
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>foo</id>
<goals>
<goal>exec</goal>
</goals>
<phase></phase>
<configuration>
<executable>echo</executable>
<arguments>
<argument>foo</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>bar</id>
<goals>
<goal>exec</goal>
</goals>
<phase></phase>
<configuration>
<executable>echo</executable>
<arguments>
<argument>bar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
[...]
</project>
Now is it possible to call
mvn exec:exec
with some added magic to run execution "foo"?
For the curious there is an alternative solution using profiles available here:
http://www.mail-archive.com/user#mojo.codehaus.org/msg00151.html
It is now possible, starting from Maven 3.3.1: see improvement MNG-5768 and Maven 3.3.1 release notes
You can call a specific execution configuration with this syntax:
mvn exec:exec#foo
No, it's not possible. Executions are for binding to the lifecycle (i.e. they are not designed to be invoked on the command line). So you'll have to use the profile trick described in the link that you provided.
Not mentioned here is that, as of Maven 2.2.0, if you give an execution of any plugin the id "default-cli", then when you run that plugin from the command line, that configuration is used. You're limited to only one default execution of each plugin, but it's a start.
I think if you write execute the goal:
org.codehaus.mojo:exec-maven-plugin:¿Version?:exec
it worked for me in eclipse maven plugin.