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

Allow null argument for ResponseEntity.DefaultBuilder.eTag(String etag) #28947

Closed
abremora opened this issue Aug 10, 2022 · 0 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@abremora
Copy link

Affects: v5.3.22

Description

Building a response with etags can result in a NullPointerException if the argument for etag is null. Surprisingly, HttpHeaders.setETag(@Nullable String etag) handles null arguments and is called in ResponseEntity.DefaultBuilder.eTag(String etag).

Sample Code

    var response = ResponseEntity
        .ok()
        .eTag(null)
        .body(body);

Effected Code

public BodyBuilder eTag(String etag) {
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
this.headers.setETag(etag);
return this;
}

public void setETag(@Nullable String etag) {
if (etag != null) {
Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/"),
"Invalid ETag: does not start with W/ or \"");
Assert.isTrue(etag.endsWith("\""), "Invalid ETag: does not end with \"");
set(ETAG, etag);
}
else {
remove(ETAG);
}
}

Solution

  • Allow null strings
  • remove etag for null strings (like HttpHeaders.setETag() already does)

Therefore it should be safe just to call HttpHeaders.setETag() for null values:

public BodyBuilder eTag(String etag) {
	if (etag != null && !etag.startsWith("\"") && !etag.startsWith("W/\"")) {
		etag = "\"" + etag;
	}
	if (etag != null && !etag.endsWith("\"")) {
		etag = etag + "\"";
	}
	this.headers.setETag(etag);
	return this;
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 10, 2022
@poutsma poutsma self-assigned this Aug 16, 2022
@poutsma poutsma added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Aug 16, 2022
@poutsma poutsma added this to the 6.0.0-M6 milestone Aug 16, 2022
@poutsma poutsma removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 17, 2022
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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants