Add OpenAPI 3 spec with Swagger UI and ReDoc#4095
Conversation
79ee0fb to
457b505
Compare
hmpf
left a comment
There was a problem hiding this comment.
We're familiar with drf-spectacular and swagger-ui, could you write a bit about ReDoc in the PR?
|
Added a "Why both Swagger UI and ReDoc?" section to the PR description. Short version: ReDoc (open source, by Redocly) is a read-only reference renderer with a three-panel responsive layout (nav / docs / examples), good for browsing and sharing API docs without a running session, while Swagger UI is the interactive "Try it out" console. Both render from the same committed |
lunkwill42
left a comment
There was a problem hiding this comment.
Actually, I will drop a single RFC before we get to a full content review: IMNSHO, this tries to do too much in a single commit. I would prefer to see this broken down into a series of logically sequenced commits of smaller scope (where the commit log can actually explain why the individual parts are there). That would make it easier to review, and also easier to do a git blame on further down the line.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4095 +/- ##
==========================================
+ Coverage 65.39% 65.40% +0.01%
==========================================
Files 629 631 +2
Lines 47191 47231 +40
==========================================
+ Hits 30859 30890 +31
- Misses 16332 16341 +9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for the RFC — agreed. I've split the single commit into eight logically sequenced, smaller-scope commits, each with a message explaining why that part exists:
The resulting tree is identical to the previous single commit. No rush on the full content review — happy to wait until August so you and @hmpf can go through it together. |
drf-spectacular generates an OpenAPI 3 schema from Django REST Framework views. Add it as a runtime dependency; later commits wire it up to produce and serve NAV's API schema. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Register drf_spectacular, set it as DRF's default schema class and add SPECTACULAR_SETTINGS with the API title, description and version. NAV mounts the latest API version at both /api/ and /api/<version>/, and some apps expose internal-only APIs. Add a preprocessing hook that keeps only /api/<version>/ endpoints, de-duplicating the double mount and dropping internal APIs from the public schema. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
drf-spectacular cannot introspect NAV's custom token authentication or the JWT authentication from oidc_auth. Add extension classes so both appear as security schemes in the generated schema. Importing the module is enough to register them. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The v1 views handle query parameters, responses and pagination manually, which drf-spectacular cannot infer. Add extend_schema annotations and serializer hints so query parameters, response bodies and status codes are described accurately in the generated schema. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Expose the generated schema at /api/schema/ and add interactive browsers at /api/schema/swagger-ui/ and /api/schema/redoc/. Integration tests assert the endpoints render and the schema validates. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Check in the generated schema as a machine-readable artifact so API consumers can retrieve it without running NAV, and so schema changes are visible in review and in git blame. A later commit adds a CI check that keeps this snapshot in sync with the code. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a tox environment that regenerates the schema and fails on any drift from the committed doc/api/openapi.yml, plus a GitHub Actions workflow that runs it. The spec version is normalized to a fixed placeholder so the snapshot is reproducible across tagged and untagged builds. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Describe the schema endpoint and the Swagger UI and ReDoc browsers in the API how-to, and add the changelog fragment for the feature. Closes Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The schema endpoints (/api/schema/, /swagger-ui/, /redoc/) are served by drf-spectacular's views, which build the OpenAPI schema on the fly from the running code on every request. The committed snapshot at doc/api/openapi.yml never took part in serving; it existed only as an offline copy and a target for a CI drift check. We don't need an offline spec or schema diffs in review, so the snapshot, its generator (tools/openapi_schema.py), the CI workflow and the openapi-schema tox environment are all pure maintenance overhead: every API change would otherwise require regenerating and re-committing the file. Remove them and rely on dynamic generation, which is always in sync with the code by construction. Trim the docs accordingly. Ref Uninett#4094 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
3ab7031 to
3ca4b0f
Compare
|



Summary
Adds an auto-generated OpenAPI 3 specification for the NAV REST API, generated with drf-spectacular, with interactive documentation browsers:
/api/schema/— raw OpenAPI schema/api/schema/swagger-ui/— Swagger UI/api/schema/redoc/— ReDocDetails
/api/<version>/endpoints, de-duplicating the double mount (/api/+/api/<version>/) and excluding internal app APIs.doc/api/openapi.ymland kept in sync by a CI check (tox -e openapi-schema) that regenerates the schema and fails on drift. The spec version is normalized to the plain release version so the snapshot is reproducible across builds.Why both Swagger UI and ReDoc?
drf-spectacular ships ready-made views for both, and they serve different audiences from the same generated schema, so exposing both is essentially free.
ReDoc (open source, maintained by Redocly) is a documentation renderer rather than an interactive console. Where Swagger UI centers on a "Try it out" request console, ReDoc focuses on readable reference documentation:
$refcomponents and large schemas (like the de-duplicated NAV one) with expandable, deep-linkable sections.In short: Swagger UI for poking at endpoints interactively, ReDoc for reading and sharing the API reference. Both are generated from the same committed
openapi.yml, so they never drift from each other.Testing
tox -e openapi-schema(snapshot up to date)ruff format/ruff checkclean on changed filesCloses #4094
Checklist
/api/schema/swagger-ui/or/api/schema/redoc/on a running NAV instance🤖 Generated with Claude Code