Skip to content

[type:bug] retry admin swagger pull at the backend root when the registered context path returns 404 - #8480

Merged
Aias00 merged 1 commit into
apache:masterfrom
markjoin123:cursor/fix-swagger-context-path-404-bd61
Nov 17, 2026
Merged

Aias00 merged 1 commit into
apache:masterfrom
markjoin123:cursor/fix-swagger-context-path-404-bd61

Conversation

@markjoin123

Copy link
Copy Markdown
Contributor

Fixes #8410

Why prefixed first, then root on 404

Admin still requests http://ip:port{contextPath}/v3/api-docs first. A backend whose server.servlet.context-path matches the registered context path keeps succeeding on that first request, with no extra call and no change to which document is imported when both URLs exist.

Only HTTP 404 triggers one retry at http://ip:port/v3/api-docs. That covers a registered context path that is only a gateway prefix (for example /mcp) stripped before forwarding, while the backend serves its OpenAPI document at the root. The retry is skipped when contextPath is empty or /, because that first URL is already the root document. 401, 5xx, and I/O errors are not retried.

An empty context path has no cluster name. The pull lock uses an empty key in that case so the single root request can run.

Tests

./mvnw -pl shenyu-admin -am test -Dtest=SwaggerDocManagerImplTest -Dsurefire.failIfNoSpecifiedTests=false

SwaggerDocManagerImplTest: 12 tests, 0 failures. Checkstyle ran during validate and reported 0 violations. ./mvnw -pl shenyu-admin apache-rat:check passed (unapproved 0).

Covered:

  • prefixed /mcp/v3/api-docs returns 200 and is imported, with no root request
  • prefixed 404, then root 200, and the root document is imported
  • prefixed and root both 404, nothing imported, exactly one retry
  • 404 with an empty context path and with /, one request only
  • 401 and 500, no retry
  • I/O error on the prefixed request, no retry

Existing lock tests in the same class still pass, including the HTTP 503 case that must not fall back.

This change was made with AI assistance and reviewed.

Keep requesting /v3/api-docs under the registered context path first, so a
real servlet context-path still succeeds. When that request returns 404 and
the context path is a gateway prefix, retry the backend root document once.
Empty or "/" context paths are not retried, and 401, 5xx, and I/O failures
are not either.

Fixes apache#8410

Co-authored-by: markxu <[email protected]>

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream Agent analysis

PR #8480 fixes admin swagger pull to retry at the backend root when the registered context-path URL returns HTTP 404, while still preferring the prefixed URL first. PullSwaggerDocServiceImpl now uses an injectable instance HttpUtils, a null-safe interner lock, and a new openSwaggerResponse retry path; SwaggerDocManagerImplTest adds comprehensive coverage for the new behavior.

Evidence

  • Complete VERSIONED_DIFF read (3 pages, total 16672 chars, has_more=false) for PullSwaggerDocServiceImpl.java (+48/-6) and SwaggerDocManagerImplTest.java (+210/-0).
  • CI: 74 checks complete, all success (e2e, CodeQL, e2e-cases), passed=true, failed_checks=[]
  • Tests cover prefixed-first, 404 root retry, both-404 give up, empty/root context no-retry, non-404 no-retry, and IOException no-retry paths.

Recommended next steps

Scope: captured diff and GitHub check metadata only; no local tests were executed.

Task: 2bc6a23e-d6e6-40fb-b455-ec5b6af4491b · Commit: 03864e3ddc0a0f8742529c82c1fcc0e84afd885e

Review verdict: PASS

Full diff coverage achieved; CI passed with no failed checks; the retry logic only triggers on HTTP 404 with a non-empty non-root context path, preserving prefixed-first behavior and adding null-safe locking plus test coverage, with no concrete blocking defect found in the diff.

Publication: APPROVE (EXPLICIT_PASS)

@wy471x wy471x left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: retry the swagger pull at the backend root on 404

Verdict: Approve — the fix is correct and well tested; one policy question should be answered before merge (see below).

