Annotation needed from swagger codegen - 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

Related

How to manipulate pom.xml while creating project via archetype

I'm making an archetype via the goal create-from-project and a propertyFile for webservice projets which using jaxws. Therefore I have to enter the service endpoint implementation into the configuration of the jaxws plugin.
My POM-Snippet:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>wsgen</id>
<goals>
<goal>wsgen</goal>
</goals>
<configuration>
<sei>ws.ExampleWSBean</sei>
How can I manipulate the ws.ExampleWSBean?
I solved my problem. After the creation of the archetype project via the command:
mvn archetype:create-from-project -Darchetype.properties=../archetype.properties
I modified the pom.xml in the archetype-resources. I only replaced the ws.ExampleWSBean against ${package}.ExampleWSBean.

How to stop Apache maven <outputDirectory> to overwrite the folder

I have the following directory hierarchy:
generated
|
| -->java
Java directory has the following package: com.model
that contains java models that I copy/paste from somewhere before I compile the application.
The issue that I use Protocol buffer and I tell maven to output the generated files on same previous directory BUT over a new package:
Result : Protocol buffer generates the new package and deletes the old package.
I have no idea why does it do that although the package names are different?
Here is that part of pom I use to generate java from protocol buffer:
<plugin>
<groupId>com.google.protobuf.tools</groupId>
<artifactId>maven-protoc-plugin</artifactId>
<configuration>
<protocExecutable>C:\protoc.exe</protocExecutable>
<protoSourceRoot>./src/proto</protoSourceRoot>
<outputDirectory>./src/generated/java</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
if you look at the code for the plugin you'll see that the code has been hardcoded to clean the directory:
https://github.com/dtrott/maven-protoc-plugin/blob/master/src/main/java/com/google/protobuf/maven/AbstractProtocMojo.java#L154
// Quick fix to fix issues with two mvn installs in a row (ie no clean)
cleanDirectory(outputDirectory);
There's 2 ways to solve this..either set the output directory to a temp directory and then use the maven copy plugin or the maven build plugin to copy the files into the directory of your choice, or modify the maven plugin to remove that line (or better yet make it configurable).
Tommy
I have solved my issue by the following :
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete dir="./destination"/>
<copy todir="./destination">
<fileset dir="./source"/>
</copy>
<delete dir="./source"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
However , I get this error "An Ant BuildException has occured: Only one of tofile and todir may be set"

Maven: copying directories using exec plugin

I'm using Maven 3.0.3. I'm having trouble using the Maven exec plugin to copy the contents of one directory to another. Sadly, when I include this plugin in my pom.xml …
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<executable>cp</executable>
<arguments>
<argument>-r</argument>
<argument>web-app/*</argument>
<argument>src/main/webapp</argument>
</arguments>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
It isn't working. I get the error below …
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.1.1:exec (default-cli) on project jx: Result of /bin/sh -c cd /Users/davea/Documents/workspace/mycoUSA2/Technology/nna/myco2usa/jx && cp -r 'web-app/*' src/main/webapp execution is: '1'. -> [Help 1]
Does anyone know how I can modify my plugin config to copy the contents of one directory to another? Thanks, - Dave
If you are using bash, try the following:
<executable>bash</executable>
<arguments>
<argument>-c</argument>
<argument>cp -r web-app/* src/main/webapp</argument>
</arguments>
This spawns a new bash and gives it the command cp -r web-app/* src/main/webapp to execute.
You can also test if it works for you by inputting this into a normal Terminal window first:
bash -c "cp -r web-app/* src/main/webapp"
Note that the " signs do make a difference as the exec-maven-plugin does insert them automatically, thus they are not included in the <argument>-tag.
Note the command it ran. From the error output:
cp -r 'web-app/*' src/main/webapp
Note in particular the 'web-app/*' file it has tried to copy. Because it has quoted this argument the cp command is looking for a specific file with the name * in the web-app directory. Because you don't have a file with this name it has exited with the error code 1.
The maven-resources-plugin has a goal designed to perform this task. Why not give it a try? It would have the added benefit of making your build platform independent.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/web-app</outputDirectory>
<resources>
<resource>
<directory>web-app</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
mvn -X might be more revealing
Many people would use the maven-antrun-plugin and script this in ant so as to get a portable solution.

Get name of maven pom file

I am running a Maven (2) release build with with: mvn -f release.xml clean deploy
and want to get the currently running pom file name (release.xml) into a property or mojo.
Is it possible?
well you can use the property
${env.MAVEN_CMD_LINE_ARGS}
in your pom or a filtered resource, which will expand to something like
clean install -Paxis -Dmaven.test.skip -f mypom.xml -pl util
however, if you just want the mypom.xml part, you're going to have to do some scripting, which is not supported out of the box in maven. Common solutions are either maven antrun plugin or gmaven (groovy) plugin. Here's a way to do it in gmaven:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.out.println(
System
.getenv("MAVEN_CMD_LINE_ARGS")
.replaceFirst( /.*\-f\s+(\S+).*/ , 'POM File: $1')
);
</source>
</configuration>
</execution>
</executions>
</plugin>
Edit: as you want it in a property, either use System.setProperty or write directly to project.properties

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.