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

properties bound from request header to data class #34125

Closed
664623107 opened this issue Dec 20, 2024 · 6 comments
Closed

properties bound from request header to data class #34125

664623107 opened this issue Dec 20, 2024 · 6 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid

Comments

@664623107
Copy link

I found that when using Spring Boot 3.4.1, this version places the properties from the header into the request when handling web requests.

There is a property bu in the header of request.

code:

@GetMapping("/periodic-data")
fun periodicData(condition: MarketingBoardDataDtoV2) = marketingBoardV2Service.periodicData(condition)

data class MarketingBoardDataDtoV2(
    val category: String?,
    val subBrand: String?,
    val bu: String?,
    val startDate: LocalDate,
    val endDate: LocalDate,
)

request:

GET {{host}}/v2/marketing-board/periodic-data?startDate=2024-08-14&endDate=2024-12-14

in 3.4.1:
image

in <=3.4.0
image

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 20, 2024
@snicoll snicoll transferred this issue from spring-projects/spring-boot Dec 20, 2024
@bclozel bclozel added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Dec 20, 2024
@bclozel
Copy link
Member

bclozel commented Dec 20, 2024

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Dec 20, 2024
@bclozel bclozel changed the title I found that when using Spring Boot 3.4.1, this version places the properties from the header into the request when handling web requests. properties bound from request header to data class Dec 20, 2024
@664623107
Copy link
Author

664623107 commented Dec 20, 2024

Have you tried filtering this property in the binder as explained here?

@bclozel I added the property bu to the header of each request in the Filter.

override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain) {
    val httpServletRequest = request as HttpServletRequest
    // something ...
    request.setHeader(Constant.BU, "$bu")
    // something ...
}

But in the old version, there were no issues; I have always used it this way.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 20, 2024
@bclozel
Copy link
Member

bclozel commented Dec 20, 2024

@664623107 that is not what I was suggesting. Please try the code snippet suggested here, configuring the binder to ignore the "bu" header.

Here's a class you can copy to your project that will ignore all header values. Let us know how this works for you.

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder;

@ControllerAdvice
public class MyControllerAdvice {

	@InitBinder
	public void initBinder(ExtendedServletRequestDataBinder binder) {
		binder.addHeaderPredicate(header -> false);
	}
}

@bclozel bclozel added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Dec 20, 2024
@664623107
Copy link
Author

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder;

@ControllerAdvice
public class MyControllerAdvice {

	@InitBinder
	public void initBinder(ExtendedServletRequestDataBinder binder) {
		binder.addHeaderPredicate(header -> false);
	}
}

@bclozel Thanks,It worked.
But why were there no issues in versions <= 3.4.0?
Is this a bug?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 20, 2024
@bclozel
Copy link
Member

bclozel commented Dec 20, 2024

Thanks for letting us know. I think this is due to #34073, so not a bug. Your DTO class takes this argument in its constructor, which is a pretty strong signal for the binder to do the binding. I'll leave this opened for now to let Rossen comment on this, but I suspect that binding constructor arguments is the expected behavior here.

@bclozel
Copy link
Member

bclozel commented Jan 7, 2025

We have discussed this as a team, and we're going to refine this support in #34182.
As for this particular case, we think that this behavior change uncovered a problem in the design of your application. Rather than using request headers as a context map, you should be adding this information as a request attribute.

We're closing this issue in favor of #34182. You can keep using the code snippet I shared but please consider refactoring this part of your application. Thanks!

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
@bclozel bclozel added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants