Skip to content

Commit a17aef7

Browse files
committed
feat: Add Request.pathParam(name) convenience accessor
Mirrors Request.queryParam(name); avoids the round-trip through pathParams().get(name) for the common single-lookup case.
1 parent 42ac911 commit a17aef7

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class PostDataHandler implements RequestHandler {
4444
// Or get the already-parsed object (Map / List) produced by the registered TypeMapper.
4545
Object parsed = request.parsed();
4646
// Path parameters, query parameters, and headers are also available.
47-
String id = request.pathParams().get("id");
47+
String id = request.pathParam("id");
4848
String filter = request.queryParam("filter");
4949
String corr = request.header("correlation-id");
5050

@@ -218,7 +218,7 @@ A handler in this setup is just business logic:
218218
public class GetPromotionHandler implements RequestHandler {
219219
@Override
220220
public Response handle(Request request) {
221-
String id = request.pathParams().get("id");
221+
String id = request.pathParam("id");
222222
String tenant = TENANT_ID.get();
223223
return promotionService
224224
.find(tenant, id)
@@ -257,7 +257,7 @@ public final class App {
257257
Spec spec = Spec.fromPath(Path.of("openapi.yaml")); // SnakeYAML parses the spec
258258

259259
RequestHandler getPromotion = req -> {
260-
String id = req.pathParams().get("id");
260+
String id = req.pathParam("id");
261261
return PromotionService.find(TENANT.get(), id) // uses bound tenant
262262
.<Response>map(p -> Response.of(HTTP_OK, p)) // 200 + JSON via Gson
263263
.orElseGet(() -> Response.status(HTTP_NOT_FOUND)); // 404, no body

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public Map<String, String> pathParams() {
4949
return pathParameters;
5050
}
5151

52+
/** Value of the path parameter {@code name}, or {@code null} if absent. */
53+
public String pathParam(String name) {
54+
return pathParameters.get(name);
55+
}
56+
5257
public String header(String name) {
5358
return exchange.getRequestHeaders().getFirst(name);
5459
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ void readsBoundContext() throws Exception {
4141
assertThat(seenPathParams.get()).containsEntry("id", "42");
4242
}
4343

44+
@Test
45+
void pathParamReturnsValueOrNull() {
46+
HttpExchange exchange = mock(HttpExchange.class);
47+
Request req = new Request(exchange, new byte[0], null, "op", Map.of("id", "42"));
48+
49+
assertThat(req.pathParam("id")).isEqualTo("42");
50+
assertThat(req.pathParam("missing")).isNull();
51+
}
52+
4453
@Test
4554
void exposesQueryParams() {
4655
HttpExchange exchange = mock(HttpExchange.class);

0 commit comments

Comments
 (0)