Skip to content

Improve nullability in DefaultResponseCreator#36969

Open
bjmi wants to merge 1 commit into
spring-projects:mainfrom
bjmi:nullable-DefaultResponseCreator
Open

Improve nullability in DefaultResponseCreator#36969
bjmi wants to merge 1 commit into
spring-projects:mainfrom
bjmi:nullable-DefaultResponseCreator

Conversation

@bjmi

@bjmi bjmi commented Jun 26, 2026

Copy link
Copy Markdown

In response to the call for improvements to Spring code regarding Jspecify Nulls are dead - Moritz Halbritter | IntelliJ IDEA Tech Talks here is an enhancement for DefaultResponseCreator encountered during writing tests.

The DefaultResponseCreator builder from spring-test unnecessarily restricts certain method parameters to non-nullable types although the nullable types are supported.
This just makes writing parameterized tests, for example, unnecessarily difficult.

Signed-off-by: Björn Michael <b.michael@gmx.de>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 26, 2026
@bjmi bjmi force-pushed the nullable-DefaultResponseCreator branch from c506ee0 to d34e472 Compare June 26, 2026 07:01
@sbrannen sbrannen added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jun 26, 2026
@sbrannen sbrannen requested a review from rstoyanchev June 26, 2026 08:35

@rstoyanchev rstoyanchev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpHeaders does allow null values as a way to remove. On the other hand DefaultResponseCreator is used to build a response and is therefore additive, and flagging null is potentially useful. Could you provide a sketch of a sample test class that shows a common scenario?

Broadly, we have many similar facade APIs over HttpHeaders -- Rest|WebTestClient, Rest|WebClient, functional server APIs, and others that should be considered too.

@bjmi

bjmi commented Jun 26, 2026

Copy link
Copy Markdown
Author

E.g. following parametrized test exercising the fetchStatus() depending on a nullable content-type.

Webservice webservice;  // using RestClient inside

@CsvSource({
	"https://example.org/status.txt , text/plain      ",
	"https://example.org/status.txt ,                 ",
	"https://example.org/status.json, application/json", 
	"https://example.org/status.json,                 ",
})
@ParameterizedTest(name = "{arguments}")
void fetchStatus(URI url, @Nullable MediaType mediaType) {
	mockServer.expect(requestTo(url))
			.andRespond(withSuccess().contentType(mediaType));

	SomeStatus result = webservice.fetchStatus(url);

	assertThat(result).isEqualTo(SomeStatus.OK);
	mockServer.verify();
}

Personally, I find builders useful that allow to unset properties again when they aren't needed.
It allows to create a DefaultResponseCreator with resonable defaults for tests that can be changed easily if the defaults doesn't fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants