Update beanValidationCore.mustache (#13631) (fix #3519)

* Update beanValidationCore.mustache

Update to use x-pattern-message for message customization

* Update spring.md

Update this page adding documentation for x-pattern-message

* added unit test
meet requested corrections

* build the project

* remove space

---------

Co-authored-by: Rodrigo de Almeida - RMA3 <rodrigo.ma3@gmail.com>
Co-authored-by: Rodrigo Maciel de Almeida <rodrigo.almeida@wefin.com.br>
This commit is contained in:
Matheus Teles 2023-04-21 04:08:27 -03:00 committed by GitHub
parent 2679819694
commit 33e0c6775f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 1 deletions

View File

@ -120,6 +120,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null |x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false |x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
|x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null |x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD|null
## IMPORT MAPPING ## IMPORT MAPPING

View File

@ -113,6 +113,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null |x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false |x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
|x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null |x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD|null
## IMPORT MAPPING ## IMPORT MAPPING

View File

@ -16,6 +16,7 @@ public enum VendorExtension {
X_CLASS_EXTRA_ANNOTATION("x-class-extra-annotation", ExtensionLevel.MODEL, "List of custom annotations to be added to model", null), X_CLASS_EXTRA_ANNOTATION("x-class-extra-annotation", ExtensionLevel.MODEL, "List of custom annotations to be added to model", null),
X_FIELD_EXTRA_ANNOTATION("x-field-extra-annotation", ExtensionLevel.FIELD, "List of custom annotations to be added to property", null), X_FIELD_EXTRA_ANNOTATION("x-field-extra-annotation", ExtensionLevel.FIELD, "List of custom annotations to be added to property", null),
X_VERSION_PARAM("x-version-param", ExtensionLevel.OPERATION_PARAMETER, "Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false", null), X_VERSION_PARAM("x-version-param", ExtensionLevel.OPERATION_PARAMETER, "Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false", null),
X_PATTERN_MESSAGE("x-pattern-message", ExtensionLevel.FIELD, "Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable", null),
; ;
private final String name; private final String name;

View File

@ -1255,6 +1255,7 @@ public class SpringCodegen extends AbstractJavaCodegen
List<VendorExtension> extensions = super.getSupportedVendorExtensions(); List<VendorExtension> extensions = super.getSupportedVendorExtensions();
extensions.add(VendorExtension.X_SPRING_PAGINATED); extensions.add(VendorExtension.X_SPRING_PAGINATED);
extensions.add(VendorExtension.X_VERSION_PARAM); extensions.add(VendorExtension.X_VERSION_PARAM);
extensions.add(VendorExtension.X_PATTERN_MESSAGE);
return extensions; return extensions;
} }

View File

@ -1,4 +1,4 @@
{{#pattern}}{{^isByteArray}}@Pattern(regexp = "{{{pattern}}}") {{/isByteArray}}{{/pattern}}{{! {{#pattern}}{{^isByteArray}}@Pattern(regexp = "{{{pattern}}}"{{#vendorExtensions.x-pattern-message}}, message="{{vendorExtensions.x-pattern-message}}"{{/vendorExtensions.x-pattern-message}}) {{/isByteArray}}{{/pattern}}{{!
minLength && maxLength set minLength && maxLength set
}}{{#minLength}}{{#maxLength}}@Size(min = {{minLength}}, max = {{maxLength}}) {{/maxLength}}{{/minLength}}{{! }}{{#minLength}}{{#maxLength}}@Size(min = {{minLength}}, max = {{maxLength}}) {{/maxLength}}{{/minLength}}{{!
minLength set, maxLength not minLength set, maxLength not

View File

@ -2514,4 +2514,44 @@ public class SpringCodegenTest {
.doesNotContainsWithName("RestController"); .doesNotContainsWithName("RestController");
} }
@Test
public void testXPatternMessage_issue5857() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue5857.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime");
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
codegen.additionalProperties().put(USE_RESPONSE_ENTITY, "false");
codegen.additionalProperties().put(DELEGATE_PATTERN, "true");
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);
DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("ObjTest.java"));
javaFileAssert
.assertMethod("getField2")
.assertMethodAnnotations()
.containsWithNameAndAttributes("Pattern", ImmutableMap.of(
"regexp", "\"\\\\w\"",
"message", "\"Only letters, numbers and underscore\""
));
javaFileAssert
.assertMethod("getField3")
.assertMethodAnnotations()
.containsWithNameAndAttributes("Pattern", ImmutableMap.of(
"regexp", "\"\\\\w\""
));
}
} }

View File

@ -0,0 +1,45 @@
swagger: '2.0'
info:
description: 'blah'
version: 1.0.0
title: sample spec
host: fake.site.com
tags:
- name: Test
schemes:
- https
paths:
/test:
post:
summary: Post to test
description: ''
operationId: postToTest
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: Obj to test
schema:
$ref: '#/definitions/ObjTest'
responses:
'201':
description: successful operation
schema:
$ref: '#/definitions/ObjTest'
definitions:
ObjTest:
description: A model to return
type: object
properties:
field1:
type: integer
format: int64
field2:
type: string
pattern: "\\w"
x-pattern-message: "Only letters, numbers and underscore"
field3:
type: string
pattern: "\\w"