Repository navigation
[type:bug] retry admin swagger pull at the backend root when the registered context path returns 404 - #8480
Conversation
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
IOExceptionpropagate through the original path with no retry. - No double import: there is still exactly one
addDocInfocall 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→""viaStringUtils.defaultString) collapses to the root URL, so the retry predicate correctly suppresses a duplicate request. I also checkedUriComponentsBuilder.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
PullSwaggerDocServiceImpl#openSwaggerResponsereturns a second value through a caller-suppliedStringBuilder urlout-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 ofopenSwaggerResponseis dead on the freshly created builder.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.- 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/debugnaming both attempted URLs would make "imported from the root because the prefixed path 404'd" visible to operators. private HttpUtils httpUtils = new HttpUtils()replacedprivate static final HttpUtils HTTP_UTILSpurely 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 changesOkHttpClientownership from one shared instance to one per bean.testPullApiDocumentDoesNotRetryRootWhenPrefixedRequestFailsassertstimes(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, butverifyNoMoreInteractions(httpUtils)(or asserting on the recorded URLs, as the other tests do) states the intent properly.testPullApiDocumentGivesUpWhenPrefixedAndRootSwaggerAreNotFoundpasses a throwawaynew ArrayList<>(), so the closure of both responses is unchecked on the one path where closing before the retry matters. Use the sameclosedlist plusassertAllClosed(closed).stubSwaggersignals "unexpected extra request" by throwingIOExceptionfrom 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.assertPullFinishedWithoutImportalso assertsupdateTagExtwas called twice, an internal write count rather than observable behaviour.- Consider a case with
contextPath == null; production can produce it, and it takes theStringUtils.isNotEmptyfalse branch.
None of these block the fix; items 1-2 and the missing "both 200" test are what I would most like to see.
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]>
Fixes #8410
Why prefixed first, then root on 404
Admin still requests
http://ip:port{contextPath}/v3/api-docsfirst. A backend whoseserver.servlet.context-pathmatches 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 whencontextPathis 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=falseSwaggerDocManagerImplTest: 12 tests, 0 failures. Checkstyle ran during validate and reported 0 violations.
./mvnw -pl shenyu-admin apache-rat:checkpassed (unapproved 0).Covered:
/mcp/v3/api-docsreturns 200 and is imported, with no root request/, one request onlyExisting 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.