Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the ClassCastException when CacheRequestBodyFilter is used with CircuitBreakerFilter. #3547

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

qnnn
Copy link
Contributor

@qnnn qnnn commented Oct 1, 2024

Currently, the CacheRequestBodyFilter overrides the CACHED_REQUEST_BODY_ATTR attribute with the decoded object. If the fallbackUri of the CircuitBreaker is routed to another route, passing through the AdaptCachedBodyGlobalFilter triggers a ClassCastException(see unit test). The CacheRequestBodyFilter behaves similarly to a ReadBodyRoutePredicateFactory without a predicate. The current modification places the decoded body into CACHE_REQUEST_BODY_OBJECT_KEY to distinguish the responsibilities.

@qnnn qnnn marked this pull request as draft October 1, 2024 19:38
@qnnn qnnn marked this pull request as ready for review October 1, 2024 20:18
@spencergibb
Copy link
Member

I'm afraid, I still don't understand the reason for this change. It is also a breaking change, so this is something that will not happen soon.

@qnnn
Copy link
Contributor Author

qnnn commented Oct 2, 2024

@spencergibb Thank you for your quick reply. This issue arises because the cacheRequestBody replaces the DataBuffer type body in the CACHED_REQUEST_BODY_ATTR attribute with the decoded body. When forwarding, entering the AdaptCachedBodyGlobalFilter again to retrieve the DataBuffer type body throws a ClassCastException. The reproducible route definition is as follows:

.route(
       r -> r.order(-2000).path("/routeTest")
             .filters(f -> f.cacheRequestBody(String.class))
             .uri("forward:/cacheBodyTest"))
.route(
       r -> r.order(-2000).path("/cacheBodyTest")
             .uri("http://127.0.0.1:8888"))

I understand this is a breaking change, and I will follow your arrangements.

qnnn added 3 commits December 15, 2024 14:29
…rcuit-breaker-cce

# Conflicts:
#	spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants