diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache index 6305be42a15..e5f9c6136c5 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache @@ -146,6 +146,9 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}} {{#useBeanValidation}} {{>beanValidation}} {{/useBeanValidation}} + {{^useBeanValidation}} + {{#required}}@NotNull{{/required}} + {{/useBeanValidation}} {{#swagger2AnnotationLibrary}} @Schema(name = "{{{baseName}}}", {{#isReadOnly}}accessMode = Schema.AccessMode.READ_ONLY, {{/isReadOnly}}{{#example}}example = "{{{.}}}", {{/example}}{{#description}}description = "{{{.}}}", {{/description}}required = {{{required}}}) {{/swagger2AnnotationLibrary}} 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 a2d23d1e28e..ac1f12a3eb0 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 @@ -38,8 +38,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; @@ -1562,6 +1561,82 @@ public class SpringCodegenTest { } @Test + public void requiredFieldShouldIncludeNotNullAnnotation_issue13365() throws IOException { + + SpringCodegen codegen = new SpringCodegen(); + codegen.setLibrary(SPRING_BOOT); + codegen.additionalProperties().put(SpringCodegen.INTERFACE_ONLY, "true"); + codegen.additionalProperties().put(SpringCodegen.USE_BEANVALIDATION, "false"); + codegen.additionalProperties().put(SpringCodegen.PERFORM_BEANVALIDATION, "false"); + codegen.additionalProperties().put(SpringCodegen.OPENAPI_NULLABLE, "false"); + codegen.additionalProperties().put(SpringCodegen.UNHANDLED_EXCEPTION_HANDLING, "false"); + codegen.additionalProperties().put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, "false"); + codegen.additionalProperties().put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, "false"); + codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson"); + codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, "PascalCase"); + codegen.additionalProperties().put(SpringCodegen.USE_TAGS, "true"); + + DefaultGenerator generator = new DefaultGenerator(); + Map files = generateFiles(codegen, "src/test/resources/bugs/issue_13365.yml"); + + //Assert that NotNull annotation exists alone with no other BeanValidation annotations + JavaFileAssert.assertThat(files.get("Person.java")) + .printFileContent().assertMethod("getName").assertMethodAnnotations() + .containsWithName("NotNull").anyMatch(annotation -> + !annotation.getNameAsString().equals("Valid") || + !annotation.getNameAsString().equals("Pattern") || + !annotation.getNameAsString().equals("Email") || + !annotation.getNameAsString().equals("Size")); + + } + + @Test + public void nonRequiredFieldShouldNotIncludeNotNullAnnotation_issue13365() throws IOException { + + SpringCodegen codegen = new SpringCodegen(); + codegen.setLibrary(SPRING_BOOT); + codegen.additionalProperties().put(SpringCodegen.INTERFACE_ONLY, "true"); + codegen.additionalProperties().put(SpringCodegen.USE_BEANVALIDATION, "false"); + codegen.additionalProperties().put(SpringCodegen.PERFORM_BEANVALIDATION, "false"); + codegen.additionalProperties().put(SpringCodegen.OPENAPI_NULLABLE, "false"); + codegen.additionalProperties().put(SpringCodegen.UNHANDLED_EXCEPTION_HANDLING, "false"); + codegen.additionalProperties().put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, "false"); + codegen.additionalProperties().put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, "false"); + codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson"); + codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, "PascalCase"); + codegen.additionalProperties().put(SpringCodegen.USE_TAGS, "true"); + + Map files = generateFiles(codegen, "src/test/resources/bugs/issue_13365.yml"); + + JavaFileAssert.assertThat(files.get("Alien.java")) + .printFileContent().assertMethod("getName") + .assertMethodAnnotations().anyMatch(annotation -> !annotation.getNameAsString().equals("NotNull")); + } + + @Test + public void requiredFieldShouldIncludeNotNullAnnotationWithBeanValidationTrue_issue13365() throws IOException { + + SpringCodegen codegen = new SpringCodegen(); + codegen.setLibrary(SPRING_BOOT); + codegen.additionalProperties().put(SpringCodegen.INTERFACE_ONLY, "true"); + codegen.additionalProperties().put(SpringCodegen.USE_BEANVALIDATION, "true"); + codegen.additionalProperties().put(SpringCodegen.PERFORM_BEANVALIDATION, "false"); + codegen.additionalProperties().put(SpringCodegen.OPENAPI_NULLABLE, "false"); + codegen.additionalProperties().put(SpringCodegen.UNHANDLED_EXCEPTION_HANDLING, "false"); + codegen.additionalProperties().put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, "false"); + codegen.additionalProperties().put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, "false"); + codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson"); + codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, "PascalCase"); + codegen.additionalProperties().put(SpringCodegen.USE_TAGS, "true"); + + Map files = generateFiles(codegen, "src/test/resources/bugs/issue_13365.yml"); + + JavaFileAssert.assertThat(files.get("Person.java")) + .printFileContent().assertMethod("getName").assertMethodAnnotations() + .containsWithName("NotNull").containsWithName("Size").containsWithName("Email"); + + } + public void shouldUseEqualsNullableForArrayWhenSetInConfig_issue13385() throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_13365.yml b/modules/openapi-generator/src/test/resources/bugs/issue_13365.yml new file mode 100644 index 00000000000..020e1d8c998 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_13365.yml @@ -0,0 +1,40 @@ +openapi: 3.0.0 +info: + description: Specification to reproduce nullable issue with Array + title: Required Api +paths: + '/person': + post: + summary: Inserts a person + operationId: postPerson + tags: + - person + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/Person' + + responses: ... + +components: + schemas: + Person: + type: object + required: + - name + properties: + name: + type: string + maxLength: 50 + format: email + id: + type: integer + Alien: + type: object + properties: + name: + type: string + id: + type: integer \ No newline at end of file