E52F feat: Add custom dark/light theme to Swagger UI by HarshitVerma109 · Pull Request #4237 · OWASP/Nest · GitHub
[go: up one dir, main page]

Skip to content

feat: Add custom dark/light theme to Swagger UI#4237

Open
HarshitVerma109 wants to merge 2 commits intoOWASP:mainfrom
HarshitVerma109:feat/swagger-ui-theme
Open

feat: Add custom dark/light theme to Swagger UI#4237
HarshitVerma109 wants to merge 2 commits intoOWASP:mainfrom
HarshitVerma109:feat/swagger-ui-theme

Conversation

@HarshitVerma109
Copy link
Contributor

Proposed change

Resolves #4197

Added a custom Dark/Light mode theme to the Swagger UI to perfectly match the OWASP Nest.

Checklist

  • Required: I followed the contributing workflow
  • Required: I verified that my code works as intended and resolves the issue as described
  • Required: I ran make check-test locally: all warnings addressed, tests passed
  • I used AI for code, documentation, tests, or communication related to this PR

@coderabbitai
Copy link
Contributor
coderabbitai bot commented Mar 10, 2026

Summary by CodeRabbit

  • New Features
    • Added an interactive API documentation page with a polished Swagger UI.
    • Introduced full light/dark theming with a visible theme toggle and persisted user preference.
    • Theme auto-detects and follows system color-scheme and updates live when the system or another tab changes.
    • Improved overall visual contrast and readability across documentation UI components.

Walkthrough

Adds a new Swagger UI template and a comprehensive theme stylesheet providing light/dark palettes, theme initialization/switching logic, CSRF handling, and granular UI styling for Swagger components.

Changes

Cohort / File(s) Summary
Swagger Theme Stylesheet
backend/static/css/swagger-theme.css
Adds a full Swagger UI theme with light and dark mode palettes, color and border overrides for buttons and UI regions, JSON/schema styling, scrollbar and modal adjustments, and contrast/opacity tuning.
Swagger UI Template
backend/templates/ninja/swagger.html
Adds a Django/Jinja2 template that loads static assets, injects theme initialization (localStorage + prefers-color-scheme), exposes setTheme, listens for storage and system theme changes, and embeds swagger settings and CSRF/data attributes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • arkid15r
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a custom dark/light theme to Swagger UI, which is the primary focus of the changeset.
Description check ✅ Passed The description directly relates to the changeset by referencing issue #4197 and explaining the intent to add dark/light mode theming to match OWASP Nest styling.
Linked Issues check ✅ Passed The PR successfully implements theme support for Swagger UI with light/dark modes and automatic theme application via localStorage and system preferences, addressing all primary objectives from issue #4197.
Out of Scope Changes check ✅ Passed All changes are directly scoped to adding Swagger UI theming: a stylesheet for light/dark mode colors and a template for theme initialization and rendering, with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@backend/static/css/swagger-theme.css`:
- Around line 30-34: The .btn.execute rule in swagger-theme.css uses
background-color and border-color values that yield insufficient contrast for
normal text; update the selector html .swagger-ui .btn.execute (and the
equivalent rule near lines 115-119) to use slightly darker background/border
colors—replace the light-mode color with `#1a6fc2` and the dark-mode color with
`#2563eb` while keeping color: `#ffffff`; ensure both background-color and
border-color are changed so the button meets WCAG contrast targets for normal
text and re-run a contrast check after the update.

In `@backend/templates/ninja/swagger.html`:
- Line 3: The top-level <html> element in backend/templates/ninja/swagger.html
lacks a lang attribute required for accessibility; update the <html> tag (the
root element in this template) to include an appropriate lang value (e.g., "en"
or a template/i18n variable) so screen readers and WCAG checks can detect the
document language.
- Around line 45-51: The Django template currently splits the template variable
braces across lines so "{{ swagger_settings | safe }}" won't render; update the
script with id "swagger-settings" to use contiguous Django variable tags and
filter syntax (i.e., use {{ swagger_settings|safe }} as a single token) so the
swagger_settings JSON is rendered into the script block for Swagger UI
initialization.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e29bd1fc-4ea0-42ca-bd24-9010f6c6969c

📥 Commits

Reviewing files that changed from the base of the PR and between 3e4f436 and e02dca0.

📒 Files selected for processing (2)
  • backend/static/css/swagger-theme.css
  • backend/templates/ninja/swagger.html

Copy link
Contributor
@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 2 files

Confidence score: 3/5

  • Possible Swagger UI breakage: backend/templates/ninja/swagger.html may output literal braces because swagger_settings lacks proper {{ ... }} wrapping, which could invalidate the JSON.
  • Dark mode styling risk in backend/static/css/swagger-theme.css: an unscoped !important light text rule can leave controls/text with incorrect colors.
  • Score reflects a concrete user-facing issue (Swagger JSON rendering) plus a medium-severity UI regression risk.
  • Pay close attention to backend/templates/ninja/swagger.html and backend/static/css/swagger-theme.css - template rendering correctness and dark-mode overrides.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/templates/ninja/swagger.html">

<violation number="1" location="backend/templates/ninja/swagger.html:48">
P2: The `swagger_settings` template variable is not wrapped in a valid `{{ ... }}` tag, so Django will output literal braces and break the JSON used by Swagger UI.</violation>
</file>

<file name="backend/static/css/swagger-theme.css">

<violation number="1" location="backend/static/css/swagger-theme.css:46">
P2: Unscoped `!important` light text rule is not fully overridden in dark mode, leaving several model controls/text with light-theme color values.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@sonarqubecloud
Copy link

Copy link
Contributor
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
backend/templates/ninja/swagger.html (1)

45-51: ⚠️ Potential issue | 🔴 Critical

Render swagger_settings as a single Django template expression.

This block still uses malformed template syntax, so the page will emit literal braces/text instead of the JSON config that swagger-ui-init.js expects.

🐛 Proposed fix
         <script type="application/json" id="swagger-settings">
-            {
-                {
-                    swagger_settings | safe
-                }
-            }
+            {{ swagger_settings|safe }}
         </script>
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/templates/ninja/swagger.html` around lines 45 - 51, The template
renders swagger_settings with extra braces so the JSON block emits literal
braces; change the script block to output swagger_settings as a single Django
expression (use the template variable swagger_settings inside the script with
the safe filter) so the element with id "swagger-settings" contains valid JSON
for swagger-ui-init.js to consume; update the <script id="swagger-settings">
content to a single {{ swagger_settings|safe }} expression (preserving the id
and type attributes) so the JSON config is rendered correctly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@backend/templates/ninja/swagger.html`:
- Around line 45-51: The template renders swagger_settings with extra braces so
the JSON block emits literal braces; change the script block to output
swagger_settings as a single Django expression (use the template variable
swagger_settings inside the script with the safe filter) so the element with id
"swagger-settings" contains valid JSON for swagger-ui-init.js to consume; update
the <script id="swagger-settings"> content to a single {{ swagger_settings|safe
}} expression (preserving the id and type attributes) so the JSON config is
rendered correctly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7016c928-9aa6-4c36-b8b7-93f0e6056004

📥 Commits

Reviewing files that changed from the base of the PR and between e02dca0 and b0cc10b.

📒 Files selected for processing (2)
  • backend/static/css/swagger-theme.css
  • backend/templates/ninja/swagger.html
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/static/css/swagger-theme.css

@HarshitVerma109
Copy link
Contributor Author

@arkid15r , please review it and let me know if any changes are required.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce OWASP Nest theme support for API Swagger UI

1 participant

0