Skip to content

Commit

Permalink
adds container tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodator authored and aslobodyanyk-wio committed Jan 6, 2025
1 parent f959312 commit 7de6e07
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5121,10 +5121,60 @@ public void shouldAnnotateNonRequiredFieldsAsNullable() throws IOException {
JavaFileAssert.assertThat(file)
.assertProperty("nullableStr")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("mandatoryContainer")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("optionalContainer")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("optionalContainerWithDefault")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("nullableContainer")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.fileContains(
"public Item(" +
"String mandatoryName," +
" @Nullable String optionalDescription," +
" String optionalOneWithDefault," +
" String nullableStr," +
" List<String> mandatoryContainer," +
" List<String> optionalContainer," +
" List<String> optionalContainerWithDefault," +
" List<String> nullableContainer)"
);
}

@Test
public void shouldAnnotateNonRequiredFieldsAsNullableWhenSetContainerDefaultToNull() throws IOException {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.setGenerateConstructorWithAllArgs(true);
codegen.setContainerDefaultToNull(true);

Map<String, File> files = generateFiles(codegen, "src/test/resources/3_0/nullable-annotation.yaml");
var file = files.get("Item.java");

JavaFileAssert.assertThat(file)
.assertProperty("mandatoryContainer")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("optionalContainer")
.hasAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("optionalContainerWithDefault")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("nullableContainer")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.fileContains(
"public Item(String mandatoryName, @Nullable String optionalDescription," +
" String optionalOneWithDefault, String nullableStr)"
", List<String> mandatoryContainer," +
" @Nullable List<String> optionalContainer," +
" List<String> optionalContainerWithDefault," +
" List<String> nullableContainer)"
);
}

Expand Down Expand Up @@ -5153,16 +5203,17 @@ public void shouldNotAnnotateNonRequiredFieldsAsNullableWhileUseOptional() throw
JavaFileAssert.assertThat(file)
.fileContains(
"public Item(String mandatoryName, String optionalDescription," +
" String optionalOneWithDefault, String nullableStr)"
" String optionalOneWithDefault, String nullableStr"
);
}

@Test
public void shouldAnnotateNonRequiredFieldsAsNullableWhileNotUsingOpenApiNullable() throws IOException {
public void shouldAnnotateNonRequiredFieldsAsNullableWhileNotUsingOpenApiNullableAndContainerDefaultToNullSet() throws IOException {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.setGenerateConstructorWithAllArgs(true);
codegen.setOpenApiNullable(false);
codegen.setContainerDefaultToNull(true);

Map<String, File> files = generateFiles(codegen, "src/test/resources/3_0/nullable-annotation.yaml");
var file = files.get("Item.java");
Expand All @@ -5179,10 +5230,25 @@ public void shouldAnnotateNonRequiredFieldsAsNullableWhileNotUsingOpenApiNullabl
JavaFileAssert.assertThat(file)
.assertProperty("nullableStr")
.hasAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("mandatoryContainer")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("optionalContainer")
.hasAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("optionalContainerWithDefault")
.doesNotHaveAnnotation("Nullable");
JavaFileAssert.assertThat(file)
.assertProperty("nullableContainer")
.hasAnnotation("Nullable");

JavaFileAssert.assertThat(file)
.fileContains(
"public Item(String mandatoryName, @Nullable String optionalDescription," +
" String optionalOneWithDefault, @Nullable String nullableStr)"
" List<String> mandatoryContainer," +
" @Nullable List<String> optionalContainer," +
" List<String> optionalContainerWithDefault," +
" @Nullable List<String> nullableContainer)"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,33 @@ components:
type: object
required:
- mandatoryName
- mandatoryContainer
properties:
mandatoryName:
type: String
type: string
optionalDescription:
type: string
optionalOneWithDefault:
type: string
default: "someDefaultValue"
nullableStr:
type: String
type: string
nullable: true
mandatoryContainer:
type: array
items:
type: string
optionalContainer:
type: array
items:
type: string
optionalContainerWithDefault:
type: array
items:
type: string
default: [ ]
nullableContainer:
type: array
items:
type: string
nullable: true

0 comments on commit 7de6e07

Please sign in to comment.