Fix pattern extraction when MVC is using a PathPatternParser

Fixes gh-24874
This commit is contained in:
Andy Wilkinson 2021-01-19 10:34:11 +00:00
parent 59b01324ae
commit 3ad2832cb2
2 changed files with 52 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.condition.MediaTypeExpression;
import org.springframework.web.servlet.mvc.condition.NameValueExpression;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
/**
@ -53,11 +54,17 @@ public class RequestMappingConditionsDescription {
this.methods = requestMapping.getMethodsCondition().getMethods();
this.params = requestMapping.getParamsCondition().getExpressions().stream()
.map(NameValueExpressionDescription::new).collect(Collectors.toList());
this.patterns = requestMapping.getPatternsCondition().getPatterns();
this.patterns = extractPathPatterns(requestMapping);
this.produces = requestMapping.getProducesCondition().getExpressions().stream()
.map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
}
private Set<String> extractPathPatterns(RequestMappingInfo requestMapping) {
PatternsRequestCondition patternsCondition = requestMapping.getPatternsCondition();
return (patternsCondition != null) ? patternsCondition.getPatterns()
: requestMapping.getPathPatternsCondition().getPatternValues();
}
public List<MediaTypeExpressionDescription> getConsumes() {
return this.consumes;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -56,6 +56,9 @@ import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -94,6 +97,28 @@ class MappingsEndpointTests {
});
}
@Test
void servletWebMappingsWithPathPatternParser() {
Supplier<ConfigurableWebApplicationContext> contextSupplier = prepareContextSupplier();
new WebApplicationContextRunner(contextSupplier).withUserConfiguration(EndpointConfiguration.class,
ServletWebConfiguration.class, PathPatternParserConfiguration.class).run((context) -> {
ContextMappings contextMappings = contextMappings(context);
assertThat(contextMappings.getParentId()).isNull();
assertThat(contextMappings.getMappings()).containsOnlyKeys("dispatcherServlets", "servletFilters",
"servlets");
Map<String, List<DispatcherServletMappingDescription>> dispatcherServlets = mappings(
contextMappings, "dispatcherServlets");
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet");
List<DispatcherServletMappingDescription> handlerMappings = dispatcherServlets
.get("dispatcherServlet");
assertThat(handlerMappings).hasSize(1);
List<ServletRegistrationMappingDescription> servlets = mappings(contextMappings, "servlets");
assertThat(servlets).hasSize(1);
List<FilterRegistrationMappingDescription> filters = mappings(contextMappings, "servletFilters");
assertThat(filters).hasSize(1);
});
}
@Test
void servletWebMappingsWithAdditionalDispatcherServlets() {
Supplier<ConfigurableWebApplicationContext> contextSupplier = prepareContextSupplier();
@ -260,4 +285,21 @@ class MappingsEndpointTests {
}
@Configuration
static class PathPatternParserConfiguration {
@Bean
WebMvcConfigurer pathPatternParserConfigurer() {
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setPatternParser(new PathPatternParser());
}
};
}
}
}