Skip to content

Commit 3c4b3fb

Browse files
committed
Merge branch 'master' of https://github.com/azige/springdoc-openapi into azige-master
2 parents 98db8f2 + 5602cf0 commit 3c4b3fb

File tree

6 files changed

+233
-5
lines changed

6 files changed

+233
-5
lines changed

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

Lines changed: 9 additions & 3 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.AnnotationUtils;
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

@@ -186,8 +188,12 @@ protected void calculatePath(Map<String, Object> restControllers, Map<RequestMap
186188
String[] produces = requestMappingInfo.getProducesCondition().getProducibleMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
187189
String[] consumes = requestMappingInfo.getConsumesCondition().getConsumableMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
188190
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()))
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
194+
if ((operationPath.startsWith(DEFAULT_PATH_SEPARATOR)
195+
&& (restControllers.containsKey(handlerMethod.getBean().toString()) || (isShowActuator()))
196+
|| operationAnnotation != null)
191197
&& isFilterCondition(handlerMethod, operationPath, produces, consumes, headers)) {
192198
Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
193199
// default allowed requestmethods

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import com.fasterxml.jackson.core.JsonProcessingException;
3636
import io.swagger.v3.core.util.PathUtils;
37+
import io.swagger.v3.oas.annotations.Operation;
3738
import io.swagger.v3.oas.models.OpenAPI;
3839
import org.springdoc.api.AbstractOpenApiResource;
3940
import org.springdoc.core.AbstractRequestService;
@@ -66,7 +67,7 @@
6667

6768
/**
6869
* The type Web mvc open api resource.
69-
* @author bnasslahsen
70+
* @author bnasslahsen, Azige
7071
*/
7172
public abstract class OpenApiResource extends AbstractOpenApiResource {
7273

@@ -240,8 +241,10 @@ protected void calculatePath(Map<String, Object> restControllers, Map<RequestMap
240241
String[] produces = requestMappingInfo.getProducesCondition().getProducibleMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
241242
String[] consumes = requestMappingInfo.getConsumesCondition().getConsumableMediaTypes().stream().map(MimeType::toString).toArray(String[]::new);
242243
String[] headers = requestMappingInfo.getHeadersCondition().getExpressions().stream().map(Object::toString).toArray(String[]::new);
244+
Operation operationAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Operation.class);
243245
if (((isShowActuator() && optionalActuatorProvider.get().isRestController(operationPath, handlerMethod.getBeanType()))
244-
|| isRestController(restControllers, handlerMethod, operationPath))
246+
|| isRestController(restControllers, handlerMethod, operationPath)
247+
|| operationAnnotation != null)
245248
&& isFilterCondition(handlerMethod, operationPath, produces, consumes, headers)) {
246249
Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
247250
// default allowed requestmethods
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.Parameter;
23+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
24+
import io.swagger.v3.oas.annotations.media.Content;
25+
import io.swagger.v3.oas.annotations.media.Schema;
26+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
27+
28+
import org.springframework.stereotype.Controller;
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.ResponseBody;
31+
import org.springframework.web.servlet.ModelAndView;
32+
33+
/**
34+
* To test the case a user does not use @RestController but puts @Operation on handler methods
35+
* and wants these methods to be exposed.
36+
*
37+
* @author Azige
38+
*/
39+
@Controller
40+
public class HelloController {
41+
42+
@GetMapping("/hello")
43+
@Operation(responses = @ApiResponse(
44+
responseCode = "200",
45+
description = "OK",
46+
content = @Content(schema = @Schema(implementation = HelloMessage.class))
47+
))
48+
public String hello() {
49+
return "forward:/message";
50+
}
51+
52+
@GetMapping("/message")
53+
@Operation
54+
@ResponseBody
55+
public HelloMessage message() {
56+
return new HelloMessage("Lucky numbers!", 777);
57+
}
58+
59+
@GetMapping("/helloModelAndView")
60+
@Operation(responses = @ApiResponse(
61+
responseCode = "200",
62+
description = "OK",
63+
content = @Content(schema = @Schema(implementation = HelloMessage.class))
64+
))
65+
public ModelAndView helloModelAndView() {
66+
ModelAndView mav = new ModelAndView();
67+
mav.setViewName("forward:/message");
68+
return mav;
69+
}
70+
}
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: 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+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
25+
public class SpringDocApp149Test extends AbstractSpringDocTest {
26+
27+
@SpringBootApplication
28+
static class SpringDocTestApp {}
29+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/hello": {
15+
"get": {
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+
}
32+
}
33+
},
34+
"/message": {
35+
"get": {
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+
}
52+
}
53+
},
54+
"/helloModelAndView": {
55+
"get": {
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+
}
72+
}
73+
}
74+
},
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+
}
91+
}

0 commit comments

Comments
 (0)