Fix #4786 Bean Validation for type:string, format: email will work for the SpringCodegen (#4788)

This commit is contained in:
Sascha Groß 2019-12-19 04:30:39 +01:00 committed by William Cheng
parent 10b60e27b1
commit 366bfd2a67
3 changed files with 74 additions and 2 deletions

View File

@ -11,8 +11,10 @@ minLength not set, maxLength set
}}{{#minItems}}{{^maxItems}}@Size(min={{minItems}}) {{/maxItems}}{{/minItems}}{{!
@Size: minItems not set && maxItems set
}}{{^minItems}}{{#maxItems}}@Size(max={{maxItems}}) {{/maxItems}}{{/minItems}}{{!
@Email: performBeanValidation set && isEmail set
}}{{#performBeanValidation}}{{#isEmail}}@Email{{/isEmail}}{{/performBeanValidation}}{{!
@Email: useBeanValidation set && isEmail && java8 set
}}{{#useBeanValidation}}{{#isEmail}}{{#java8}}@javax.validation.constraints.Email{{/java8}}{{/isEmail}}{{/useBeanValidation}}{{!
@Email: performBeanValidation set && isEmail && not java8 set
}}{{#performBeanValidation}}{{#isEmail}}{{^java8}}@org.hibernate.validator.constraints.Email{{/java8}}{{/isEmail}}{{/performBeanValidation}}{{!
check for integer or long / all others=decimal type with @Decimal*
isInteger set
}}{{#isInteger}}{{#minimum}}@Min({{minimum}}){{/minimum}}{{#maximum}} @Max({{maximum}}) {{/maximum}}{{/isInteger}}{{!

View File

@ -440,4 +440,45 @@ public class SpringCodegenTest {
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ExampleApi.java",
"@RequestBody(required = false");
}
@Test
public void useBeanValidationTruePerformBeanValidationTrueJava8FalseForFormatEmail() throws IOException {
beanValidationForFormatEmail(true, true, false, "@org.hibernate.validator.constraints.Email", "@javax.validation.constraints.Email");
}
@Test
public void useBeanValidationTruePerformBeanValidationFalseJava8TrueForFormatEmail() throws IOException {
beanValidationForFormatEmail(true, false, true, "@javax.validation.constraints.Email", "@org.hibernate.validator.constraints.Email");
}
@Test
public void useBeanValidationTruePerformBeanValidationTrueJava8TrueForFormatEmail() throws IOException {
beanValidationForFormatEmail(true, true, true, "@javax.validation.constraints.Email", "@org.hibernate.validator.constraints.Email");
}
private void beanValidationForFormatEmail(boolean useBeanValidation, boolean performBeanValidation, boolean java8, String contains, String notContains) throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_4876_format_email.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.setUseBeanValidation(useBeanValidation);
codegen.setPerformBeanValidation(performBeanValidation);
codegen.setJava8(java8);
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);
MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/model/PersonWithEmail.java", contains);
checkFileNotContains(generator, outputPath + "/src/main/java/org/openapitools/model/PersonWithEmail.java", notContains);
}
}

View File

@ -0,0 +1,29 @@
openapi: 3.0.1
info:
version: 1.0.0
title: EmailExample
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/person:
put:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/personWithEmail'
responses:
'204':
description: No Content
components:
schemas:
personWithEmail:
properties:
name:
type: string
maxLength: 255
email:
type: string
format: email