fix: CLI --server parameter should take priority over OpenAPI servers #183
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where the CLI
--serverparameter is ignored when the OpenAPI specification contains a concrete server URL.Problem
When a user provides both:
--server=http://localhost:8080https://api.example.comCurrent behavior (bug): Uses
https://api.example.com(CLI ignored)Expected behavior: Uses
http://localhost:8080(CLI takes priority)The existing behavior works correctly for:
{apiRoot}/v2- CLI replaces placeholder ✅/api/v1- CLI gets prepended ✅https://api.example.com- CLI ignored ❌ BUGSolution
Refactored
ApiArguments.validateValidServer()to handle three scenarios:File:
src/main/java/com/endava/cats/args/ApiArguments.javaChanges:
--serveris provided{) or relative paths (doesn't start withhttp)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) + }); + } }Testing
Added regression test
shouldPreferCliServerOverOpenApiConcreteServer()inApiArgumentsTest.javato verify CLI server takes priority over concrete OpenAPI URLs.Behavior Matrix After Fix
http://localhost:8080https://api.example.comhttp://localhost:8080http://api.com{apiRoot}/v2http://api.com/v2(via replacement)http://api.com/v1/v1(relative path used)nullhttps://api.example.comhttps://api.example.comnullnullFiles Changed
src/main/java/com/endava/cats/args/ApiArguments.java- 17 lines added, 2 removedsrc/test/java/com/endava/cats/args/ApiArgumentsTest.java- 18 lines added (new test)Checklist
🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com