fix issue 18959 (#18962)

Co-authored-by: Rodrigo Maciel de Almeida <rodrigo.almeida@wefin.com.br>
This commit is contained in:
Rodrigo de Almeida - RMA3 2024-06-24 05:58:02 -03:00 committed by GitHub
parent 861e8f0656
commit 5532203f9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 125 additions and 3 deletions

View File

@ -124,7 +124,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|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-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
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD, OPERATION_PARAMETER|null
## IMPORT MAPPING

View File

@ -117,7 +117,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|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-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
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD, OPERATION_PARAMETER|null
## IMPORT MAPPING

View File

@ -5121,6 +5121,9 @@ public class DefaultCodegen implements CodegenConfig {
if (parameter.getExtensions() != null && !parameter.getExtensions().isEmpty()) {
codegenParameter.vendorExtensions.putAll(parameter.getExtensions());
}
if (parameter.getSchema() != null && parameter.getSchema().getExtensions() != null && !parameter.getSchema().getExtensions().isEmpty()) {
codegenParameter.vendorExtensions.putAll(parameter.getSchema().getExtensions());
}
Schema parameterSchema;

View File

@ -2,6 +2,7 @@ package org.openapitools.codegen;
import lombok.Getter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -21,7 +22,7 @@ import java.util.List;
X_FIELD_EXTRA_ANNOTATION("x-field-extra-annotation", ExtensionLevel.FIELD, "List of custom annotations to be added to property", null),
X_OPERATION_EXTRA_ANNOTATION("x-operation-extra-annotation", ExtensionLevel.OPERATION, "List of custom annotations to be added to operation", 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),
X_PATTERN_MESSAGE("x-pattern-message", Arrays.asList(ExtensionLevel.FIELD, ExtensionLevel.OPERATION_PARAMETER), "Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable", null),
;
private final String name;

View File

@ -3148,6 +3148,61 @@ public class SpringCodegenTest {
));
}
@Test
public void testXPatternMessage_issue18959() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_18959.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");
codegen.additionalProperties().put(USE_BEANVALIDATION, "true");
codegen.additionalProperties().put(PERFORM_BEANVALIDATION, "true");
codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface");
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);
DefaultGenerator generator = new DefaultGenerator();
generator.setGenerateMetadata(false); // skip metadata generation
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApi.java"));
javaFileAssert
.assertMethod("_postToTest")
.assertParameter("groupObj")
.assertParameterAnnotations()
.containsWithNameAndAttributes("Pattern", ImmutableMap.of(
"regexp", "\"[a-zA-Z]\"",
"message", "\"Only letters\""
))
.toParameter()
.toMethod()
.assertParameter("token")
.assertParameterAnnotations()
.containsWithNameAndAttributes("Pattern", ImmutableMap.of(
"regexp", "\"[0-9a-fA-F]\"",
"message", "\"Only numbers and letters a-f\""
))
.toParameter()
.toMethod()
.assertParameter("clientId")
.assertParameterAnnotations()
.containsWithNameAndAttributes("Pattern", ImmutableMap.of(
"regexp", "\"\\\\d\"",
"message", "\"Only numbers\""
));
}
@Test
public void testEnumCaseInsensitive_issue8084() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

View File

@ -0,0 +1,63 @@
openapi: 3.0.3
info:
title: sample spec
version: 1.0.0
paths:
/test/{groupObj}:
post:
summary: Post to test
description: ''
operationId: postToTest
parameters:
- $ref: '#/components/parameters/groupObj'
- $ref: '#/components/parameters/token'
- $ref: '#/components/parameters/clientId'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ObjTest'
responses:
201:
description: success
components:
parameters:
groupObj:
in: path
name: groupObj
required: true
schema:
type: string
pattern: "[a-zA-Z]"
x-pattern-message: "Only letters"
token:
in: query
name: token
required: true
schema:
type: string
pattern: "[0-9a-fA-F]"
x-pattern-message: "Only numbers and letters a-f"
clientId:
in: header
name: clientId
required: true
schema:
type: string
pattern: "\\d"
x-pattern-message: "Only numbers"
schemas:
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"