forked from loafle/openapi-generator-original
[Java][Spring] Fix #13365 Adjusted pojo.mustache to handle required fields when beanValidation false (#13580)
* Adjusted pojo.mustache to handle required fields * Updated tests and config to cover more scenarios * Removing unused imports * Resolved conflicts
This commit is contained in:
parent
4ee0cfa1ad
commit
0060dfd195
@ -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}}
|
||||
|
@ -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<String, File> 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<String, File> 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<String, File> 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();
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user