Skip to content

Commit 0556e87

Browse files
committed
code review
1 parent 3c4b3fb commit 0556e87

File tree

8 files changed

+290
-65
lines changed

8 files changed

+290
-65
lines changed

springdoc-openapi-webflux-core/src/main/java/org/springdoc/webflux/api/OpenApiResource.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
import org.springframework.beans.factory.ObjectFactory;
5050
import org.springframework.context.ApplicationContext;
51-
import org.springframework.core.annotation.AnnotationUtils;
51+
import org.springframework.core.annotation.AnnotatedElementUtils;
5252
import org.springframework.http.server.reactive.ServerHttpRequest;
5353
import org.springframework.util.MimeType;
5454
import org.springframework.web.bind.annotation.RequestMethod;
@@ -185,15 +185,11 @@ protected void calculatePath(Map<String, Object> restControllers, Map<RequestMap
185185
String operationPath = pathPattern.getPatternString();
186186
Map<String, String> regexMap = new LinkedHashMap<>();
187187
operationPath = PathUtils.parsePath(operationPath, regexMap);
188-
String[] produces = requestMappingInfo.getProducesCondition().getProducibleMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
189-
String[] consumes = requestMappingInfo.getConsumesCondition().getConsumableMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
190-
String[] headers = requestMappingInfo.getHeadersCondition().getExpressions().stream().map(Object::toString).toArray(String[]::new);
191-
Operation operationAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Operation.class);
192-
// Added operationAnnotation condition and made it consistent with the one in webmvc module for now
193-
// Delete this message after review
188+
String[] produces = requestMappingInfo.getProducesCondition().getProducibleMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
189+
String[] consumes = requestMappingInfo.getConsumesCondition().getConsumableMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
190+
String[] headers = requestMappingInfo.getHeadersCondition().getExpressions().stream().map(Object::toString).toArray(String[]::new);
194191
if ((operationPath.startsWith(DEFAULT_PATH_SEPARATOR)
195-
&& (restControllers.containsKey(handlerMethod.getBean().toString()) || (isShowActuator()))
196-
|| operationAnnotation != null)
192+
&& (isRestController(restControllers,handlerMethod) || (isShowActuator())))
197193
&& isFilterCondition(handlerMethod, operationPath, produces, consumes, headers)) {
198194
Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
199195
// default allowed requestmethods
@@ -252,4 +248,15 @@ protected void calculateServerUrl(ServerHttpRequest serverHttpRequest, String ap
252248
*/
253249
protected abstract String getServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl);
254250

251+
/**
252+
* Is rest controller boolean.
253+
*
254+
* @param restControllers the rest controllers
255+
* @param handlerMethod the handler method
256+
* @return the boolean
257+
*/
258+
private boolean isRestController(Map<String, Object> restControllers, HandlerMethod handlerMethod) {
259+
boolean hasOperationAnnotation = AnnotatedElementUtils.hasAnnotation(handlerMethod.getMethod(), Operation.class);
260+
return hasOperationAnnotation || restControllers.containsKey(handlerMethod.getBean().toString());
261+
}
255262
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app149;
20+
21+
import io.swagger.v3.oas.annotations.Operation;
22+
import io.swagger.v3.oas.annotations.media.Content;
23+
import io.swagger.v3.oas.annotations.media.Schema;
24+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
25+
26+
import org.springframework.stereotype.Controller;
27+
import org.springframework.web.bind.annotation.GetMapping;
28+
import org.springframework.web.bind.annotation.ResponseBody;
29+
30+
/**
31+
* To test the case a user does not use @RestController but puts @Operation on handler methods
32+
* and wants these methods to be exposed.
33+
*
34+
* @author Azige
35+
*/
36+
@Controller
37+
public class HelloController {
38+
39+
@GetMapping("/hello")
40+
@Operation(responses = @ApiResponse(
41+
responseCode = "200",
42+
description = "OK",
43+
content = @Content(schema = @Schema(implementation = HelloMessage.class))
44+
))
45+
public String hello() {
46+
return "forward:/message";
47+
}
48+
49+
@GetMapping("/message")
50+
@Operation
51+
@ResponseBody
52+
public HelloMessage message() {
53+
return new HelloMessage("Lucky numbers!", 777);
54+
}
55+
56+
@GetMapping("/helloModelAndView")
57+
@Operation(responses = @ApiResponse(
58+
responseCode = "200",
59+
description = "OK",
60+
content = @Content(schema = @Schema(implementation = HelloMessage.class))
61+
))
62+
public HelloMessage helloModelAndView() {
63+
return null;
64+
}
65+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app149;
20+
21+
public class HelloMessage {
22+
public String text;
23+
public int number;
24+
25+
public HelloMessage(String text, int number) {
26+
this.text = text;
27+
this.number = number;
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app149;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.context.annotation.ComponentScan;
25+
26+
public class SpringDocApp149Test extends AbstractSpringDocTest {
27+
28+
@SpringBootApplication
29+
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app149" })
30+
static class SpringDocTestApp {}
31+
}

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app150/SpringDocApp149Test.java renamed to springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app150/SpringDocApp150Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.springframework.context.annotation.ComponentScan;
2828

2929
@AutoConfigureWebTestClient(timeout = "3600000")
30-
public class SpringDocApp149Test extends AbstractSpringDocTest {
30+
public class SpringDocApp150Test extends AbstractSpringDocTest {
3131

3232
@SpringBootApplication
3333
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app150" })

springdoc-openapi-webflux-core/src/test/resources/results/app149.json

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,66 +11,81 @@
1111
}
1212
],
1313
"paths": {
14-
"/users/test/greeter": {
14+
"/hello": {
1515
"get": {
16-
"operationId": "get-users",
17-
"responses": {}
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "hello",
20+
"responses": {
21+
"200": {
22+
"description": "OK",
23+
"content": {
24+
"*/*": {
25+
"schema": {
26+
"$ref": "#/components/schemas/HelloMessage"
27+
}
28+
}
29+
}
30+
}
31+
}
1832
}
1933
},
20-
"/users/test/greeter/greeter3/titi": {
21-
"post": {
22-
"operationId": "create-user-group-special",
23-
"responses": {}
24-
}
25-
},
26-
"/users/test/greeter/greeter3/toto": {
27-
"get": {
28-
"operationId": "get-user-groups",
29-
"responses": {}
30-
}
31-
},
32-
"/users/test/greeter/greeter4/titi": {
33-
"post": {
34-
"operationId": "create-user-group-special_1",
35-
"responses": {}
36-
}
37-
},
38-
"/users/test/greeter/greeter4/toto": {
39-
"get": {
40-
"operationId": "get-user-groups_1",
41-
"responses": {}
42-
}
43-
},
44-
"/users/test/greeter/groups/titi": {
45-
"post": {
46-
"operationId": "create-user-group-special_2",
47-
"responses": {}
48-
}
49-
},
50-
"/users/test/greeter/groups/toto": {
34+
"/message": {
5135
"get": {
52-
"operationId": "get-user-groups_2",
53-
"responses": {}
54-
}
55-
},
56-
"/users/test/greeter/groups2/titi": {
57-
"post": {
58-
"operationId": "create-user-group-special_3",
59-
"responses": {}
36+
"tags": [
37+
"hello-controller"
38+
],
39+
"operationId": "message",
40+
"responses": {
41+
"200": {
42+
"description": "OK",
43+
"content": {
44+
"*/*": {
45+
"schema": {
46+
"$ref": "#/components/schemas/HelloMessage"
47+
}
48+
}
49+
}
50+
}
51+
}
6052
}
6153
},
62-
"/users/test/greeter/groups2/toto": {
54+
"/helloModelAndView": {
6355
"get": {
64-
"operationId": "get-user-groups_3",
65-
"responses": {}
66-
}
67-
},
68-
"/users/test/greeter/special": {
69-
"post": {
70-
"operationId": "create-user-special",
71-
"responses": {}
56+
"tags": [
57+
"hello-controller"
58+
],
59+
"operationId": "helloModelAndView",
60+
"responses": {
61+
"200": {
62+
"description": "OK",
63+
"content": {
64+
"*/*": {
65+
"schema": {
66+
"$ref": "#/components/schemas/HelloMessage"
67+
}
68+
}
69+
}
70+
}
71+
}
7272
}
7373
}
7474
},
75-
"components": {}
75+
"components": {
76+
"schemas": {
77+
"HelloMessage": {
78+
"type": "object",
79+
"properties": {
80+
"number": {
81+
"type": "integer",
82+
"format": "int32"
83+
},
84+
"text": {
85+
"type": "string"
86+
}
87+
}
88+
}
89+
}
90+
}
7691
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/users/test/greeter": {
15+
"get": {
16+
"operationId": "get-users",
17+
"responses": {}
18+
}
19+
},
20+
"/users/test/greeter/greeter3/titi": {
21+
"post": {
22+
"operationId": "create-user-group-special",
23+
"responses": {}
24+
}
25+
},
26+
"/users/test/greeter/greeter3/toto": {
27+
"get": {
28+
"operationId": "get-user-groups",
29+
"responses": {}
30+
}
31+
},
32+
"/users/test/greeter/greeter4/titi": {
33+
"post": {
34+
"operationId": "create-user-group-special_1",
35+
"responses": {}
36+
}
37+
},
38+
"/users/test/greeter/greeter4/toto": {
39+
"get": {
40+
"operationId": "get-user-groups_1",
41+
"responses": {}
42+
}
43+
},
44+
"/users/test/greeter/groups/titi": {
45+
"post": {
46+
"operationId": "create-user-group-special_2",
47+
"responses": {}
48+
}
49+
},
50+
"/users/test/greeter/groups/toto": {
51+
"get": {
52+
"operationId": "get-user-groups_2",
53+
"responses": {}
54+
}
55+
},
56+
"/users/test/greeter/groups2/titi": {
57+
"post": {
58+
"operationId": "create-user-group-special_3",
59+
"responses": {}
60+
}
61+
},
62+
"/users/test/greeter/groups2/toto": {
63+
"get": {
64+
"operationId": "get-user-groups_3",
65+
"responses": {}
66+
}
67+
},
68+
"/users/test/greeter/special": {
69+
"post": {
70+
"operationId": "create-user-special",
71+
"responses": {}
72+
}
73+
}
74+
},
75+
"components": {}
76+
}

0 commit comments

Comments
 (0)