-
Notifications
You must be signed in to change notification settings - Fork 6
fix(mobile-api): Swagger UI self-hosted (niente CDN) — /api/v1/docs era vuoto #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fabiodalez-dev
wants to merge
4
commits into
main
Choose a base branch
from
fix/swagger-ui-vendor-no-cdn
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−33
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2d710f
fix(mobile-api): vendor Swagger UI locally, drop the CDN fallback
fabiodalez-dev a2ccd99
test(mobile-api): E2E regression for the self-hosted Swagger UI docs
fabiodalez-dev 2376fea
fix(mobile-api): escape Host-derived Swagger asset URLs + guard vendo…
fabiodalez-dev e05e8ff
fix(mobile-api): JSON-encode the JS-context spec URL + diagnostic on …
fabiodalez-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // @ts-check | ||
| // Regression for the Mobile API docs page (`GET /api/v1/docs`). It must be | ||
| // self-hosted: Swagger UI is served from public/assets/swagger-ui/ and NEVER | ||
| // from a CDN, so it renders behind an egress firewall. Guards against the bug | ||
| // where only `.gitkeep` shipped and the page fell back to jsDelivr → blank. | ||
| const { test, expect } = require('@playwright/test'); | ||
|
|
||
| const BASE = process.env.E2E_BASE_URL || 'http://localhost:8081'; | ||
|
|
||
| // Public endpoints (no bearer token needed) but they only exist when the | ||
| // bundled Mobile API plugin is active. Probe once and skip cleanly otherwise. | ||
| let apiAvailable = true; | ||
|
|
||
| test.describe.serial('Mobile API — Swagger UI docs (self-hosted, no CDN)', () => { | ||
| test.beforeAll(async ({ request }) => { | ||
| try { | ||
| const res = await request.get(`${BASE}/api/v1/openapi.json`); | ||
| apiAvailable = res.status() === 200; | ||
| } catch { | ||
| apiAvailable = false; | ||
| } | ||
| }); | ||
|
|
||
| test.beforeEach(() => { | ||
| test.skip(!apiAvailable, 'Mobile API plugin not active / app not installed'); | ||
| }); | ||
|
|
||
| test('openapi.json is a valid OpenAPI 3 spec with paths', async ({ request }) => { | ||
| const res = await request.get(`${BASE}/api/v1/openapi.json`); | ||
| expect(res.status()).toBe(200); | ||
| expect(res.headers()['content-type']).toContain('json'); | ||
| const spec = await res.json(); | ||
| expect(String(spec.openapi)).toMatch(/^3\./); | ||
| expect(Object.keys(spec.paths || {}).length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test('docs page references LOCAL assets and never a CDN', async ({ request }) => { | ||
| const res = await request.get(`${BASE}/api/v1/docs`); | ||
| expect(res.status()).toBe(200); | ||
| const html = await res.text(); | ||
| // Loads the vendored bundle + css from the app's own asset path | ||
| expect(html).toContain('/assets/swagger-ui/swagger-ui-bundle.js'); | ||
| expect(html).toContain('/assets/swagger-ui/swagger-ui.css'); | ||
| // No external CDN host anywhere in the page | ||
| expect(html).not.toMatch(/cdn\.jsdelivr\.net|cdnjs\.cloudflare\.com|unpkg\.com/); | ||
| }); | ||
|
|
||
| test('the local Swagger UI assets are actually served', async ({ request }) => { | ||
| for (const f of ['swagger-ui-bundle.js', 'swagger-ui.css']) { | ||
| const res = await request.get(`${BASE}/assets/swagger-ui/${f}`); | ||
| expect(res.status(), f).toBe(200); | ||
| const body = await res.body(); | ||
| expect(body.length, `${f} should be a real file, not empty`).toBeGreaterThan(1000); | ||
| } | ||
| }); | ||
|
|
||
| test('Swagger UI renders the API operations from the local bundle', async ({ page }) => { | ||
| await page.goto(`${BASE}/api/v1/docs`); | ||
| // The bundle must load and SwaggerUIBundle must be defined (proves no broken CDN) | ||
| await page.waitForFunction(() => typeof window.SwaggerUIBundle !== 'undefined', null, { timeout: 15000 }); | ||
| // The spec must render at least one operation block | ||
| await page.waitForSelector('#swagger-ui .opblock', { timeout: 15000 }); | ||
| const ops = await page.locator('#swagger-ui .opblock').count(); | ||
| expect(ops).toBeGreaterThan(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Non usare l’endpoint sotto test come condizione di skip.
Se la regressione rompe
GET /api/v1/openapi.jsonsu Line 17,apiAvailablediventafalsee da Line 25 in poi l’intero spec viene saltato invece di fallire. Così il test non protegge più proprio il bug che dovrebbe intercettare. Il gate deve dipendere solo dai segnali d’ambiente che CI inietta per gli E2E, oppure questo probe va rimosso del tutto.Diff proposto
const { test, expect } = require('`@playwright/test`'); const BASE = process.env.E2E_BASE_URL || 'http://localhost:8081'; - -// Public endpoints (no bearer token needed) but they only exist when the -// bundled Mobile API plugin is active. Probe once and skip cleanly otherwise. -let apiAvailable = true; +const ADMIN_EMAIL = process.env.E2E_ADMIN_EMAIL; +const ADMIN_PASS = process.env.E2E_ADMIN_PASS; test.describe.serial('Mobile API — Swagger UI docs (self-hosted, no CDN)', () => { - test.beforeAll(async ({ request }) => { - try { - const res = await request.get(`${BASE}/api/v1/openapi.json`); - apiAvailable = res.status() === 200; - } catch { - apiAvailable = false; - } - }); - test.beforeEach(() => { - test.skip(!apiAvailable, 'Mobile API plugin not active / app not installed'); + test.skip(!ADMIN_EMAIL || !ADMIN_PASS, 'Credenziali E2E admin non configurate'); });Based on learnings, i Playwright E2E in questo repo devono essere gated da variabili d’ambiente che CI inietta, e negli UI-only spec il guard deve usare solo
E2E_ADMIN_EMAILeE2E_ADMIN_PASS.📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Learnings