Skip to content

Commit a97b0d4

Browse files
committed
fix: Default basePath to / when server URL has no path
URI.create("http://127.0.0.1:4444").getPath() returns an empty string, not null, so the previous Optional.ofNullable fallback left basePath empty and HttpServer.createContext rejected it.
1 parent ffb8af1 commit a97b0d4

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/main/java/com/retailsvc/http/spec/Spec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ private static String computeBasePath(List<Server> servers) {
245245
if (servers.isEmpty()) {
246246
throw new IllegalStateException("no servers declared");
247247
}
248-
return Optional.ofNullable(URI.create(servers.getFirst().url()).getPath()).orElse("");
248+
String path = URI.create(servers.getFirst().url()).getPath();
249+
return (path == null || path.isEmpty()) ? "/" : path;
249250
}
250251

251252
private static <T> Map<String, T> indexByRef(Map<String, T> components, String prefix) {

src/test/java/com/retailsvc/http/spec/SpecTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ void parsesMinimalSpec() {
3333
assertThat(spec.operations()).isEmpty();
3434
}
3535

36+
@Test
37+
void basePathDefaultsToRootWhenServerUrlHasNoPath() {
38+
Map<String, Object> raw =
39+
Map.of(
40+
"openapi", "3.1.0",
41+
"info", Map.of("title", "x", "version", "1"),
42+
"servers", List.of(Map.of("url", "http://127.0.0.1:4444")),
43+
"paths", Map.of());
44+
Spec spec = Spec.from(raw);
45+
assertThat(spec.basePath()).isEqualTo("/");
46+
}
47+
3648
@Test
3749
void parsesPathsWithMethods() {
3850
Map<String, Object> raw =

0 commit comments

Comments
 (0)