|
9 | 9 | import io.swagger.codegen.v3.CodegenParameter;
|
10 | 10 | import io.swagger.codegen.v3.CodegenProperty;
|
11 | 11 | import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
|
| 12 | +import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures; |
12 | 13 | import io.swagger.codegen.v3.generators.handlebars.java.JavaHelper;
|
13 | 14 | import io.swagger.v3.oas.models.OpenAPI;
|
14 | 15 | import io.swagger.v3.oas.models.Operation;
|
|
40 | 41 |
|
41 | 42 | import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
|
42 | 43 | import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
|
| 44 | +import static io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION; |
43 | 45 | import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
|
44 | 46 |
|
45 | 47 | public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
|
@@ -81,6 +83,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
|
81 | 83 | protected String apiDocPath = "docs/";
|
82 | 84 | protected String modelDocPath = "docs/";
|
83 | 85 | protected boolean supportJava6= false;
|
| 86 | + private NotNullAnnotationFeatures notNullOption; |
84 | 87 |
|
85 | 88 | public AbstractJavaCodegen() {
|
86 | 89 | super();
|
@@ -150,7 +153,9 @@ public AbstractJavaCodegen() {
|
150 | 153 | cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC));
|
151 | 154 | cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));
|
152 | 155 | cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_OAS2, CodegenConstants.USE_OAS2_DESC));
|
153 |
| - |
| 156 | + if(this instanceof NotNullAnnotationFeatures){ |
| 157 | + cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes")); |
| 158 | + } |
154 | 159 | CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
|
155 | 160 | Map<String, String> dateOptions = new HashMap<String, String>();
|
156 | 161 | dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
|
@@ -328,6 +333,17 @@ public void processOpts() {
|
328 | 333 | this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
|
329 | 334 | }
|
330 | 335 |
|
| 336 | + if (this instanceof NotNullAnnotationFeatures) { |
| 337 | + notNullOption = (NotNullAnnotationFeatures)this; |
| 338 | + if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) { |
| 339 | + notNullOption.setNotNullJacksonAnnotation(convertPropertyToBoolean(NOT_NULL_JACKSON_ANNOTATION)); |
| 340 | + writePropertyBack(NOT_NULL_JACKSON_ANNOTATION, notNullOption.isNotNullJacksonAnnotation()); |
| 341 | + if (notNullOption.isNotNullJacksonAnnotation()) { |
| 342 | + importMapping.put("JsonInclude", "com.fasterxml.jackson.annotation.JsonInclude"); |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + |
331 | 347 | if (fullJavaUtil) {
|
332 | 348 | javaUtilPrefix = "java.util.";
|
333 | 349 | }
|
@@ -912,6 +928,16 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
|
912 | 928 | final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel, allSchemas);
|
913 | 929 | codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
|
914 | 930 | }
|
| 931 | + if (this instanceof NotNullAnnotationFeatures) { |
| 932 | + if (this instanceof NotNullAnnotationFeatures) { |
| 933 | + notNullOption = (NotNullAnnotationFeatures)this; |
| 934 | + if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) { |
| 935 | + if (notNullOption.isNotNullJacksonAnnotation()) { |
| 936 | + codegenModel.imports.add("JsonInclude"); |
| 937 | + } |
| 938 | + } |
| 939 | + } |
| 940 | + } |
915 | 941 | return codegenModel;
|
916 | 942 | }
|
917 | 943 |
|
|
0 commit comments