Skip to content

Add OpenAPI 3 spec with Swagger UI and ReDoc#4095

Open
adobloug wants to merge 9 commits into
Uninett:masterfrom
adobloug:drf-spectacular
Open

Add OpenAPI 3 spec with Swagger UI and ReDoc#4095
adobloug wants to merge 9 commits into
Uninett:masterfrom
adobloug:drf-spectacular

Conversation

@adobloug

@adobloug adobloug commented Jun 18, 2026

Copy link
Copy Markdown

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/ — ReDoc

Details

  • Views and serializers are annotated so manually-handled query parameters, responses and authentication schemes are reflected in the schema.
  • A preprocessing hook keeps only the versioned /api/<version>/ endpoints, de-duplicating the double mount (/api/ + /api/<version>/) and excluding internal app APIs.
  • Custom token auth and JWT auth are registered as OpenAPI security schemes.
  • A generated snapshot is committed at doc/api/openapi.yml and 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:

  • Three-panel responsive layout — navigation sidebar, prose documentation in the middle, and request/response examples plus payload samples on the right.
  • Reference-oriented, read-only — no live request execution by default, which makes it well suited for browsing, sharing and linking to API docs without a running/authenticated session.
  • Deep schema rendering — handles nested objects, $ref components and large schemas (like the de-duplicated NAV one) with expandable, deep-linkable sections.
  • Single self-contained page — easy to point external integrators at.

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 check clean on changed files
  • New integration tests assert the schema endpoints return 200 and the schema contains only versioned paths

Closes #4094

Checklist

  • Added a changelog fragment for towncrier
  • Added/amended tests for new/changed code
  • Added/changed documentation
  • Linted/formatted the code with ruff, easiest by using pre-commit
  • Wrote the commit message so that the first line continues the sentence "If applied, this commit will ...", starts with a capital letter, does not end with punctuation and is 50 characters or less long. See https://cbea.ms/git-commit/
  • Based this pull request on the correct upstream branch (master, since this is a new feature)
  • If applicable: Created new issues if this PR does not fix the issue completely/there is further work to be done — n/a, fully closes Publish an OpenAPI 3 specification with Swagger UI and ReDoc for the REST API #4094
  • Described how to interact with NAV to observe the effects: visit /api/schema/swagger-ui/ or /api/schema/redoc/ on a running NAV instance
  • If this results in changes in the UI: Added screenshots of the before and after — adds new Swagger UI / ReDoc pages; no "before" to compare against
  • If this adds a new Python source code file: Added the boilerplate header to that file

🤖 Generated with Claude Code

@adobloug adobloug changed the title Add OpenAPI 3 specification with Swagger UI and ReDoc Add OpenAPI 3 spec with Swagger UI and ReDoc Jun 19, 2026
@johannaengland
johannaengland requested a review from a team June 23, 2026 09:10

@hmpf hmpf 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.

We're familiar with drf-spectacular and swagger-ui, could you write a bit about ReDoc in the PR?

@hmpf hmpf added api documentation Related to documentation of NAV dependencies Pull requests that update a dependency file labels Jun 24, 2026
@hmpf
hmpf requested a review from a team June 24, 2026 11:27
@adobloug

adobloug commented Jun 29, 2026

Copy link
Copy Markdown
Author

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 openapi.yml, so they stay in sync.

@lunkwill42 lunkwill42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd simply say this supersedes my old attempt at the same in #3380 - I couldn't get anyone to review that unfinished piece of crap, and so it was forgotten.

I'll have a closer look at this, but probably not until August, when I can do it together with @hmpf

@lunkwill42 lunkwill42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.40%. Comparing base (dc68ca0) to head (212bc9c).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@adobloug

adobloug commented Jul 9, 2026

Copy link
Copy Markdown
Author

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:

  1. Add drf-spectacular dependency
  2. Enable OpenAPI schema generation
  3. Register API auth schemes in the schema
  4. Annotate API views and serializers for the schema
  5. Serve the API schema, Swagger UI and ReDoc
  6. Commit generated OpenAPI schema snapshot
  7. Keep the OpenAPI snapshot in sync via CI
  8. Document the API schema and add changelog

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.

adobloug and others added 9 commits July 10, 2026 08:50
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>
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api dependencies Pull requests that update a dependency file documentation Related to documentation of NAV

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Publish an OpenAPI 3 specification with Swagger UI and ReDoc for the REST API

4 participants