Skip to content

Keep the host URL path prefix in the generated API client - #38

Open
DzmitryHumianiuk wants to merge 1 commit into
masterfrom
fix/host-path-prefix
Open

DzmitryHumianiuk wants to merge 1 commit into
masterfrom
fix/host-path-prefix

Conversation

@DzmitryHumianiuk

Copy link
Copy Markdown
Member

Summary

Fixes #37

With a host such as https://srv/reportportal, the generated OpenAPI client requested /api/v1/... while the Resty clients requested /reportportal/api/.... gorp launch list (without filters) and gorp quality-gate check therefore missed a ReportPortal served under a path prefix, while reporting worked. Both clients now resolve the same URLs for the same host.

Changes

  • pkg/gorp/client.go (newAPIClient): prepend the escaped path of the host URL, with trailing slashes trimmed, to every server URL of the generated configuration. A host without a path (or with /) leaves the configuration as it was.
  • pkg/gorp/base_path_test.go: table tests against an httptest server that serves only under <prefix>/api/ and records the request paths.
    • TestClientKeepsHostPathPrefix: LaunchAPI.GetProjectLaunches, LaunchAPI.GetLaunch (generated) and GetLaunchesByFilterString (Resty) on one Client.
    • TestReportingClientKeepsHostPathPrefix: ReportingClient.StartLaunch.
    • Hosts covered: no path, /, /reportportal, /reportportal/, /reportportal//, /a/b/, /report%20portal/.
  • No change in pkg/openapi, the spec or scripts/openapi-preprocess.jq; nothing was regenerated. The server URL /api still comes from the spec.

Before the fix, 5 of the 7 TestClientKeepsHostPathPrefix cases fail (every host with a path prefix). The two cases without a prefix and all TestReportingClientKeepsHostPathPrefix cases passed before as well: they pin the behaviour that was already right, so the two clients stay consistent.

Lint: task lint needs Docker, which is not running, so the pinned version was run without it: go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.15.0 run ./... reports 0 issues. Not run: task e2e, and the CLI commands themselves against a prefixed server (the tests cover the library calls they make).

Investigation log

All commands run from the repository root; line numbers are for this branch.

  1. Where the generated client gets its URL.
    • sed -n 101,116p pkg/openapi/configuration.go: NewConfiguration sets Servers to one entry with URL: "/api"; OperationServers is empty.
    • grep -n localBasePath pkg/openapi/api_launch.go | head -2: each operation builds localBasePath + "/v1/{projectKey}/...", where localBasePath is cfg.ServerURLWithContext(...), i.e. /api.
    • sed -n 500,508p pkg/openapi/client.go: prepareRequest (starts at line 417) overrides only url.Host and url.Scheme from the configuration.
    • python3 -c "import json;print(json.load(open('reportportal-api-docs.json'))['servers'])" prints [{'url': '/api'}].
  2. How pkg/gorp builds the configuration.
    • pkg/gorp/client.go:89-103 (newAPIClient): before the fix it set only conf.Scheme and conf.Host; lines 94-101 are the fix.
    • grep -rn --include='*.go' -e NewConfiguration -e NewAPIClient . | grep -v '^./pkg/openapi': pkg/gorp/client.go is the only construction site.
  3. How the Resty clients build URLs.
    • pkg/gorp/client.go:79 SetBaseURL(u.String()) and pkg/gorp/reporting_client.go:27 SetBaseURL(host); request paths are absolute, e.g. pkg/gorp/launches.go:12 "/api/v1/{project}/launch".
    • Resty joins them as strings.TrimRight(baseURL, "/") + path (middleware.go:147 in $(go list -m -f '{{.Dir}}' resty.dev/v3)), so the prefix is kept and trailing slashes are dropped.
  4. Which CLI commands use the generated client.
    • grep -rn --include='*.go' 'API\.' internal: only internal/commands/launch.go:141 (GetProjectLaunches, the branch without filters) and internal/commands/quality_gate.go:140 (GetLaunch).
    • internal/commands/commands.go:172-182 passes the parsed host unchanged to gorp.NewClient.
  5. Reproduce (tests on the code before the fix).
    • git stash is not needed: git diff HEAD~1 HEAD -- pkg/gorp/client.go > /tmp/fix.patch && git apply -R /tmp/fix.patch
    • go test ./pkg/gorp/ -run KeepsHostPathPrefix -v
    • Result: TestClientKeepsHostPathPrefix fails for prefix, prefix_with_trailing_slash, prefix_with_trailing_slashes, nested_prefix, escaped_prefix, e.g. expected GET /reportportal/api/v1/prj/launch, GET /reportportal/api/v1/prj/launch/42, GET /reportportal/api/v1/prj/launch; actual GET /api/v1/prj/launch, GET /api/v1/prj/launch/42, GET /reportportal/api/v1/prj/launch (the third request is Resty). no_path, root_path and all TestReportingClientKeepsHostPathPrefix cases pass.
    • git apply /tmp/fix.patch to restore the fix.
  6. Verify the fix.
    • go test ./pkg/gorp/ -run KeepsHostPathPrefix -v: all 14 subtests pass.
    • go build ./... && go vet ./... && go test ./...: ok for internal/commands and pkg/gorp.
    • go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.15.0 run ./...: 0 issues.

🤖 Generated with Claude Code

With a host such as https://srv/reportportal the generated OpenAPI client
requested /api/v1/... while the Resty clients requested
/reportportal/api/..., so 'gorp launch list' (without filters) and
'gorp quality-gate check' missed a ReportPortal served under a path prefix.

newAPIClient copied only scheme and host into the configuration; the path
came from the fixed server URL of the spec (/api). Prepend the escaped,
slash-trimmed path of the host URL to the configured server URLs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

Generated API client drops the path prefix of the host URL (launch list, quality-gate check behind a reverse proxy)

2 participants