Skip to content

Commit 2d61892

Browse files
committed
Enabling PathPatternParser causes endpoints to be ignored. Fixes #1034.
1 parent b6396f8 commit 2d61892

File tree

4 files changed

+126
-4
lines changed

4 files changed

+126
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@
5353
import org.springframework.beans.factory.ObjectFactory;
5454
import org.springframework.core.annotation.AnnotatedElementUtils;
5555
import org.springframework.core.annotation.AnnotationUtils;
56+
import org.springframework.util.CollectionUtils;
5657
import org.springframework.util.MimeType;
5758
import org.springframework.web.bind.annotation.RequestMethod;
5859
import org.springframework.web.bind.annotation.ResponseBody;
5960
import org.springframework.web.method.HandlerMethod;
6061
import org.springframework.web.servlet.ModelAndView;
62+
import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
6163
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
6264
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
6365
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
@@ -233,9 +235,8 @@ protected void calculatePath(Map<String, Object> restControllers, Map<RequestMap
233235
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : entries) {
234236
RequestMappingInfo requestMappingInfo = entry.getKey();
235237
HandlerMethod handlerMethod = entry.getValue();
236-
PatternsRequestCondition patternsRequestCondition = requestMappingInfo.getPatternsCondition();
237-
if (patternsRequestCondition != null) {
238-
Set<String> patterns = patternsRequestCondition.getPatterns();
238+
Set<String> patterns = getActivePatterns(requestMappingInfo);
239+
if (!CollectionUtils.isEmpty(patterns)) {
239240
Map<String, String> regexMap = new LinkedHashMap<>();
240241
for (String pattern : patterns) {
241242
String operationPath = PathUtils.parsePath(pattern, regexMap);
@@ -256,6 +257,25 @@ && isFilterCondition(handlerMethod, operationPath, produces, consumes, headers))
256257
}
257258
}
258259

260+
/**
261+
* Gets active patterns.
262+
*
263+
* @param requestMappingInfo the request mapping info
264+
* @return the active patterns
265+
*/
266+
private Set<String> getActivePatterns(RequestMappingInfo requestMappingInfo) {
267+
Set<String> patterns = null;
268+
PatternsRequestCondition patternsRequestCondition = requestMappingInfo.getPatternsCondition();
269+
if (patternsRequestCondition != null)
270+
patterns = patternsRequestCondition.getPatterns();
271+
else {
272+
PathPatternsRequestCondition pathPatternsRequestCondition = requestMappingInfo.getPathPatternsCondition();
273+
if (pathPatternsRequestCondition != null)
274+
patterns = pathPatternsRequestCondition.getPatternValues();
275+
}
276+
return patterns;
277+
}
278+
259279
/**
260280
* By reversed request mapping infos comparator.
261281
*
@@ -297,7 +317,7 @@ protected boolean isRestController(Map<String, Object> restControllers, HandlerM
297317
*/
298318
protected void calculateServerUrl(HttpServletRequest request, String apiDocsUrl) {
299319
super.initOpenAPIBuilder();
300-
String calculatedUrl = getServerUrl(request,apiDocsUrl);
320+
String calculatedUrl = getServerUrl(request, apiDocsUrl);
301321
openAPIService.setServerBaseUrl(calculatedUrl);
302322
}
303323

@@ -310,4 +330,5 @@ protected void calculateServerUrl(HttpServletRequest request, String apiDocsUrl)
310330
*/
311331
protected abstract String getServerUrl(HttpServletRequest request, String apiDocsUrl);
312332

333+
313334
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package test.org.springdoc.api.app152;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9+
import org.springframework.web.util.pattern.PathPatternParser;
10+
11+
@RestController
12+
@RequestMapping("/api")
13+
public class HelloController {
14+
15+
@GetMapping
16+
public String helloWorld() {
17+
return "ok";
18+
}
19+
20+
@Configuration
21+
public class WebConfig implements WebMvcConfigurer {
22+
23+
@Override
24+
public void configurePathMatch(PathMatchConfigurer configurer) {
25+
configurer.setPatternParser(new PathPatternParser());
26+
}
27+
28+
}
29+
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.app152;
20+
21+
22+
import test.org.springdoc.api.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
26+
/**
27+
* Tests Spring meta-annotations as method parameters
28+
*/
29+
public class SpringDocApp152Test extends AbstractSpringDocTest {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
"/api": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "helloWorld",
20+
"responses": {
21+
"200": {
22+
"description": "OK",
23+
"content": {
24+
"*/*": {
25+
"schema": {
26+
"type": "string"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
},
35+
"components": {}
36+
}
37+

0 commit comments

Comments
 (0)