Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load vendorExtensions in the operation parameter #18962

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/generators/java-camel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5121,6 +5121,9 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
if (parameter.getExtensions() != null && !parameter.getExtensions().isEmpty()) {
codegenParameter.vendorExtensions.putAll(parameter.getExtensions());
}
if (parameter.getSchema() != null && parameter.getSchema().getExtensions() != null && !parameter.getSchema().getExtensions().isEmpty()) {
rodrigoma3 marked this conversation as resolved.
Show resolved Hide resolved
codegenParameter.vendorExtensions.putAll(parameter.getSchema().getExtensions());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @OpenAPITools/generator-core-team


Schema parameterSchema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Getter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -21,7 +22,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,61 @@ public void testXPatternMessage_issue5857() throws IOException {
));
}

@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();
Expand Down
63 changes: 63 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_18959.yaml
Original file line number Diff line number Diff line change
@@ -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"
Loading