Skip to content

Field 'host' in query parameters is automatically populated with HTTP 'Host' header after upgrading to 3.4.1 #43860

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

Closed
bartolcz opened this issue Jan 16, 2025 · 1 comment
Labels
status: duplicate A duplicate of another issue

Comments

@bartolcz
Copy link

bartolcz commented Jan 16, 2025

Hi,
I have encountered an issue with Spring Boot's data binding mechanism. When using a class in a controller method to bind query parameters, a field named host automatically gets populated with the value from the HTTP Host header if no parameter named host is provided in the request.

This behavior is unexpected because the host field should remain null if the parameter is not present in the request. Instead, the field is populated with the host value from the incoming request, such as localhost:8080.

Steps to Reproduce

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class InstancesFilterCriteria {
    private String host;
    private String name;
// and many more 
}
@GetMapping("/api/instances")
public ResponseEntity<?> getInstances(@Valid InstancesFilterCriteria filterCriteria) {
    return ResponseEntity.ok(filterCriteria.getHost());
}

http call
GET /api/instances?name=MyServer

Expected Behavior
The host field should remain null when the parameter is not provided in the request.

Actual Behavior
The host field is automatically populated with the value of the HTTP Host header (localhost:8080), even though the parameter is not present in the request.

I recently migrated from Spring Boot 2.7.18 to 3.4.1, and this issue started occurring after the migration.
What I want to achieve is control over my parameter naming, as the current implementation is highly integrated into the API and changing the naming convention is difficult to conclude at this stage.
Is there a way to configure Spring Boot to handle such naming collisions differently or prevent this automatic binding to the HTTP Host header?

I would greatly appreciate at least a brief explanation of this behavior and potential solutions.

Best Regards,
Bartek

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 16, 2025
@wilkinsona
Copy link
Member

Duplicates spring-projects/spring-framework#34182.

@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants