Check that PathPatternParser is not set too late

This commit ensures the PathPatternParser cannot be set after request mappings
have been initialized when it is too late.

See gh-26427
This commit is contained in:
Rossen Stoyanchev
2021-01-22 17:10:48 +00:00
parent 4a7a2258f9
commit 5c1cbb769c
2 changed files with 19 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-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.
@@ -48,6 +48,7 @@ import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.pattern.PathPatternParser;
/**
* Abstract base class for {@link HandlerMapping} implementations that define
@@ -99,6 +100,14 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
private final MappingRegistry mappingRegistry = new MappingRegistry();
@Override
public void setPatternParser(PathPatternParser patternParser) {
Assert.state(this.mappingRegistry.getRegistrations().isEmpty(),
"PathPatternParser must be set before the initialization of " +
"request mappings through InitializingBean#afterPropertiesSet.");
super.setPatternParser(patternParser);
}
/**
* Whether to detect handler methods in beans in ancestor ApplicationContexts.
* <p>Default is "false": Only beans in the current ApplicationContext are

View File

@@ -41,6 +41,7 @@ import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
/**
* Abstract base class for URL-mapped {@link HandlerMapping} implementations.
@@ -74,6 +75,14 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping i
private final Map<PathPattern, Object> pathPatternHandlerMap = new LinkedHashMap<>();
@Override
public void setPatternParser(PathPatternParser patternParser) {
Assert.state(this.handlerMap.isEmpty(),
"PathPatternParser must be set before the initialization of " +
"the handler map via ApplicationContextAware#setApplicationContext.");
super.setPatternParser(patternParser);
}
/**
* Set the root handler for this handler mapping, that is,
* the handler to be registered for the root path ("/").