Skip to content

Conversation

@ericfitz
Copy link
Contributor

@ericfitz ericfitz commented Nov 29, 2025

Summary

Fixes a bug where the CLI --server parameter is ignored when the OpenAPI specification contains a concrete server URL.

Problem

When a user provides both:

  • CLI parameter: --server=http://localhost:8080
  • OpenAPI spec with: https://api.example.com

Current behavior (bug): Uses https://api.example.com (CLI ignored)
Expected behavior: Uses http://localhost:8080 (CLI takes priority)

The existing behavior works correctly for:

  • Placeholder servers: {apiRoot}/v2 - CLI replaces placeholder ✅
  • Relative paths: /api/v1 - CLI gets prepended ✅
  • Concrete URLs: https://api.example.com - CLI ignored ❌ BUG

Solution

Refactored ApiArguments.validateValidServer() to handle three scenarios:

  1. No CLI server provided: Use OpenAPI server (existing behavior)
  2. CLI server + OpenAPI server with placeholders/relative paths: Use OpenAPI server for variable replacement (existing behavior)
  3. CLI server + concrete OpenAPI URL: Use CLI server (NEW - fixes the bug)

File: src/main/java/com/endava/cats/args/ApiArguments.java

Changes:

  • Split logic into two branches based on whether CLI --server is provided
  • When CLI server exists, check if OpenAPI server has placeholders ({) or relative paths (doesn't start with http)
  • Concrete OpenAPI URLs no longer override CLI server parameter
  • Improved debug logging
 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() in ApiArgumentsTest.java to verify CLI server takes priority over concrete OpenAPI URLs.

Behavior Matrix After Fix

CLI Server OpenAPI Server Result Status
http://localhost:8080 https://api.example.com http://localhost:8080 FIXED
http://api.com {apiRoot}/v2 http://api.com/v2 (via replacement) Behavior unchanged
http://api.com /v1 /v1 (relative path used) Behavior unchanged
null https://api.example.com https://api.example.com Behavior unchanged
null null Error Behavior unchanged

Files Changed

  • src/main/java/com/endava/cats/args/ApiArguments.java - 17 lines added, 2 removed
  • src/test/java/com/endava/cats/args/ApiArgumentsTest.java - 18 lines added (new test)

Checklist

  • Added tests for the bug fix
  • Preserves all existing behavior for placeholders and relative paths
  • Follows IntelliJ IDEA coding style
  • Test suite passes

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

When both CLI --server and OpenAPI spec contain server URLs, the CLI
parameter should take precedence. Previously, concrete OpenAPI server
URLs would override the CLI parameter, which was unexpected behavior.

This fix ensures:
- CLI --server always has highest priority when it's a concrete URL
- OpenAPI servers with placeholders (e.g., {apiRoot}/v2) are used for
  template replacement when CLI --server is provided
- OpenAPI servers with relative paths are combined with CLI --server
- OpenAPI servers only used as-is when CLI --server not provided

Changes:
- ApiArguments.java: Enhanced server priority logic to distinguish
  between placeholder/relative URLs and concrete URLs
- ApiArgumentsTest.java: Includes regression test for CLI server priority
  and placeholder replacement

Test Results:
- All 11 tests in ApiArgumentsTest passing
- Verified placeholder replacement: {apiRoot}/v2 + http://api.com = http://api.com/v2
- Verified CLI priority: CLI http://localhost:8080 overrides OpenAPI https://api.example.com

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@en-milie en-milie merged commit 33dafa7 into Endava:master Dec 4, 2025
@ericfitz ericfitz deleted the fix/cli-server-priority branch December 5, 2025 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants