Skip to content

Commit 5e31edb

Browse files
committed
Merge branch 'azige-master'
2 parents 98db8f2 + 0556e87 commit 5e31edb

File tree

12 files changed

+513
-60
lines changed

12 files changed

+513
-60
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.fasterxml.jackson.core.JsonProcessingException;
3434
import io.swagger.v3.core.util.PathUtils;
35+
import io.swagger.v3.oas.annotations.Operation;
3536
import io.swagger.v3.oas.models.OpenAPI;
3637
import org.springdoc.api.AbstractOpenApiResource;
3738
import org.springdoc.core.AbstractRequestService;
@@ -47,6 +48,7 @@
4748

4849
import org.springframework.beans.factory.ObjectFactory;
4950
import org.springframework.context.ApplicationContext;
51+
import org.springframework.core.annotation.AnnotatedElementUtils;
5052
import org.springframework.http.server.reactive.ServerHttpRequest;
5153
import org.springframework.util.MimeType;
5254
import org.springframework.web.bind.annotation.RequestMethod;
@@ -63,7 +65,7 @@
6365

6466
/**
6567
* The type Open api resource.
66-
* @author bnasslahsen
68+
* @author bnasslahsen, Azige
6769
*/
6870
public abstract class OpenApiResource extends AbstractOpenApiResource {
6971

@@ -183,11 +185,11 @@ protected void calculatePath(Map<String, Object> restControllers, Map<RequestMap
183185
String operationPath = pathPattern.getPatternString();
184186
Map<String, String> regexMap = new LinkedHashMap<>();
185187
operationPath = PathUtils.parsePath(operationPath, regexMap);
186-
String[] produces = requestMappingInfo.getProducesCondition().getProducibleMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
187-
String[] consumes = requestMappingInfo.getConsumesCondition().getConsumableMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
188-
String[] headers = requestMappingInfo.getHeadersCondition().getExpressions().stream().map(Object::toString).toArray(String[]::new);
189-
if (operationPath.startsWith(DEFAULT_PATH_SEPARATOR)
190-
&& (restControllers.containsKey(handlerMethod.getBean().toString()) || (isShowActuator()))
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+
if ((operationPath.startsWith(DEFAULT_PATH_SEPARATOR)
192+
&& (isRestController(restControllers,handlerMethod) || (isShowActuator())))
191193
&& isFilterCondition(handlerMethod, operationPath, produces, consumes, headers)) {
192194
Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
193195
// default allowed requestmethods
@@ -246,4 +248,15 @@ protected void calculateServerUrl(ServerHttpRequest serverHttpRequest, String ap
246248
*/
247249
protected abstract String getServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl);
248250

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+
}
249262
}
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
}

0 commit comments

Comments
 (0)