What I verified

  • Only HTTP 404 triggers the extra request: the prefixed response is closed first, then the root URL is requested once. 401, 5xx and IOException propagate through the original path with no retry.
  • No double import: there is still exactly one addDocInfo call per pull, and the doc lock / refresh time bookkeeping is unchanged on both the success and the give-up paths.
  • Empty or root context path ("", "/", null → "" via StringUtils.defaultString) collapses to the root URL, so the retry predicate correctly suppresses a duplicate request. I also checked UriComponentsBuilder.path() joining: "", "/", and "/mcp/" all produce a single slash (http://host:port/v3/api-docs, http://host:port/mcp/v3/api-docs), so the @ValueSource({"", "/"}) assertion is sound and no extra slash normalization is needed.

Policy question (please resolve before merge)

#8410's "Proposed compatible fix" says: "Request /v3/api-docs at the upstream root first. Only on HTTP 404, retry the URL prefixed with the registered context path." This PR does the opposite order (prefixed first, root on 404) and argues in the description that a backend whose server.servlet.context-path really matches keeps succeeding on the first call. The trade-off is real, and the inverse order has a failure mode worth naming: a backend that answers 200 with something other than the OpenAPI document under the registered prefix (a UI page, another service behind the same path) would have that body imported, whereas root-first would have selected the correct document — which is exactly the ambiguity the issue resolves in favor of the root. Either implement the order the issue states, or record the deliberate deviation on #8410 so the next reader does not treat it as a bug. There is also no test for the "both locations serve 200" case; adding one makes the chosen policy explicit and regression-proof.

Standards / nits

  1. PullSwaggerDocServiceImpl#openSwaggerResponse returns a second value through a caller-supplied StringBuilder url out-parameter. It works, but the same fix reads better by returning a small holder (response + url) or by computing the URL first and logging it at the call site. url.setLength(0) at the top of openSwaggerResponse is dead on the freshly created builder.
  2. buildSwaggerRequestUrl(instance, boolean includeContextPath) is a flag argument whose two wrappers differ by one line; passing the prefix explicitly would say the same thing without the boolean.
  3. The double-404 path logs only the root URL (the buffer was reset), and the successful fallback logs nothing at all. A single LOG.info/debug naming both attempted URLs would make "imported from the root because the prefixed path 404'd" visible to operators.
  4. private HttpUtils httpUtils = new HttpUtils() replaced private static final HttpUtils HTTP_UTILS purely so a test can inject it. If injection is the reason, prefer a constructor parameter (or an existing seam) over a mutable field, since the field changes OkHttpClient ownership from one shared instance to one per bean.
  5. testPullApiDocumentDoesNotRetryRootWhenPrefixedRequestFails asserts times(1) on the prefixed URL only, which would still pass if a root request were issued. It cannot fail today because the mock throws rather than returning 404, but verifyNoMoreInteractions(httpUtils) (or asserting on the recorded URLs, as the other tests do) states the intent properly.
  6. testPullApiDocumentGivesUpWhenPrefixedAndRootSwaggerAreNotFound passes a throwaway new ArrayList<>(), so the closure of both responses is unchecked on the one path where closing before the retry matters. Use the same closed list plus assertAllClosed(closed).
  7. stubSwagger signals "unexpected extra request" by throwing IOException from the answer, which the production code swallows as a pull failure. Asserting on the recorded URL list (already done in most tests) is enough; the throw is a covert assertion. assertPullFinishedWithoutImport also asserts updateTagExt was called twice, an internal write count rather than observable behaviour.
  8. Consider a case with contextPath == null; production can produce it, and it takes the StringUtils.isNotEmpty false branch.

None of these block the fix; items 1-2 and the missing "both 200" test are what I would most like to see.

@Aias00
Aias00 merged commit 1ef9eb9 into apache:master Nov 17, 2026
74 checks passed
Aias00 pushed a commit that referenced this pull request Nov 19, 2026
Follow-up test for merged #8480 / #8410. When the registered context path
and the backend root both serve /v3/api-docs, import the prefixed document
and do not request the root.

Co-authored-by: markxu <[email protected]>
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.

[BUG] Admin Swagger pull assumes registered contextPath is the backend servlet path

3 participants