diff --git a/src/main/java/com/endava/cats/args/ApiArguments.java b/src/main/java/com/endava/cats/args/ApiArguments.java index d30e2143..e2f33895 100644 --- a/src/main/java/com/endava/cats/args/ApiArguments.java +++ b/src/main/java/com/endava/cats/args/ApiArguments.java @@ -94,8 +94,21 @@ public void validateValidServer(CommandLine.Model.CommandSpec spec, OpenAPI open if (openAPI != null) { List servers = OpenApiServerExtractor.getServerUrls(openAPI); - log.debug("--server not provided. Loaded from OpenAPI: {}", servers); - servers.stream().findFirst().ifPresent(theServer -> this.server = theServer); + log.debug("Servers from OpenAPI: {}", servers); + + if (serverFromInput == null) { + // No CLI server provided, use OpenAPI server + servers.stream().findFirst().ifPresent(theServer -> this.server = theServer); + } else { + // CLI server provided, check if OpenAPI server has placeholders or relative paths + servers.stream().findFirst().ifPresent(openApiServer -> { + if (openApiServer.contains("{") || !openApiServer.startsWith("http")) { + // OpenAPI server has placeholders or is relative, use it for replacement + this.server = openApiServer; + } + // Otherwise keep CLI server (concrete OpenAPI URLs don't override CLI) + }); + } } if (this.server != null && serverFromInput != null) { diff --git a/src/test/java/com/endava/cats/args/ApiArgumentsTest.java b/src/test/java/com/endava/cats/args/ApiArgumentsTest.java index cdf03d3d..e760564e 100644 --- a/src/test/java/com/endava/cats/args/ApiArgumentsTest.java +++ b/src/test/java/com/endava/cats/args/ApiArgumentsTest.java @@ -98,6 +98,24 @@ void shouldReplaceServerPlaceholder() { Assertions.assertThat(args.getServer()).isEqualTo("http://api.com/v2"); } + @Test + void shouldPreferCliServerOverOpenApiConcreteServer() { + CommandLine.Model.CommandSpec spec = Mockito.mock(CommandLine.Model.CommandSpec.class); + Mockito.when(spec.commandLine()).thenReturn(Mockito.mock(CommandLine.class)); + ApiArguments args = new ApiArguments(); + args.setServer("http://localhost:8080"); + + OpenAPI openAPI = new OpenAPI(); + openAPI.setServers(Collections.singletonList( + new io.swagger.v3.oas.models.servers.Server().url("https://api.example.com") + )); + + args.validateValidServer(spec, openAPI); + + // CLI server should take priority over concrete OpenAPI server + Assertions.assertThat(args.getServer()).isEqualTo("http://localhost:8080"); + } + @Test void shouldThrowExceptionWhenServerIsInvalidUrl() { CommandLine.Model.CommandSpec spec = Mockito.mock(CommandLine.Model.CommandSpec.class);