Skip to content

Commit 147b8fb

Browse files
committed
Turn off use of path extensions by default
Closes gh-23915
1 parent 153690e commit 147b8fb

File tree

9 files changed

+161
-194
lines changed

9 files changed

+161
-194
lines changed

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@
5050
* <th>Enabled Or Not</th>
5151
* </tr>
5252
* <tr>
53-
* <td>{@link #setFavorPathExtension favorPathExtension}</td>
54-
* <td>true</td>
55-
* <td>{@link PathExtensionContentNegotiationStrategy}</td>
56-
* <td>Enabled</td>
57-
* </tr>
58-
* <tr>
5953
* <td>{@link #setFavorParameter favorParameter}</td>
6054
* <td>false</td>
6155
* <td>{@link ParameterContentNegotiationStrategy}</td>
6256
* <td>Off</td>
6357
* </tr>
6458
* <tr>
59+
* <td>{@link #setFavorPathExtension favorPathExtension}</td>
60+
* <td>false (as of 5.3)</td>
61+
* <td>{@link PathExtensionContentNegotiationStrategy}</td>
62+
* <td>Off</td>
63+
* </tr>
64+
* <tr>
6565
* <td>{@link #setIgnoreAcceptHeader ignoreAcceptHeader}</td>
6666
* <td>false</td>
6767
* <td>{@link HeaderContentNegotiationStrategy}</td>
@@ -104,11 +104,11 @@ public class ContentNegotiationManagerFactoryBean
104104
private List<ContentNegotiationStrategy> strategies;
105105

106106

107-
private boolean favorPathExtension = true;
108-
109107
private boolean favorParameter = false;
110108

111-
private boolean ignoreAcceptHeader = false;
109+
private String parameterName = "format";
110+
111+
private boolean favorPathExtension = true;
112112

113113
private Map<String, MediaType> mediaTypes = new HashMap<>();
114114

@@ -117,7 +117,7 @@ public class ContentNegotiationManagerFactoryBean
117117
@Nullable
118118
private Boolean useRegisteredExtensionsOnly;
119119

120-
private String parameterName = "format";
120+
private boolean ignoreAcceptHeader = false;
121121

122122
@Nullable
123123
private ContentNegotiationStrategy defaultNegotiationStrategy;
@@ -141,17 +141,35 @@ public void setStrategies(@Nullable List<ContentNegotiationStrategy> strategies)
141141
this.strategies = (strategies != null ? new ArrayList<>(strategies) : null);
142142
}
143143

144+
/**
145+
* Whether a request parameter ("format" by default) should be used to
146+
* determine the requested media type. For this option to work you must
147+
* register {@link #setMediaTypes media type mappings}.
148+
* <p>By default this is set to {@code false}.
149+
* @see #setParameterName
150+
*/
151+
public void setFavorParameter(boolean favorParameter) {
152+
this.favorParameter = favorParameter;
153+
}
154+
155+
/**
156+
* Set the query parameter name to use when {@link #setFavorParameter} is on.
157+
* <p>The default parameter name is {@code "format"}.
158+
*/
159+
public void setParameterName(String parameterName) {
160+
Assert.notNull(parameterName, "parameterName is required");
161+
this.parameterName = parameterName;
162+
}
163+
144164
/**
145165
* Whether the path extension in the URL path should be used to determine
146166
* the requested media type.
147-
* <p>By default this is set to {@code true} in which case a request
148-
* for {@code /hotels.pdf} will be interpreted as a request for
149-
* {@code "application/pdf"} regardless of the 'Accept' header.
167+
* <p>By default this is set to {@code false} in which case path extensions
168+
* have no impact on content negotiation.
150169
* @deprecated as of 5.2.4. See class-level note on the deprecation of path
151170
* extension config options. As there is no replacement for this method,
152-
* for the time being it's necessary to continue using it in order to set it
153-
* to {@code false}. In 5.3 when {@code false} becomes the default, use of
154-
* this property will no longer be necessary.
171+
* in 5.2.x it is necessary to set it to {@code false}. In 5.3 {@code false}
172+
* becomes the default, and use of this property is longer be necessary.
155173
*/
156174
@Deprecated
157175
public void setFavorPathExtension(boolean favorPathExtension) {
@@ -224,8 +242,8 @@ public void setIgnoreUnknownPathExtensions(boolean ignore) {
224242
/**
225243
* Indicate whether to use the Java Activation Framework as a fallback option
226244
* to map from file extensions to media types.
227-
* @deprecated as of 5.0, in favor of {@link #setUseRegisteredExtensionsOnly(boolean)}, which
228-
* has reverse behavior.
245+
* @deprecated as of 5.0, in favor of {@link #setUseRegisteredExtensionsOnly(boolean)},
246+
* which has reverse behavior.
229247
*/
230248
@Deprecated
231249
public void setUseJaf(boolean useJaf) {
@@ -247,26 +265,6 @@ private boolean useRegisteredExtensionsOnly() {
247265
return (this.useRegisteredExtensionsOnly != null && this.useRegisteredExtensionsOnly);
248266
}
249267

250-
/**
251-
* Whether a request parameter ("format" by default) should be used to
252-
* determine the requested media type. For this option to work you must
253-
* register {@link #setMediaTypes media type mappings}.
254-
* <p>By default this is set to {@code false}.
255-
* @see #setParameterName
256-
*/
257-
public void setFavorParameter(boolean favorParameter) {
258-
this.favorParameter = favorParameter;
259-
}
260-
261-
/**
262-
* Set the query parameter name to use when {@link #setFavorParameter} is on.
263-
* <p>The default parameter name is {@code "format"}.
264-
*/
265-
public void setParameterName(String parameterName) {
266-
Assert.notNull(parameterName, "parameterName is required");
267-
this.parameterName = parameterName;
268-
}
269-
270268
/**
271269
* Whether to disable checking the 'Accept' request header.
272270
* <p>By default this value is set to {@code false}.

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@
4949
* <th>Enabled Or Not</th>
5050
* </tr>
5151
* <tr>
52-
* <td>{@link #favorPathExtension}</td>
53-
* <td>true</td>
54-
* <td>{@link org.springframework.web.accept.PathExtensionContentNegotiationStrategy
55-
* PathExtensionContentNegotiationStrategy}</td>
56-
* <td>Enabled</td>
57-
* </tr>
58-
* <tr>
5952
* <td>{@link #favorParameter}</td>
6053
* <td>false</td>
6154
* <td>{@link ParameterContentNegotiationStrategy}</td>
6255
* <td>Off</td>
6356
* </tr>
6457
* <tr>
58+
* <td>{@link #favorPathExtension}</td>
59+
* <td>false (as of 5.3)</td>
60+
* <td>{@link org.springframework.web.accept.PathExtensionContentNegotiationStrategy
61+
* PathExtensionContentNegotiationStrategy}</td>
62+
* <td>Off</td>
63+
* </tr>
64+
* <tr>
6565
* <td>{@link #ignoreAcceptHeader}</td>
6666
* <td>false</td>
6767
* <td>{@link HeaderContentNegotiationStrategy}</td>
@@ -123,18 +123,34 @@ public void strategies(@Nullable List<ContentNegotiationStrategy> strategies) {
123123
this.factory.setStrategies(strategies);
124124
}
125125

126+
/**
127+
* Whether a request parameter ("format" by default) should be used to
128+
* determine the requested media type. For this option to work you must
129+
* register {@link #mediaType(String, MediaType) media type mappings}.
130+
* <p>By default this is set to {@code false}.
131+
* @see #parameterName(String)
132+
*/
133+
public ContentNegotiationConfigurer favorParameter(boolean favorParameter) {
134+
this.factory.setFavorParameter(favorParameter);
135+
return this;
136+
}
137+
138+
/**
139+
* Set the query parameter name to use when {@link #favorParameter} is on.
140+
* <p>The default parameter name is {@code "format"}.
141+
*/
142+
public ContentNegotiationConfigurer parameterName(String parameterName) {
143+
this.factory.setParameterName(parameterName);
144+
return this;
145+
}
146+
126147
/**
127148
* Whether the path extension in the URL path should be used to determine
128149
* the requested media type.
129-
* <p>By default this is set to {@code true} in which case a request
130-
* for {@code /hotels.pdf} will be interpreted as a request for
131-
* {@code "application/pdf"} regardless of the 'Accept' header.
132-
* @deprecated as of 5.2.4. See class-level note in
133-
* {@link ContentNegotiationManagerFactoryBean} on the deprecation of path
134-
* extension config options. As there is no replacement for this method,
135-
* for the time being it's necessary to continue using it in order to set it
136-
* to {@code false}. In 5.3 when {@code false} becomes the default, use of
137-
* this property will no longer be necessary.
150+
* <p>By default this is set to {@code false} in which case path extensions
151+
* have no impact on content negotiation.
152+
* @deprecated as of 5.2.4. See deprecation note on
153+
* {@link ContentNegotiationManagerFactoryBean#setFavorPathExtension(boolean)}.
138154
*/
139155
@Deprecated
140156
public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension) {
@@ -190,9 +206,8 @@ public ContentNegotiationConfigurer replaceMediaTypes(Map<String, MediaType> med
190206
* to any media type. Setting this to {@code false} will result in an
191207
* {@code HttpMediaTypeNotAcceptableException} if there is no match.
192208
* <p>By default this is set to {@code true}.
193-
* @deprecated as of 5.2.4. See class-level note in
194-
* {@link ContentNegotiationManagerFactoryBean} on the deprecation of path
195-
* extension config options.
209+
* @deprecated as of 5.2.4. See deprecation note on
210+
* {@link ContentNegotiationManagerFactoryBean#setIgnoreUnknownPathExtensions(boolean)}.
196211
*/
197212
@Deprecated
198213
public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore) {
@@ -224,27 +239,6 @@ public ContentNegotiationConfigurer useRegisteredExtensionsOnly(boolean useRegis
224239
return this;
225240
}
226241

227-
/**
228-
* Whether a request parameter ("format" by default) should be used to
229-
* determine the requested media type. For this option to work you must
230-
* register {@link #mediaType(String, MediaType) media type mappings}.
231-
* <p>By default this is set to {@code false}.
232-
* @see #parameterName(String)
233-
*/
234-
public ContentNegotiationConfigurer favorParameter(boolean favorParameter) {
235-
this.factory.setFavorParameter(favorParameter);
236-
return this;
237-
}
238-
239-
/**
240-
* Set the query parameter name to use when {@link #favorParameter} is on.
241-
* <p>The default parameter name is {@code "format"}.
242-
*/
243-
public ContentNegotiationConfigurer parameterName(String parameterName) {
244-
this.factory.setParameterName(parameterName);
245-
return this;
246-
}
247-
248242
/**
249243
* Whether to disable checking the 'Accept' request header.
250244
* <p>By default this value is set to {@code false}.

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public class PathMatchConfigurer {
6969
* @see #registeredSuffixPatternMatch
7070
* @deprecated as of 5.2.4. See class-level note in
7171
* {@link RequestMappingHandlerMapping} on the deprecation of path extension
72-
* config options. As there is no replacement for this method, for the time
73-
* being it's necessary to set it to {@code false}. In 5.3 when {@code false}
74-
* becomes the default, use of this property will no longer be necessary.
72+
* config options. As there is no replacement for this method, in 5.2.x it is
73+
* necessary to set it to {@code false}. In 5.3 {@code false} becomes the
74+
* default, and use of this property is longer be necessary.
7575
*/
7676
@Deprecated
7777
public PathMatchConfigurer setUseSuffixPatternMatch(Boolean suffixPatternMatch) {
@@ -150,9 +150,8 @@ public PathMatchConfigurer addPathPrefix(String prefix, Predicate<Class<?>> pred
150150

151151
/**
152152
* Whether to use registered suffixes for pattern matching.
153-
* @deprecated as of 5.2.4. See class-level note in
154-
* {@link RequestMappingHandlerMapping} on the deprecation of path extension
155-
* config options.
153+
* @deprecated as of 5.2.4, see deprecation note on
154+
* {@link #setUseSuffixPatternMatch(Boolean)}.
156155
*/
157156
@Nullable
158157
@Deprecated
@@ -162,9 +161,8 @@ public Boolean isUseSuffixPatternMatch() {
162161

163162
/**
164163
* Whether to use registered suffixes for pattern matching.
165-
* @deprecated as of 5.2.4. See class-level note in
166-
* {@link RequestMappingHandlerMapping} on the deprecation of path extension
167-
* config options.
164+
* @deprecated as of 5.2.4, see deprecation note on
165+
* {@link #setUseRegisteredSuffixPatternMatch(Boolean)}.
168166
*/
169167
@Nullable
170168
@Deprecated

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public static class BuilderConfiguration {
543543

544544
private boolean trailingSlashMatch = true;
545545

546-
private boolean suffixPatternMatch = true;
546+
private boolean suffixPatternMatch = false;
547547

548548
private boolean registeredSuffixPatternMatch = false;
549549

@@ -600,11 +600,10 @@ public boolean useTrailingSlashMatch() {
600600

601601
/**
602602
* Set whether to apply suffix pattern matching in PatternsRequestCondition.
603-
* <p>By default this is set to 'true'.
603+
* <p>By default this is set to 'false'.
604604
* @see #setRegisteredSuffixPatternMatch(boolean)
605-
* @deprecated as of 5.2.4. See class-level note in
606-
* {@link RequestMappingHandlerMapping} on the deprecation of path
607-
* extension config options.
605+
* @deprecated as of 5.2.4. See deprecation note on
606+
* {@link RequestMappingHandlerMapping#setUseSuffixPatternMatch(boolean)}.
608607
*/
609608
@Deprecated
610609
public void setSuffixPatternMatch(boolean suffixPatternMatch) {
@@ -613,9 +612,8 @@ public void setSuffixPatternMatch(boolean suffixPatternMatch) {
613612

614613
/**
615614
* Return whether to apply suffix pattern matching in PatternsRequestCondition.
616-
* @deprecated as of 5.2.4. See class-level note in
617-
* {@link RequestMappingHandlerMapping} on the deprecation of path
618-
* extension config options.
615+
* @deprecated as of 5.2.4. See deprecation note on
616+
* {@link RequestMappingHandlerMapping#setUseSuffixPatternMatch(boolean)}.
619617
*/
620618
@Deprecated
621619
public boolean useSuffixPatternMatch() {
@@ -630,8 +628,7 @@ public boolean useSuffixPatternMatch() {
630628
* obtain the registered file extensions.
631629
* @deprecated as of 5.2.4. See class-level note in
632630
* {@link RequestMappingHandlerMapping} on the deprecation of path
633-
* extension config options; note also that in 5.3 the default for this
634-
* property switches from {@code false} to {@code true}.
631+
* extension config options.
635632
*/
636633
@Deprecated
637634
public void setRegisteredSuffixPatternMatch(boolean registeredSuffixPatternMatch) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
* <p><strong>Deprecation Note:</strong></p> In 5.2.4,
6262
* {@link #setUseSuffixPatternMatch(boolean) useSuffixPatternMatch} and
6363
* {@link #setUseRegisteredSuffixPatternMatch(boolean) useRegisteredSuffixPatternMatch}
64-
* are deprecated in order to discourage use of path extensions for request
64+
* were deprecated in order to discourage use of path extensions for request
6565
* mapping and for content negotiation (with similar deprecations in
6666
* {@link ContentNegotiationManager}). For further context, please read issue
6767
* <a href="https://github.com/spring-projects/spring-framework/issues/24179">#24719</a>.
@@ -74,7 +74,7 @@
7474
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
7575
implements MatchableHandlerMapping, EmbeddedValueResolverAware {
7676

77-
private boolean useSuffixPatternMatch = true;
77+
private boolean useSuffixPatternMatch = false;
7878

7979
private boolean useRegisteredSuffixPatternMatch = false;
8080

@@ -93,14 +93,13 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
9393
/**
9494
* Whether to use suffix pattern match (".*") when matching patterns to
9595
* requests. If enabled a method mapped to "/users" also matches to "/users.*".
96-
* <p>The default value is {@code true}.
96+
* <p>By default value this is set to {@code false}.
9797
* <p>Also see {@link #setUseRegisteredSuffixPatternMatch(boolean)} for
9898
* more fine-grained control over specific suffixes to allow.
99-
* @deprecated as of 5.2.4. See class level comment about deprecation of
99+
* @deprecated as of 5.2.4. See class level note on the deprecation of
100100
* path extension config options. As there is no replacement for this method,
101-
* for the time being it's necessary to set it to {@code false}. In 5.3
102-
* when {@code false} becomes the default, use of this property will no
103-
* longer be necessary.
101+
* in 5.2.x it is necessary to set it to {@code false}. In 5.3 {@code false}
102+
* becomes the default, and use of this property is longer be necessary.
104103
*/
105104
@Deprecated
106105
public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch) {
@@ -113,7 +112,7 @@ public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch) {
113112
* is generally recommended to reduce ambiguity and to avoid issues such as
114113
* when a "." appears in the path for other reasons.
115114
* <p>By default this is set to "false".
116-
* @deprecated as of 5.2.4. See class level comment about deprecation of
115+
* @deprecated as of 5.2.4. See class level note on the deprecation of
117116
* path extension config options.
118117
*/
119118
@Deprecated
@@ -191,8 +190,8 @@ public void afterPropertiesSet() {
191190

192191
/**
193192
* Whether to use registered suffixes for pattern matching.
194-
* @deprecated as of 5.2.4. See class-level note on the deprecation of path
195-
* extension config options.
193+
* @deprecated as of 5.2.4. See deprecation notice on
194+
* {@link #setUseSuffixPatternMatch(boolean)}.
196195
*/
197196
@Deprecated
198197
public boolean useSuffixPatternMatch() {
@@ -201,8 +200,8 @@ public boolean useSuffixPatternMatch() {
201200

202201
/**
203202
* Whether to use registered suffixes for pattern matching.
204-
* @deprecated as of 5.2.4. See class-level note on the deprecation of path
205-
* extension config options.
203+
* @deprecated as of 5.2.4. See deprecation notice on
204+
* {@link #setUseRegisteredSuffixPatternMatch(boolean)}.
206205
*/
207206
@Deprecated
208207
public boolean useRegisteredSuffixPatternMatch() {

0 commit comments

Comments
 (0)