Skip to content

Commit

Permalink
Java - fix @NotNull resolving from required/nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed May 27, 2024
1 parent 1378751 commit 569e074
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {
public static final String DEFAULT_CONTENT_TYPE = "application/json";
public static final String REQUEST_BODY_NAME = "body";
public static final String DEFAULT_TEMPLATE_DIR = "handlebars";
public static final String IS_NULLABLE_FALSE = "x-nullable-false";
public static final String IS_NULLABLE_TRUE = "x-nullable-true";

protected OpenAPI openAPI;
protected OpenAPI unflattenedOpenAPI;
Expand Down Expand Up @@ -1466,6 +1468,8 @@ else if (schema instanceof ComposedSchema) {
}
}
codegenModel.getVendorExtensions().put(CodegenConstants.IS_NULLABLE_EXT_NAME, Boolean.TRUE.equals(schema.getNullable()));
codegenModel.getVendorExtensions().put(IS_NULLABLE_FALSE, Boolean.FALSE.equals(schema.getNullable()));
codegenModel.getVendorExtensions().put(IS_NULLABLE_TRUE, Boolean.TRUE.equals(schema.getNullable()));

addVars(codegenModel, schema.getProperties(), schema.getRequired());
}
Expand Down Expand Up @@ -1613,6 +1617,8 @@ protected void setSchemaProperties(String name, CodegenProperty codegenProperty,
codegenProperty.jsonSchema = Json.pretty(schema);
codegenProperty.nullable = Boolean.TRUE.equals(schema.getNullable());
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_NULLABLE_EXT_NAME, Boolean.TRUE.equals(schema.getNullable()));
codegenProperty.getVendorExtensions().put(IS_NULLABLE_FALSE, Boolean.FALSE.equals(schema.getNullable()));
codegenProperty.getVendorExtensions().put(IS_NULLABLE_TRUE, Boolean.TRUE.equals(schema.getNullable()));
if (schema.getReadOnly() != null) {
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_READ_ONLY_EXT_NAME, schema.getReadOnly());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
public static final String SUPPORT_JAVA6 = "supportJava6";
public static final String ERROR_ON_UNKNOWN_ENUM = "errorOnUnknownEnum";
public static final String CHECK_DUPLICATED_MODEL_NAME = "checkDuplicatedModelName";
public static final String USE_NULLABLE_FOR_NOTNULL = "useNullableForNotNull";

public static final String WIREMOCK_OPTION = "wiremock";

Expand Down Expand Up @@ -96,6 +97,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
protected boolean supportJava6= false;
protected boolean jakarta = false;
private NotNullAnnotationFeatures notNullOption;
protected boolean useNullableForNotNull = true;

public AbstractJavaCodegen() {
super();
Expand Down Expand Up @@ -205,6 +207,8 @@ public AbstractJavaCodegen() {
jeeSpecModeOptions.put("false", "Use Java EE (javax.*)");
jeeSpec.setEnum(jeeSpecModeOptions);
cliOptions.add(jeeSpec);

cliOptions.add(CliOption.newBoolean(USE_NULLABLE_FOR_NOTNULL, "Add @NotNull depending on `nullable` property instead of `required`"));
}

@Override
Expand Down Expand Up @@ -387,6 +391,11 @@ public void processOpts() {
}
}

if (additionalProperties.containsKey(USE_NULLABLE_FOR_NOTNULL)) {
this.setUseNullableForNotnull(Boolean.valueOf(additionalProperties.get(USE_NULLABLE_FOR_NOTNULL).toString()));
}
writePropertyBack(USE_NULLABLE_FOR_NOTNULL, this.useNullableForNotNull);

if (fullJavaUtil) {
javaUtilPrefix = "java.util.";
}
Expand Down Expand Up @@ -1511,6 +1520,10 @@ public void setScmUrl(String scmUrl) {
this.scmUrl = scmUrl;
}

public void setUseNullableForNotnull(Boolean useNullableForNotNull) {
this.useNullableForNotNull = useNullableForNotNull;
}

public void setDeveloperName(String developerName) {
this.developerName = developerName;
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/handlebars/Java/beanValidation.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}
{{#is this 'container'}}
{{#isNot this 'primitive-type'}}
{{#isNot this 'enum'}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}
{{#is this 'container'}}
{{#isNot this 'primitive-type'}}
{{#isNot this 'enum'}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}
{{#isContainer}}
{{^isPrimitiveType}}
{{^isEnum}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}
{{#pattern}}
@Pattern(regexp="{{{pattern}}}")
{{/pattern}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}}
@Valid{{/isPrimitiveType}}{{/isNotContainer}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}}
@Valid{{/isPrimitiveType}}{{/isNotContainer}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#required}}
{{#required}}{{^useNullableForNotNull}}
@NotNull
{{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
{{/useNullableForNotNull}}{{/required}}
{{#useNullableForNotNull}}{{^nullable}}
@NotNull
{{/nullable}}{{/useNullableForNotNull}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}}
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}}
@Valid{{/isPrimitiveType}}{{/isNotContainer}}
{{>beanValidationCore}}
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,24 @@ public void testJava8Javax() throws Exception {
folder.delete();
}

@Test(description = "verify that schema with required and nullable=true is generated correctly")
public void testNullableTrue() throws Exception {
final TemporaryFolder folder = new TemporaryFolder();
folder.create();
final File output = folder.getRoot();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setLang("spring")
.setInputSpecURL("src/test/resources/3_0_0/cc32744.yaml")
.setOutputDir(output.getAbsolutePath());

// configurator.addAdditionalProperty("useNullableForNotNull", true); // DEFAULT
final ClientOptInput clientOptInput = configurator.toClientOptInput();
new DefaultGenerator().opts(clientOptInput).generate();

final File file = new File(output, "src/main/java/io/swagger/model/NullableRoot.java");
final String content = FileUtils.readFileToString(file);
Assert.assertFalse(content.contains("@NotNull"));
folder.delete();
}
}
32 changes: 32 additions & 0 deletions src/test/resources/3_0_0/cc32744.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: 3.0.3
info:
title: Title
version: "2.0.0"
paths:
'/test':
put:
description: description
operationId: updateTest
responses:
200:
description: Successfully updated
content:
application/json:
schema:
$ref: '#/components/schemas/NullableRoot'
components:
schemas:
NullableRoot:
type: object
required:
- name
properties:
name:
$ref: '#/components/schemas/NullableRef'
NullableRef:
type: string
nullable: true
description: Nullable string
minLength: 0
maxLength: 100
example: 'example'

0 comments on commit 569e074

Please sign in to comment.