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 f20f77cafae4..82b92f0082e7 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 @@ -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 mandatoryContainer," + + " List optionalContainer," + + " List optionalContainerWithDefault," + + " List nullableContainer)" + ); + } + + @Test + public void shouldAnnotateNonRequiredFieldsAsNullableWhenSetContainerDefaultToNull() throws IOException { + SpringCodegen codegen = new SpringCodegen(); + codegen.setLibrary(SPRING_BOOT); + codegen.setGenerateConstructorWithAllArgs(true); + codegen.setContainerDefaultToNull(true); + + Map 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 mandatoryContainer," + + " @Nullable List optionalContainer," + + " List optionalContainerWithDefault," + + " List nullableContainer)" ); } @@ -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 files = generateFiles(codegen, "src/test/resources/3_0/nullable-annotation.yaml"); var file = files.get("Item.java"); @@ -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 mandatoryContainer," + + " @Nullable List optionalContainer," + + " List optionalContainerWithDefault," + + " @Nullable List nullableContainer)" ); } } 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 9d41e2b65c8b..1de632345f0e 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 @@ -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