Skip to content

Commit c5b8cda

Browse files
committed
Java - fix @NotNull resolving from required/nullable
1 parent 1378751 commit c5b8cda

File tree

14 files changed

+121
-20
lines changed

14 files changed

+121
-20
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {
109109
public static final String DEFAULT_CONTENT_TYPE = "application/json";
110110
public static final String REQUEST_BODY_NAME = "body";
111111
public static final String DEFAULT_TEMPLATE_DIR = "handlebars";
112+
public static final String IS_NULLABLE_FALSE = "x-nullable-false";
113+
public static final String IS_NULLABLE_TRUE = "x-nullable-true";
112114

113115
protected OpenAPI openAPI;
114116
protected OpenAPI unflattenedOpenAPI;
@@ -1466,6 +1468,8 @@ else if (schema instanceof ComposedSchema) {
14661468
}
14671469
}
14681470
codegenModel.getVendorExtensions().put(CodegenConstants.IS_NULLABLE_EXT_NAME, Boolean.TRUE.equals(schema.getNullable()));
1471+
codegenModel.getVendorExtensions().put(IS_NULLABLE_FALSE, Boolean.FALSE.equals(schema.getNullable()));
1472+
codegenModel.getVendorExtensions().put(IS_NULLABLE_TRUE, Boolean.TRUE.equals(schema.getNullable()));
14691473

14701474
addVars(codegenModel, schema.getProperties(), schema.getRequired());
14711475
}
@@ -1613,6 +1617,8 @@ protected void setSchemaProperties(String name, CodegenProperty codegenProperty,
16131617
codegenProperty.jsonSchema = Json.pretty(schema);
16141618
codegenProperty.nullable = Boolean.TRUE.equals(schema.getNullable());
16151619
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_NULLABLE_EXT_NAME, Boolean.TRUE.equals(schema.getNullable()));
1620+
codegenProperty.getVendorExtensions().put(IS_NULLABLE_FALSE, Boolean.FALSE.equals(schema.getNullable()));
1621+
codegenProperty.getVendorExtensions().put(IS_NULLABLE_TRUE, Boolean.TRUE.equals(schema.getNullable()));
16161622
if (schema.getReadOnly() != null) {
16171623
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_READ_ONLY_EXT_NAME, schema.getReadOnly());
16181624
}

src/main/java/io/swagger/codegen/v3/generators/java/AbstractJavaCodegen.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
5858
public static final String SUPPORT_JAVA6 = "supportJava6";
5959
public static final String ERROR_ON_UNKNOWN_ENUM = "errorOnUnknownEnum";
6060
public static final String CHECK_DUPLICATED_MODEL_NAME = "checkDuplicatedModelName";
61+
public static final String USE_NULLABLE_FOR_NOTNULL = "useNullableForNotNull";
6162

6263
public static final String WIREMOCK_OPTION = "wiremock";
6364

@@ -96,6 +97,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
9697
protected boolean supportJava6= false;
9798
protected boolean jakarta = false;
9899
private NotNullAnnotationFeatures notNullOption;
100+
protected boolean useNullableForNotNull = true;
99101

100102
public AbstractJavaCodegen() {
101103
super();
@@ -205,6 +207,8 @@ public AbstractJavaCodegen() {
205207
jeeSpecModeOptions.put("false", "Use Java EE (javax.*)");
206208
jeeSpec.setEnum(jeeSpecModeOptions);
207209
cliOptions.add(jeeSpec);
210+
211+
cliOptions.add(CliOption.newBoolean(USE_NULLABLE_FOR_NOTNULL, "Add @NotNull depending on `nullable` property instead of `required`"));
208212
}
209213

210214
@Override
@@ -387,6 +391,11 @@ public void processOpts() {
387391
}
388392
}
389393

394+
if (additionalProperties.containsKey(USE_NULLABLE_FOR_NOTNULL)) {
395+
this.setUseNullableForNotnull(Boolean.valueOf(additionalProperties.get(USE_NULLABLE_FOR_NOTNULL).toString()));
396+
}
397+
writePropertyBack(USE_NULLABLE_FOR_NOTNULL, this.useNullableForNotNull);
398+
390399
if (fullJavaUtil) {
391400
javaUtilPrefix = "java.util.";
392401
}
@@ -1511,6 +1520,10 @@ public void setScmUrl(String scmUrl) {
15111520
this.scmUrl = scmUrl;
15121521
}
15131522

1523+
public void setUseNullableForNotnull(Boolean useNullableForNotNull) {
1524+
this.useNullableForNotNull = useNullableForNotNull;
1525+
}
1526+
15141527
public void setDeveloperName(String developerName) {
15151528
this.developerName = developerName;
15161529
}

src/main/resources/handlebars/Java/beanValidation.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}
47
{{#is this 'container'}}
58
{{#isNot this 'primitive-type'}}
69
{{#isNot this 'enum'}}

src/main/resources/handlebars/JavaJaxRS/beanValidation.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}
47
{{#is this 'container'}}
58
{{#isNot this 'primitive-type'}}
69
{{#isNot this 'enum'}}

src/main/resources/handlebars/JavaJaxRS/cxf-cdi/beanValidation.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}
47
{{#isContainer}}
58
{{^isPrimitiveType}}
69
{{^isEnum}}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}
47
{{>beanValidationCore}}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}
47
{{>beanValidationCore}}

src/main/resources/handlebars/JavaJaxRS/resteasy/eap/beanValidation.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}
47
{{#pattern}}
58
@Pattern(regexp="{{{pattern}}}")
69
{{/pattern}}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}
47
{{>beanValidationCore}}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
{{#required}}
1+
{{#required}}{{^useNullableForNotNull}}
22
@NotNull
3-
{{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
3+
{{/useNullableForNotNull}}{{/required}}
4+
{{#useNullableForNotNull}}{{^nullable}}
5+
@NotNull
6+
{{/nullable}}{{/useNullableForNotNull}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
47
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}}
58
@Valid{{/isPrimitiveType}}{{/isNotContainer}}
69
{{>beanValidationCore}}

0 commit comments

Comments
 (0)