Skip to content

Commit 8961a41

Browse files
committed
refactor!: Rename Builder.addHandler to extraRoute
Reads more honestly. "handlers" and "addHandler" sounded like the same concept but they aren't — handlers(Map) is operationId-keyed and validated against the OpenAPI spec; the other is path-keyed and bypasses validation. "extraRoute" makes that distinction visible at the call site: .handlers(handlerMap) // OpenAPI operations .extraRoute("/alive", Handlers.aliveHandler()) // side route
1 parent 337471c commit 8961a41

4 files changed

Lines changed: 16 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ to OpenAPI parameter / body validation.
364364
var server = OpenApiServer.builder()
365365
.spec(spec)
366366
.handlers(handlers)
367-
.addHandler("/alive", Handlers.aliveHandler())
368-
.addHandler("/schemas/v1/openapi.yaml",
367+
.extraRoute("/alive", Handlers.aliveHandler())
368+
.extraRoute("/schemas/v1/openapi.yaml",
369369
Handlers.specHandler("/schemas/v1/openapi.yaml"))
370370
.build();
371371
```

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,17 @@ public Builder shutdownTimeoutSeconds(int shutdownTimeoutSeconds) {
197197
return this;
198198
}
199199

200-
public Builder addHandler(String path, HttpHandler handler) {
200+
/**
201+
* Registers an extra HTTP route at {@code path} that bypasses OpenAPI validation and routing.
202+
* Use for side concerns like {@code /alive}, {@code /health}, or serving the spec itself —
203+
* anything that isn't an OpenAPI {@code operationId}. For OpenAPI-described operations use
204+
* {@link #handlers(Map)}.
205+
*/
206+
public Builder extraRoute(String path, HttpHandler handler) {
201207
requireNonNull(path, "path must not be null");
202208
requireNonNull(handler, "handler must not be null");
203209
if (extras.containsKey(path)) {
204-
throw new IllegalStateException("duplicate extra handler path: " + path);
210+
throw new IllegalStateException("duplicate extra route path: " + path);
205211
}
206212
extras.put(path, handler);
207213
return this;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void aliveExtraReturns204AndBypassesValidation() throws Exception {
1919
.handlers(Map.of())
2020
.exceptionHandler(defaultExceptionHandler())
2121
.port(0)
22-
.addHandler("/alive", Handlers.aliveHandler())
22+
.extraRoute("/alive", Handlers.aliveHandler())
2323
.build();
2424
var client = httpClient()) {
2525

@@ -43,7 +43,7 @@ void specHandlerServesClasspathResource() throws Exception {
4343
.handlers(Map.of())
4444
.exceptionHandler(defaultExceptionHandler())
4545
.port(0)
46-
.addHandler("/openapi.yaml", Handlers.specHandler("/openapi.yaml"))
46+
.extraRoute("/openapi.yaml", Handlers.specHandler("/openapi.yaml"))
4747
.build();
4848
var client = httpClient()) {
4949

@@ -73,7 +73,7 @@ void extraHandlerExceptionFlowsThroughExceptionHandler() throws Exception {
7373
.handlers(Map.of())
7474
.exceptionHandler(defaultExceptionHandler())
7575
.port(0)
76-
.addHandler("/boom", boom)
76+
.extraRoute("/boom", boom)
7777
.build();
7878
var client = httpClient()) {
7979

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ void buildsWithRequiredFieldsOnly() {
2828
void rejectsDuplicateExtraPathOnSecondAddHandler() {
2929
HttpHandler duplicate = Handlers.aliveHandler();
3030
OpenApiServer.Builder b =
31-
OpenApiServer.builder().spec(spec).handlers(emptyMap()).addHandler("/alive", duplicate);
31+
OpenApiServer.builder().spec(spec).handlers(emptyMap()).extraRoute("/alive", duplicate);
3232

33-
assertThatThrownBy(() -> b.addHandler("/alive", duplicate))
33+
assertThatThrownBy(() -> b.extraRoute("/alive", duplicate))
3434
.isInstanceOf(IllegalStateException.class)
3535
.hasMessageContaining("/alive");
3636
}
@@ -42,7 +42,7 @@ void rejectsExtraPathEqualToSpecBasePathAtBuildTime() {
4242
OpenApiServer.builder()
4343
.spec(spec)
4444
.handlers(emptyMap())
45-
.addHandler("/api", Handlers.aliveHandler())
45+
.extraRoute("/api", Handlers.aliveHandler())
4646
.port(0);
4747

4848
assertThatThrownBy(b::build)

0 commit comments

Comments
 (0)