Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/main/java/com/endava/cats/args/ApiArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,21 @@ public void validateValidServer(CommandLine.Model.CommandSpec spec, OpenAPI open

if (openAPI != null) {
List<String> 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) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/endava/cats/args/ApiArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down