Skip to content

Commit 0840082

Browse files
committed
feat!: Drop Response.created(body, location) overload
Location had a dedicated parameter slot; every other header went through withHeader. A real footgun — callers reached for Response.created(body) and forgot the Location header. Forcing .withHeader("Location", uri) for symmetry removes the trap.
1 parent 8961a41 commit 0840082

3 files changed

Lines changed: 8 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ Response.empty(); // 204 No Content, no body
6666
Response.status(200); // 200 OK, no body
6767
Response.ok(Map.of("id", "42")); // 200 OK, JSON body via TypeMapper
6868
Response.created(newResource); // 201 Created, JSON body
69-
Response.created(newResource, "/things/42"); // 201 Created + Location header
69+
Response.created(newResource)
70+
.withHeader("Location", "/things/42"); // 201 Created + Location header
7071
Response.accepted(); // 202 Accepted, no body
7172
Response.accepted(Map.of("jobId", "job-42")); // 202 Accepted, JSON body
7273
Response.notFound(); // 404 Not Found, no body

src/main/java/com/retailsvc/http/Response.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ public static Response ok(Object body) {
5555
return new Response(HTTP_OK, body, null, Map.of());
5656
}
5757

58-
/** {@code 201 Created} with {@code body} serialised as JSON. */
59-
public static Response created(Object body) {
60-
return new Response(HTTP_CREATED, body, null, Map.of());
61-
}
62-
6358
/**
64-
* {@code 201 Created} with {@code body} as JSON and a {@code Location} header the canonical
65-
* shape for a POST that creates a new resource.
59+
* {@code 201 Created} with {@code body} serialised as JSON. Add a {@code Location} header for the
60+
* new resource via {@link #withHeader(String, String) withHeader("Location", uri)}.
6661
*/
67-
public static Response created(Object body, String location) {
68-
return new Response(HTTP_CREATED, body, null, Map.of("Location", location));
62+
public static Response created(Object body) {
63+
return new Response(HTTP_CREATED, body, null, Map.of());
6964
}
7065

7166
/** {@code 202 Accepted} with no body. Use for fire-and-forget async work. */

src/test/java/com/retailsvc/http/ResponseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void createdWithBody() {
4040
}
4141

4242
@Test
43-
void createdWithLocation() {
44-
Response r = Response.created(Map.of("id", "x-1"), "/things/x-1");
43+
void createdWithLocationViaWithHeader() {
44+
Response r = Response.created(Map.of("id", "x-1")).withHeader("Location", "/things/x-1");
4545

4646
assertThat(r.status()).isEqualTo(HTTP_CREATED);
4747
assertThat(r.headers()).containsEntry("Location", "/things/x-1");

0 commit comments

Comments
 (0)