Skip to content

Fix HttpHeaders header-map params losing values via QueryMapEncoder - #1416

Merged
ryanjbaxter merged 1 commit into
spring-cloud:mainfrom
seonwooj0810:fix/issue-1328-httpheaders-querymap-encoder
Sep 9, 2026
Merged

Fix HttpHeaders header-map params losing values via QueryMapEncoder#1416
ryanjbaxter merged 1 commit into
spring-cloud:mainfrom
seonwooj0810:fix/issue-1328-httpheaders-querymap-encoder

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

Fixes gh-1328

Root cause

Since Spring Framework 7, HttpHeaders no longer implements the MultiValueMap contract (only Serializable). A @RequestHeader HttpHeaders parameter is wired by RequestHeaderParameterProcessor as Feign's generic headerMapIndex. At invocation time, feign.RequestTemplateFactoryResolver.toQueryMap() only takes the fast instanceof Map path for a true Map; otherwise it delegates to the configured QueryMapEncoder. Spring Cloud OpenFeign registers PageableSpringQueryMapEncoder as the default QueryMapEncoder bean for all Feign clients, and its fallback (BeanQueryMapEncoder) reflects over the object's JavaBeans getter properties.

For an HttpHeaders instance this means every real header value is dropped, and the map is instead populated with unrelated getter-derived entries (contentLength=-1, date=-1, accept=[], etc.) — reproduced locally:

PageableSpringQueryMapEncoder encoder = new PageableSpringQueryMapEncoder();
HttpHeaders headers = new HttpHeaders();
headers.add("X-Custom", "val1");
headers.add(HttpHeaders.AUTHORIZATION, "Bearer tok");
encoder.encode(headers);
// => 25 getter-derived entries (contentLength=-1, date=-1, ...), the 2 real headers are gone

Fix

PageableSpringQueryMapEncoder already special-cases Pageable/Sort instead of falling through to the generic bean-reflection encoder. This adds the same special-case for HttpHeaders, using its own forEach(BiConsumer<String, List<String>>) to build the query map directly — mirroring the pattern already established for Pageable/Sort in this class.

Test

Added PageableSpringQueryMapEncoderTests#testHttpHeadersRequest, which fails against the old code (asserts a 2-entry map with the real header values) and passes with the fix. Verified locally:

  • Targeted module test suite (spring-cloud-openfeign-core, 441 tests) passes with the change.
  • ./mvnw validate (checkstyle + spring-javaformat) passes with 0 violations.

Verification done: reproduced the exact symptom from gh-1328 locally against current main (confirmed PageableSpringQueryMapEncoder — the actual default QueryMapEncoder bean SCOF wires up — reflects HttpHeaders via BeanQueryMapEncoder and drops all real header values); confirmed no in-flight PR/linked branch on the issue; confirmed no AI-contribution policy restriction in CONTRIBUTING.md; new regression test fails on pre-fix code and passes post-fix; full module test suite green; checkstyle/javaformat clean.

Disclosure: this change was authored with AI assistance (Claude Code), with the root cause independently traced and verified against the current codebase and the fix manually reviewed before submission.

Since Spring Framework 7, HttpHeaders no longer implements the
MultiValueMap contract. A @RequestHeader HttpHeaders parameter is
resolved through Feign's generic headerMapIndex path, which falls
back to the configured QueryMapEncoder whenever the argument isn't a
java.util.Map. That reflects over HttpHeaders' JavaBeans getters via
BeanQueryMapEncoder, dropping every real header value and injecting
unrelated getter-derived entries (contentLength=-1, date=-1, etc.)
instead.

Special-case HttpHeaders in PageableSpringQueryMapEncoder, the
QueryMapEncoder bean Spring Cloud OpenFeign already registers by
default, mirroring the existing Pageable/Sort handling.

Fixes spring-cloudgh-1328

Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
@ryanjbaxter
ryanjbaxter merged commit d0fce2a into spring-cloud:main Sep 9, 2026
2 checks passed
@ryanjbaxter

Copy link
Copy Markdown
Contributor

Cherry picked to 5.0.x as well

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.

Spring Boot 4 upgrade issue - use of Feign RequestInterceptor changed how headers are presented in API

3 participants