diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/nullableAnnotation.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/nullableAnnotation.mustache index 352250582e44c..de4ee36cfcae5 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/nullableAnnotation.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/nullableAnnotation.mustache @@ -1 +1 @@ -{{^required}}{{^useOptional}}{{#openApiNullable}}{{^isNullable}}@Nullable {{/isNullable}}{{/openApiNullable}}{{^openApiNullable}}@Nullable {{/openApiNullable}}{{/useOptional}}{{/required}} \ No newline at end of file +{{^required}}{{^defaultValue}}{{^useOptional}}{{#openApiNullable}}{{^isNullable}}@Nullable {{/isNullable}}{{/openApiNullable}}{{^openApiNullable}}@Nullable {{/openApiNullable}}{{/useOptional}}{{/defaultValue}}{{#defaultValue}}{{^openApiNullable}}{{#isNullable}}@Nullable {{/isNullable}}{{/openApiNullable}}{{/defaultValue}}{{/required}} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index cd1215179c839..fb6f53a1a3542 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -4859,7 +4859,7 @@ public void optionalListShouldBeEmpty() throws IOException { .collect(Collectors.toMap(File::getName, Function.identity())); JavaFileAssert.assertThat(files.get("PetDto.java")) - .fileContains("private @Nullable List<@Valid TagDto> tags = new ArrayList<>();") + .fileContains("private List<@Valid TagDto> tags = new ArrayList<>();") .fileContains("private List photoUrls = new ArrayList<>();"); } @@ -4894,19 +4894,19 @@ public void testCollectionTypesWithDefaults_issue_18102() throws IOException { JavaFileAssert.assertThat(files.get("PetDto.java")) .fileContains("private @Nullable List<@Valid TagDto> tags") - .fileContains("private @Nullable List<@Valid TagDto> tagsDefaultList = new ArrayList<>()") + .fileContains("private List<@Valid TagDto> tagsDefaultList = new ArrayList<>()") .fileContains("private @Nullable Set<@Valid TagDto> tagsUnique") - .fileContains("private @Nullable Set<@Valid TagDto> tagsDefaultSet = new LinkedHashSet<>();") + .fileContains("private Set<@Valid TagDto> tagsDefaultSet = new LinkedHashSet<>();") .fileContains("private @Nullable List stringList") - .fileContains("private @Nullable List stringDefaultList = new ArrayList<>(Arrays.asList(\"A\", \"B\"));") - .fileContains("private @Nullable List stringEmptyDefaultList = new ArrayList<>();") + .fileContains("private List stringDefaultList = new ArrayList<>(Arrays.asList(\"A\", \"B\"));") + .fileContains("private List stringEmptyDefaultList = new ArrayList<>();") .fileContains("@Nullable Set stringSet") - .fileContains("private @Nullable Set stringDefaultSet = new LinkedHashSet<>(Arrays.asList(\"A\", \"B\"));") - .fileContains("private @Nullable Set stringEmptyDefaultSet = new LinkedHashSet<>();") - .fileDoesNotContain("List<@Valid TagDto> tags = new ArrayList<>()") - .fileDoesNotContain("Set<@Valid TagDto> tagsUnique = new LinkedHashSet<>()") - .fileDoesNotContain("List stringList = new ArrayList<>()") - .fileDoesNotContain("Set stringSet = new LinkedHashSet<>()"); + .fileContains("private Set stringDefaultSet = new LinkedHashSet<>(Arrays.asList(\"A\", \"B\"));") + .fileContains("private Set stringEmptyDefaultSet = new LinkedHashSet<>();") + .fileDoesNotContain("private List<@Valid TagDto> tags = new ArrayList<>()") + .fileDoesNotContain("private Set<@Valid TagDto> tagsUnique = new LinkedHashSet<>()") + .fileDoesNotContain("private List stringList = new ArrayList<>()") + .fileDoesNotContain("private Set stringSet = new LinkedHashSet<>()"); } @Test @@ -5114,6 +5114,9 @@ public void shouldAnnotateNonRequiredFieldsAsNullable() throws IOException { JavaFileAssert.assertThat(file) .assertProperty("optionalDescription") .hasAnnotation("Nullable"); + JavaFileAssert.assertThat(file) + .assertProperty("optionalOneWithDefault") + .doesNotHaveAnnotation("Nullable"); JavaFileAssert.assertThat(file) .assertProperty("nullableStr") .doesNotHaveAnnotation("Nullable"); @@ -5134,6 +5137,9 @@ public void shouldNotAnnotateNonRequiredFieldsAsNullableWhileUseOptional() throw JavaFileAssert.assertThat(file) .assertProperty("optionalDescription") .doesNotHaveAnnotation("Nullable"); + JavaFileAssert.assertThat(file) + .assertProperty("optionalOneWithDefault") + .doesNotHaveAnnotation("Nullable"); JavaFileAssert.assertThat(file) .assertProperty("nullableStr") .doesNotHaveAnnotation("Nullable"); @@ -5154,6 +5160,9 @@ public void shouldNotAnnotateNonRequiredFieldsAsNullableWhileNotUsingOpenApiNull JavaFileAssert.assertThat(file) .assertProperty("optionalDescription") .hasAnnotation("Nullable"); + JavaFileAssert.assertThat(file) + .assertProperty("optionalOneWithDefault") + .doesNotHaveAnnotation("Nullable"); JavaFileAssert.assertThat(file) .assertProperty("nullableStr") .hasAnnotation("Nullable"); diff --git a/modules/openapi-generator/src/test/resources/3_0/nullable-annotation.yaml b/modules/openapi-generator/src/test/resources/3_0/nullable-annotation.yaml index 8af42a7b072c5..9d41e2b65c8b4 100644 --- a/modules/openapi-generator/src/test/resources/3_0/nullable-annotation.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/nullable-annotation.yaml @@ -10,6 +10,9 @@ components: type: String optionalDescription: type: string + optionalOneWithDefault: + type: string + default: "someDefaultValue" nullableStr: type: String nullable: true