Skip to content

refactor(openapi)!: make documentation endpoints explicit - #115

Merged
Kilerd merged 2 commits into
mainfrom
fix/openapi-assembly
Sep 14, 2026
Merged

Kilerd merged 2 commits into
mainfrom
fix/openapi-assembly

Conversation

@Kilerd

@Kilerd Kilerd commented Sep 14, 2026 •

Copy link
Copy Markdown
Owner

Enabling the openapi feature previously mounted /openapi.json, /redoc, and /scalar after business middleware, so those endpoints could bypass authentication. with_openapi() had no effect, and exporting the assembled document required an HTTP request.

Documentation endpoints are now disabled by default. with_openapi() explicitly enables the standard paths; OpenApiEndpoints configures the JSON path and optional UI paths through the builder or GotchaApp::openapi_endpoints. Endpoint settings belong to the application, and both UIs load the configured JSON URL. Invalid or duplicate endpoint paths fail assembly; conflicts with business routes follow Axum's existing behavior.

Both APIs use one assembly pipeline: business routes, optional documentation, then application middleware. Builder app_layer and trait finish_router cover documentation and fallback routes as well as business routes. Existing layer behavior remains scoped to routes already registered. Application layers are installed once, in Axum order, with the last registered layer receiving requests first.

Gotcha::into_openapi, GotchaRouter::into_openapi, and GotchaApp::openapi_document expose the complete document without loading configuration, initializing state, binding a listener, or registering tasks. Export and HTTP serving share schema generation and the existing FnOnce transform pipeline. When neither export nor HTTP documentation is requested, no document is generated and transforms do not run.

Migration: applications that previously relied on the feature alone must explicitly enable endpoints. Existing with_openapi() calls now do so. Move authentication to app_layer / finish_router when it must also protect documentation. The migration guide covers custom paths, disabled UIs, middleware order, and custom build_router overrides. Existing documentation tests and examples now opt in; the OpenAPI example adds --export-openapi. Remove the now-unused direct cfg-if dependency.

Validation:

  • Seven assembly tests cover both adapters, disabled/default/custom endpoints, UI URLs and escaping, middleware scope/order/fallback coverage, invalid configuration, export/HTTP byte equality, one-shot transforms, and export without a runtime or initialization. One unit test covers endpoint path validation.
  • cargo test -p gotcha and cargo test -p gotcha --all-features, including existing schema, response-contract, transform, startup, and doctest coverage.
  • Workspace all-features and gotcha default checks; workspace all-features Clippy with -D warnings; formatting and diff checks.
  • RUSTDOCFLAGS='-D warnings' cargo doc -p gotcha --all-features --no-deps.
  • Built and ran the OpenAPI example's export command with an invalid configuration file present: it exited successfully with 7 paths and 5 component schemas, without starting the application.

All 11 GitHub CI checks passed for 4b25e914e3abba446bd6dcd77898e9c757570c5c.

Closes #88.
Closes #77.

@Kilerd
Kilerd merged commit 90e0efe into main Sep 14, 2026
11 checks passed
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.

[P1][openapi] 分离文档生成与端点挂载,明确中间件覆盖范围 [openapi] The assembled spec is unreachable without an HTTP request

1 participant