Skip to content

Commit 3ad2832

Browse files
committed
Fix pattern extraction when MVC is using a PathPatternParser
Fixes gh-24874
1 parent 59b0132 commit 3ad2832

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/mappings/servlet/RequestMappingConditionsDescription.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.web.bind.annotation.RequestMethod;
2424
import org.springframework.web.servlet.mvc.condition.MediaTypeExpression;
2525
import org.springframework.web.servlet.mvc.condition.NameValueExpression;
26+
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
2627
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
2728

2829
/**
@@ -53,11 +54,17 @@ public class RequestMappingConditionsDescription {
5354
this.methods = requestMapping.getMethodsCondition().getMethods();
5455
this.params = requestMapping.getParamsCondition().getExpressions().stream()
5556
.map(NameValueExpressionDescription::new).collect(Collectors.toList());
56-
this.patterns = requestMapping.getPatternsCondition().getPatterns();
57+
this.patterns = extractPathPatterns(requestMapping);
5758
this.produces = requestMapping.getProducesCondition().getExpressions().stream()
5859
.map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
5960
}
6061

62+
private Set<String> extractPathPatterns(RequestMappingInfo requestMapping) {
63+
PatternsRequestCondition patternsCondition = requestMapping.getPatternsCondition();
64+
return (patternsCondition != null) ? patternsCondition.getPatterns()
65+
: requestMapping.getPathPatternsCondition().getPatternValues();
66+
}
67+
6168
public List<MediaTypeExpressionDescription> getConsumes() {
6269
return this.consumes;
6370
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/mappings/MappingsEndpointTests.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,6 +56,9 @@
5656
import org.springframework.web.reactive.function.server.ServerResponse;
5757
import org.springframework.web.servlet.DispatcherServlet;
5858
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
59+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
60+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
61+
import org.springframework.web.util.pattern.PathPatternParser;
5962

6063
import static org.assertj.core.api.Assertions.assertThat;
6164
import static org.mockito.BDDMockito.given;
@@ -94,6 +97,28 @@ void servletWebMappings() {
9497
});
9598
}
9699

100+
@Test
101+
void servletWebMappingsWithPathPatternParser() {
102+
Supplier<ConfigurableWebApplicationContext> contextSupplier = prepareContextSupplier();
103+
new WebApplicationContextRunner(contextSupplier).withUserConfiguration(EndpointConfiguration.class,
104+
ServletWebConfiguration.class, PathPatternParserConfiguration.class).run((context) -> {
105+
ContextMappings contextMappings = contextMappings(context);
106+
assertThat(contextMappings.getParentId()).isNull();
107+
assertThat(contextMappings.getMappings()).containsOnlyKeys("dispatcherServlets", "servletFilters",
108+
"servlets");
109+
Map<String, List<DispatcherServletMappingDescription>> dispatcherServlets = mappings(
110+
contextMappings, "dispatcherServlets");
111+
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet");
112+
List<DispatcherServletMappingDescription> handlerMappings = dispatcherServlets
113+
.get("dispatcherServlet");
114+
assertThat(handlerMappings).hasSize(1);
115+
List<ServletRegistrationMappingDescription> servlets = mappings(contextMappings, "servlets");
116+
assertThat(servlets).hasSize(1);
117+
List<FilterRegistrationMappingDescription> filters = mappings(contextMappings, "servletFilters");
118+
assertThat(filters).hasSize(1);
119+
});
120+
}
121+
97122
@Test
98123
void servletWebMappingsWithAdditionalDispatcherServlets() {
99124
Supplier<ConfigurableWebApplicationContext> contextSupplier = prepareContextSupplier();
@@ -260,4 +285,21 @@ private DispatcherServlet createTestDispatcherServlet(WebApplicationContext cont
260285

261286
}
262287

288+
@Configuration
289+
static class PathPatternParserConfiguration {
290+
291+
@Bean
292+
WebMvcConfigurer pathPatternParserConfigurer() {
293+
return new WebMvcConfigurer() {
294+
295+
@Override
296+
public void configurePathMatch(PathMatchConfigurer configurer) {
297+
configurer.setPatternParser(new PathPatternParser());
298+
}
299+
300+
};
301+
}
302+
303+
}
304+
263305
}

0 commit comments

Comments
 (0)