Skip to content

Commit d955ff6

Browse files
rodrigoma3Rodrigo Maciel de Almeida
andauthored
Add annotations to the operation - case permission validation - Fix 12219 15822 (#16259)
* fix #12219 #15822 * add test with array * update samples --------- Co-authored-by: Rodrigo Maciel de Almeida <rodrigo.almeida@wefin.com.br>
1 parent 84d619b commit d955ff6

9 files changed

Lines changed: 272 additions & 0 deletions

File tree

docs/generators/java-camel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
119119
|x-content-type|Specify custom value for 'Content-Type' header for operation|OPERATION|null
120120
|x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null
121121
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null
122+
|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|null
122123
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
123124
|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
124125
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD|null

docs/generators/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
112112
|x-content-type|Specify custom value for 'Content-Type' header for operation|OPERATION|null
113113
|x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null
114114
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null
115+
|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|null
115116
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
116117
|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
117118
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD|null

modules/openapi-generator/src/main/java/org/openapitools/codegen/VendorExtension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public enum VendorExtension {
1515
X_CONTENT_TYPE("x-content-type", ExtensionLevel.OPERATION, "Specify custom value for 'Content-Type' header for operation", null),
1616
X_CLASS_EXTRA_ANNOTATION("x-class-extra-annotation", ExtensionLevel.MODEL, "List of custom annotations to be added to model", null),
1717
X_FIELD_EXTRA_ANNOTATION("x-field-extra-annotation", ExtensionLevel.FIELD, "List of custom annotations to be added to property", null),
18+
X_OPERATION_EXTRA_ANNOTATION("x-operation-extra-annotation", ExtensionLevel.OPERATION, "List of custom annotations to be added to operation", null),
1819
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),
1920
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),
2021
;

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,7 @@ public void setUseSwaggerUI(boolean useSwaggerUI) {
13041304
@Override
13051305
public List<VendorExtension> getSupportedVendorExtensions() {
13061306
List<VendorExtension> extensions = super.getSupportedVendorExtensions();
1307+
extensions.add(VendorExtension.X_OPERATION_EXTRA_ANNOTATION);
13071308
extensions.add(VendorExtension.X_SPRING_PAGINATED);
13081309
extensions.add(VendorExtension.X_VERSION_PARAM);
13091310
extensions.add(VendorExtension.X_PATTERN_MESSAGE);

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ public interface {{classname}} {
240240
{{^useResponseEntity}}
241241
@ResponseStatus({{#springHttpStatus}}{{#responses.0}}{{{code}}}{{/responses.0}}{{/springHttpStatus}})
242242
{{/useResponseEntity}}
243+
{{#vendorExtensions.x-operation-extra-annotation}}
244+
{{{.}}}
245+
{{/vendorExtensions.x-operation-extra-annotation}}
243246
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}{{#useResponseEntity}}ResponseEntity<{{/useResponseEntity}}{{>returnTypes}}{{#useResponseEntity}}>{{/useResponseEntity}}{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}(
244247
{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}},
245248
{{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}},

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,6 +2899,109 @@ public void testEnumCaseSensitive_issue8084() throws IOException {
28992899
}
29002900

29012901

2902+
@Test
2903+
public void testHasOperationExtraAnnotation_issue15822() throws IOException {
2904+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
2905+
output.deleteOnExit();
2906+
2907+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue15822.yaml");
2908+
final SpringCodegen codegen = new SpringCodegen();
2909+
codegen.setOpenAPI(openAPI);
2910+
codegen.setOutputDir(output.getAbsolutePath());
2911+
2912+
codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime");
2913+
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
2914+
codegen.additionalProperties().put(USE_RESPONSE_ENTITY, "false");
2915+
codegen.additionalProperties().put(DELEGATE_PATTERN, "true");
2916+
codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface");
2917+
codegen.additionalProperties().put(SPRING_CONTROLLER, "true");
2918+
2919+
ClientOptInput input = new ClientOptInput();
2920+
input.openAPI(openAPI);
2921+
input.config(codegen);
2922+
2923+
DefaultGenerator generator = new DefaultGenerator();
2924+
2925+
Map<String, File> files = generator.opts(input).generate().stream()
2926+
.collect(Collectors.toMap(File::getName, Function.identity()));
2927+
2928+
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApi.java"));
2929+
javaFileAssert
2930+
.assertMethod("_postToTest")
2931+
.assertMethodAnnotations()
2932+
.containsWithName("javax.annotation.security.RolesAllowed");
2933+
}
2934+
2935+
@Test
2936+
public void testHasOperationExtraAnnotation_issue12219() throws IOException {
2937+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
2938+
output.deleteOnExit();
2939+
2940+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue12219.yaml");
2941+
final SpringCodegen codegen = new SpringCodegen();
2942+
codegen.setOpenAPI(openAPI);
2943+
codegen.setOutputDir(output.getAbsolutePath());
2944+
2945+
codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime");
2946+
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
2947+
codegen.additionalProperties().put(USE_RESPONSE_ENTITY, "false");
2948+
codegen.additionalProperties().put(DELEGATE_PATTERN, "true");
2949+
codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface");
2950+
codegen.additionalProperties().put(SPRING_CONTROLLER, "true");
2951+
2952+
ClientOptInput input = new ClientOptInput();
2953+
input.openAPI(openAPI);
2954+
input.config(codegen);
2955+
2956+
DefaultGenerator generator = new DefaultGenerator();
2957+
2958+
Map<String, File> files = generator.opts(input).generate().stream()
2959+
.collect(Collectors.toMap(File::getName, Function.identity()));
2960+
2961+
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApi.java"));
2962+
javaFileAssert
2963+
.assertMethod("_postToTest")
2964+
.assertMethodAnnotations()
2965+
.containsWithName("javax.annotation.security.RolesAllowed")
2966+
.containsWithName("org.springframework.security.access.annotation.Secured")
2967+
.containsWithName("org.springframework.security.access.prepost.PreAuthorize");
2968+
}
2969+
2970+
@Test
2971+
public void testHasOperationExtraAnnotation_issue12219_array() throws IOException {
2972+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
2973+
output.deleteOnExit();
2974+
2975+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue12219_array.yaml");
2976+
final SpringCodegen codegen = new SpringCodegen();
2977+
codegen.setOpenAPI(openAPI);
2978+
codegen.setOutputDir(output.getAbsolutePath());
2979+
2980+
codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime");
2981+
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
2982+
codegen.additionalProperties().put(USE_RESPONSE_ENTITY, "false");
2983+
codegen.additionalProperties().put(DELEGATE_PATTERN, "true");
2984+
codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface");
2985+
codegen.additionalProperties().put(SPRING_CONTROLLER, "true");
2986+
2987+
ClientOptInput input = new ClientOptInput();
2988+
input.openAPI(openAPI);
2989+
input.config(codegen);
2990+
2991+
DefaultGenerator generator = new DefaultGenerator();
2992+
2993+
Map<String, File> files = generator.opts(input).generate().stream()
2994+
.collect(Collectors.toMap(File::getName, Function.identity()));
2995+
2996+
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApi.java"));
2997+
javaFileAssert
2998+
.assertMethod("_postToTest")
2999+
.assertMethodAnnotations()
3000+
.containsWithName("javax.annotation.security.RolesAllowed")
3001+
.containsWithName("org.springframework.security.access.annotation.Secured")
3002+
.containsWithName("org.springframework.security.access.prepost.PreAuthorize");
3003+
}
3004+
29023005

29033006
@Test
29043007
public void multiLineOperationDescription() throws IOException {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
swagger: '2.0'
2+
info:
3+
description: 'blah'
4+
version: 1.0.0
5+
title: sample spec
6+
host: fake.site.com
7+
tags:
8+
- name: Test
9+
schemes:
10+
- https
11+
paths:
12+
/test:
13+
post:
14+
summary: Post to test
15+
description: ''
16+
operationId: postToTest
17+
consumes:
18+
- application/json
19+
produces:
20+
- application/json
21+
parameters:
22+
- in: body
23+
name: Obj to test
24+
schema:
25+
$ref: '#/definitions/ObjTest'
26+
responses:
27+
'201':
28+
description: successful operation
29+
schema:
30+
$ref: '#/definitions/ObjTest'
31+
x-operation-extra-annotation: '@javax.annotation.security.RolesAllowed({"ROLE_TEST"}) @org.springframework.security.access.annotation.Secured({"ROLE_TEST"}) @org.springframework.security.access.prepost.PreAuthorize("hasRole(''ROLE_TEST'') and hasRole(''ROLE_TEST_TWO'')")'
32+
definitions:
33+
ObjTest:
34+
description: A model to return
35+
type: object
36+
properties:
37+
field1:
38+
type: integer
39+
format: int64
40+
field2:
41+
type: string
42+
pattern: "\\w"
43+
x-pattern-message: "Only letters, numbers and underscore"
44+
field3:
45+
type: string
46+
pattern: "\\w"
47+
EnumTest:
48+
title: An enum to test
49+
type: string
50+
enum:
51+
- ONE
52+
- Two
53+
- three
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
swagger: '2.0'
2+
info:
3+
description: 'blah'
4+
version: 1.0.0
5+
title: sample spec
6+
host: fake.site.com
7+
tags:
8+
- name: Test
9+
schemes:
10+
- https
11+
paths:
12+
/test:
13+
post:
14+
summary: Post to test
15+
description: ''
16+
operationId: postToTest
17+
consumes:
18+
- application/json
19+
produces:
20+
- application/json
21+
parameters:
22+
- in: body
23+
name: Obj to test
24+
schema:
25+
$ref: '#/definitions/ObjTest'
26+
responses:
27+
'201':
28+
description: successful operation
29+
schema:
30+
$ref: '#/definitions/ObjTest'
31+
x-operation-extra-annotation:
32+
- '@javax.annotation.security.RolesAllowed({"ROLE_TEST"})'
33+
- '@org.springframework.security.access.annotation.Secured({"ROLE_TEST"})'
34+
- '@org.springframework.security.access.prepost.PreAuthorize("hasRole(''ROLE_TEST'') and hasRole(''ROLE_TEST_TWO'')")'
35+
definitions:
36+
ObjTest:
37+
description: A model to return
38+
type: object
39+
properties:
40+
field1:
41+
type: integer
42+
format: int64
43+
field2:
44+
type: string
45+
pattern: "\\w"
46+
x-pattern-message: "Only letters, numbers and underscore"
47+
field3:
48+
type: string
49+
pattern: "\\w"
50+
EnumTest:
51+
title: An enum to test
52+
type: string
53+
enum:
54+
- ONE
55+
- Two
56+
- three
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
swagger: '2.0'
2+
info:
3+
description: 'blah'
4+
version: 1.0.0
5+
title: sample spec
6+
host: fake.site.com
7+
tags:
8+
- name: Test
9+
schemes:
10+
- https
11+
paths:
12+
/test:
13+
post:
14+
summary: Post to test
15+
description: ''
16+
operationId: postToTest
17+
consumes:
18+
- application/json
19+
produces:
20+
- application/json
21+
parameters:
22+
- in: body
23+
name: Obj to test
24+
schema:
25+
$ref: '#/definitions/ObjTest'
26+
responses:
27+
'201':
28+
description: successful operation
29+
schema:
30+
$ref: '#/definitions/ObjTest'
31+
x-operation-extra-annotation: '@javax.annotation.security.RolesAllowed({"ROLE_TEST"})'
32+
definitions:
33+
ObjTest:
34+
description: A model to return
35+
type: object
36+
properties:
37+
field1:
38+
type: integer
39+
format: int64
40+
field2:
41+
type: string
42+
pattern: "\\w"
43+
x-pattern-message: "Only letters, numbers and underscore"
44+
field3:
45+
type: string
46+
pattern: "\\w"
47+
EnumTest:
48+
title: An enum to test
49+
type: string
50+
enum:
51+
- ONE
52+
- Two
53+
- three

0 commit comments

Comments
 (0)