Skip to content

Commit 68b980f

Browse files
committed
Remove producible request attribute before mapping
The attribute was previously removed only before exception resolution in the DispatcherServlet in order to allow error rendering to make an independent choice on content negotation. However, Boot rendering happens later in an ERROR dispatch which could also be a nested dispatch on some servers. So the attribute must also generally be removed prior to mapping. We also move the methods where this is done to the base RequestMappingInfoHandlerMapping class which also deals with the produces condition and where the producible attribute is added in the first place. Closes gh-24466
1 parent 87f866b commit 68b980f

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Set;
2828
import java.util.stream.Collectors;
2929

30+
import reactor.core.publisher.Mono;
31+
3032
import org.springframework.http.HttpHeaders;
3133
import org.springframework.http.HttpMethod;
3234
import org.springframework.http.InvalidMediaTypeException;
@@ -38,6 +40,7 @@
3840
import org.springframework.web.method.HandlerMethod;
3941
import org.springframework.web.reactive.HandlerMapping;
4042
import org.springframework.web.reactive.result.condition.NameValueExpression;
43+
import org.springframework.web.reactive.result.condition.ProducesRequestCondition;
4144
import org.springframework.web.server.MethodNotAllowedException;
4245
import org.springframework.web.server.NotAcceptableStatusException;
4346
import org.springframework.web.server.ServerWebExchange;
@@ -87,6 +90,13 @@ protected Comparator<RequestMappingInfo> getMappingComparator(final ServerWebExc
8790
return (info1, info2) -> info1.compareTo(info2, exchange);
8891
}
8992

93+
@Override
94+
public Mono<HandlerMethod> getHandlerInternal(ServerWebExchange exchange) {
95+
exchange.getAttributes().remove(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
96+
return super.getHandlerInternal(exchange)
97+
.doOnTerminate(() -> ProducesRequestCondition.clearMediaTypesAttribute(exchange));
98+
}
99+
90100
/**
91101
* Expose URI template variables, matrix variables, and producible media types in the request.
92102
* @see HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -24,8 +24,6 @@
2424
import java.util.Map;
2525
import java.util.function.Predicate;
2626

27-
import reactor.core.publisher.Mono;
28-
2927
import org.springframework.context.EmbeddedValueResolverAware;
3028
import org.springframework.core.annotation.AnnotatedElementUtils;
3129
import org.springframework.core.annotation.MergedAnnotation;
@@ -45,11 +43,9 @@
4543
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
4644
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
4745
import org.springframework.web.reactive.result.condition.ConsumesRequestCondition;
48-
import org.springframework.web.reactive.result.condition.ProducesRequestCondition;
4946
import org.springframework.web.reactive.result.condition.RequestCondition;
5047
import org.springframework.web.reactive.result.method.RequestMappingInfo;
5148
import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping;
52-
import org.springframework.web.server.ServerWebExchange;
5349

5450
/**
5551
* An extension of {@link RequestMappingInfoHandlerMapping} that creates
@@ -356,10 +352,4 @@ private String resolveCorsAnnotationValue(String value) {
356352
}
357353
}
358354

359-
@Override
360-
public Mono<HandlerMethod> getHandlerInternal(ServerWebExchange exchange) {
361-
return super.getHandlerInternal(exchange)
362-
.doOnTerminate(() -> ProducesRequestCondition.clearMediaTypesAttribute(exchange));
363-
}
364-
365355
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -45,6 +45,7 @@
4545
import org.springframework.web.servlet.HandlerMapping;
4646
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
4747
import org.springframework.web.servlet.mvc.condition.NameValueExpression;
48+
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
4849
import org.springframework.web.util.WebUtils;
4950

5051
/**
@@ -102,6 +103,17 @@ protected Comparator<RequestMappingInfo> getMappingComparator(final HttpServletR
102103
return (info1, info2) -> info1.compareTo(info2, request);
103104
}
104105

106+
@Override
107+
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
108+
request.removeAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
109+
try {
110+
return super.getHandlerInternal(request);
111+
}
112+
finally {
113+
ProducesRequestCondition.clearMediaTypesAttribute(request);
114+
}
115+
}
116+
105117
/**
106118
* Expose URI template variables, matrix variables, and producible media types in the request.
107119
* @see HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
5050
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
5151
import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
52-
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
5352
import org.springframework.web.servlet.mvc.condition.RequestCondition;
5453
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
5554
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
@@ -473,14 +472,4 @@ private String resolveCorsAnnotationValue(String value) {
473472
}
474473
}
475474

476-
@Override
477-
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
478-
try {
479-
return super.getHandlerInternal(request);
480-
}
481-
finally {
482-
ProducesRequestCondition.clearMediaTypesAttribute(request);
483-
}
484-
}
485-
486475
}

0 commit comments

Comments
 (0)