Skip to content

Commit 0757eae

Browse files
committed
Clear ProducesRequestCondition attribute
Closes gh-23091
1 parent 859923b commit 0757eae

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java

+11
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ private List<ProduceMediaTypeExpression> getExpressionsToCompare() {
320320
}
321321

322322

323+
/**
324+
* Use this to clear {@link #MEDIA_TYPES_ATTRIBUTE} that contains the parsed,
325+
* requested media types.
326+
* @param exchange the current exchange
327+
* @since 5.2
328+
*/
329+
public static void clearMediaTypesAttribute(ServerWebExchange exchange) {
330+
exchange.getAttributes().remove(MEDIA_TYPES_ATTRIBUTE);
331+
}
332+
333+
323334
/**
324335
* Parses and matches a single media type expression to a request's 'Accept' header.
325336
*/

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

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Map;
2525
import java.util.function.Predicate;
2626

27+
import reactor.core.publisher.Mono;
28+
2729
import org.springframework.context.EmbeddedValueResolverAware;
2830
import org.springframework.core.annotation.AnnotatedElementUtils;
2931
import org.springframework.core.annotation.MergedAnnotation;
@@ -43,9 +45,11 @@
4345
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
4446
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
4547
import org.springframework.web.reactive.result.condition.ConsumesRequestCondition;
48+
import org.springframework.web.reactive.result.condition.ProducesRequestCondition;
4649
import org.springframework.web.reactive.result.condition.RequestCondition;
4750
import org.springframework.web.reactive.result.method.RequestMappingInfo;
4851
import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping;
52+
import org.springframework.web.server.ServerWebExchange;
4953

5054
/**
5155
* An extension of {@link RequestMappingInfoHandlerMapping} that creates
@@ -352,4 +356,10 @@ private String resolveCorsAnnotationValue(String value) {
352356
}
353357
}
354358

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

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java

+11
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ private List<ProduceMediaTypeExpression> getExpressionsToCompare() {
326326
}
327327

328328

329+
/**
330+
* Use this to clear {@link #MEDIA_TYPES_ATTRIBUTE} that contains the parsed,
331+
* requested media types.
332+
* @param request the current request
333+
* @since 5.2
334+
*/
335+
public static void clearMediaTypesAttribute(HttpServletRequest request) {
336+
request.removeAttribute(MEDIA_TYPES_ATTRIBUTE);
337+
}
338+
339+
329340
/**
330341
* Parses and matches a single media type expression to a request's 'Accept' header.
331342
*/

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

+11
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
4949
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
5050
import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
51+
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
5152
import org.springframework.web.servlet.mvc.condition.RequestCondition;
5253
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
5354
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
@@ -441,4 +442,14 @@ private String resolveCorsAnnotationValue(String value) {
441442
}
442443
}
443444

445+
@Override
446+
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
447+
try {
448+
return super.getHandlerInternal(request);
449+
}
450+
finally {
451+
ProducesRequestCondition.clearMediaTypesAttribute(request);
452+
}
453+
}
454+
444455
}

0 commit comments

Comments
 (0)