Skip to content

Commit

Permalink
ones with defaults shouldn't be annotated as @nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodator authored and aslobodyanyk-wio committed Dec 19, 2024
1 parent d23a2d2 commit 91afcfb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{^required}}{{^useOptional}}{{#openApiNullable}}{{^isNullable}}@Nullable {{/isNullable}}{{/openApiNullable}}{{^openApiNullable}}@Nullable {{/openApiNullable}}{{/useOptional}}{{/required}}
{{^required}}{{^defaultValue}}{{^useOptional}}{{#openApiNullable}}{{^isNullable}}@Nullable {{/isNullable}}{{/openApiNullable}}{{^openApiNullable}}@Nullable {{/openApiNullable}}{{/useOptional}}{{/defaultValue}}{{#defaultValue}}{{^openApiNullable}}{{#isNullable}}@Nullable {{/isNullable}}{{/openApiNullable}}{{/defaultValue}}{{/required}}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> photoUrls = new ArrayList<>();");

}
Expand Down Expand Up @@ -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<String> stringList")
.fileContains("private @Nullable List<String> stringDefaultList = new ArrayList<>(Arrays.asList(\"A\", \"B\"));")
.fileContains("private @Nullable List<String> stringEmptyDefaultList = new ArrayList<>();")
.fileContains("private List<String> stringDefaultList = new ArrayList<>(Arrays.asList(\"A\", \"B\"));")
.fileContains("private List<String> stringEmptyDefaultList = new ArrayList<>();")
.fileContains("@Nullable Set<String> stringSet")
.fileContains("private @Nullable Set<String> stringDefaultSet = new LinkedHashSet<>(Arrays.asList(\"A\", \"B\"));")
.fileContains("private @Nullable Set<String> stringEmptyDefaultSet = new LinkedHashSet<>();")
.fileDoesNotContain("List<@Valid TagDto> tags = new ArrayList<>()")
.fileDoesNotContain("Set<@Valid TagDto> tagsUnique = new LinkedHashSet<>()")
.fileDoesNotContain("List<String> stringList = new ArrayList<>()")
.fileDoesNotContain("Set<String> stringSet = new LinkedHashSet<>()");
.fileContains("private Set<String> stringDefaultSet = new LinkedHashSet<>(Arrays.asList(\"A\", \"B\"));")
.fileContains("private Set<String> stringEmptyDefaultSet = new LinkedHashSet<>();")
.fileDoesNotContain("private List<@Valid TagDto> tags = new ArrayList<>()")
.fileDoesNotContain("private Set<@Valid TagDto> tagsUnique = new LinkedHashSet<>()")
.fileDoesNotContain("private List<String> stringList = new ArrayList<>()")
.fileDoesNotContain("private Set<String> stringSet = new LinkedHashSet<>()");
}

@Test
Expand Down Expand Up @@ -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");
Expand All @@ -5134,13 +5137,16 @@ 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");
}

@Test
public void shouldNotAnnotateNonRequiredFieldsAsNullableWhileNotUsingOpenApiNullable() throws IOException {
public void shouldAnnotateNonRequiredFieldsAsNullableWhileNotUsingOpenApiNullable() throws IOException {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.setOpenApiNullable(false);
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ components:
type: String
optionalDescription:
type: string
optionalOneWithDefault:
type: string
default: "someDefaultValue"
nullableStr:
type: String
nullable: true

0 comments on commit 91afcfb

Please sign in to comment.