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 -

  1. 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

  2. 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");

enter image description here

  1. Save the file and mvn clean install to generate the swagger-code-gen-cli-2.3.1 in target directory

  2. 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)

  3. Add the line "@JsonIgnoreProperties(ignoreUnknown = true)" below @ApiModel in the "pojo.mustache" file enter image description here

  4. 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

  5. This should build and generate pojo/ model classes with @JsonIgnoreProperties(ignoreUnknown = true) and with import com.fasterxml.jackson.annotation.JsonIgnoreProperties in the class.