Skip to content

Ignore null in HeadersBuilder#headers [SPR-12792] #17389

@spring-projects-issues

Description

@spring-projects-issues

Kazuki Shimizu opened SPR-12792 and commented

If specified null value as method argument of HeadersBuilder#headers, NullPointerException occur.

I want to change this behavior as follows:

  • Ignore a null value(the same as HttpEntity constructor)
  • Check a null value using Assert#notNull method

I want to apply a first idea(ignore a null value).

e.g)

private ResponseEntity<String> createResponseEntity(
        String body,
        HttpHeaders customHeaders) {
    return ResponseEntity
            .ok()
            .headers(customHeaders)
            .header("X-Track", UUID.randomUUID().toString())
            .body(body);
}

// ...

@Test
public void test() {
    System.out.println(createResponseEntity("test", new HttpHeaders()));
    System.out.println(createResponseEntity("test", null));
}
<200 OK,test,{X-Track=[2a7ed446-c805-42a6-ada7-982ff9d2b6d7]}>

java.lang.NullPointerException
	at org.springframework.util.LinkedCaseInsensitiveMap.putAll(LinkedCaseInsensitiveMap.java:101)
	at org.springframework.http.HttpHeaders.putAll(HttpHeaders.java:907)
	at org.springframework.http.ResponseEntity$DefaultBuilder.headers(ResponseEntity.java:384)
	at org.springframework.http.ResponseEntity$DefaultBuilder.headers(ResponseEntity.java:364)
	at ResponseEntityTest.createResponseEntity(ResponseEntityTest.java:33)
	at ResponseEntityTest.test(ResponseEntityTest.java:17)
        ...

Affects: 4.1.5

Referenced from: pull request #753

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions