Skip to content

Commit

Permalink
Fix pattern extraction when MVC is using a PathPatternParser
Browse files Browse the repository at this point in the history
Fixes gh-24874
  • Loading branch information
wilkinsona committed Jan 19, 2021
1 parent 59b0132 commit 3ad2832
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
@@ -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.
Expand All @@ -23,6 +23,7 @@
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;

/**
Expand Down Expand Up @@ -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;
}
Expand Down
@@ -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.
Expand Down Expand Up @@ -56,6 +56,9 @@
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;
Expand Down Expand Up @@ -94,6 +97,28 @@ void servletWebMappings() {
});
}

@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();
Expand Down Expand Up @@ -260,4 +285,21 @@ private DispatcherServlet createTestDispatcherServlet(WebApplicationContext cont

}

@Configuration
static class PathPatternParserConfiguration {

@Bean
WebMvcConfigurer pathPatternParserConfigurer() {
return new WebMvcConfigurer() {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setPatternParser(new PathPatternParser());
}

};
}

}

}

0 comments on commit 3ad2832

Please sign in to comment.