|
1 | | -import { Elysia, t } from 'elysia' |
2 | 1 | import SwaggerParser from '@apidevtools/swagger-parser' |
| 2 | +import { Elysia, t } from 'elysia' |
3 | 3 | import { openapi } from '../src' |
4 | 4 |
|
5 | | -import { describe, expect, it } from 'bun:test' |
6 | 5 | import { fail } from 'assert' |
| 6 | +import { describe, expect, it } from 'bun:test' |
7 | 7 |
|
8 | 8 | const req = (path: string) => new Request(`http://localhost${path}`) |
9 | 9 |
|
@@ -272,4 +272,44 @@ describe('Swagger', () => { |
272 | 272 | const response = await res.json() |
273 | 273 | expect(Object.keys(response.paths['/all'])).toBeArrayOfSize(8) |
274 | 274 | }) |
| 275 | + |
| 276 | + it('should use absolute path for custom specPath to prevent path duplication', async () => { |
| 277 | + const app = new Elysia().use( |
| 278 | + openapi({ |
| 279 | + path: '/api/v1/docs', |
| 280 | + specPath: '/api/v1/openapi.json' |
| 281 | + }) |
| 282 | + ) |
| 283 | + |
| 284 | + await app.modules |
| 285 | + |
| 286 | + const res = await app.handle(req('/api/v1/docs')).then((x) => x.text()) |
| 287 | + |
| 288 | + // The data-url should be the absolute path to prevent duplication |
| 289 | + expect(res.includes('data-url="/api/v1/openapi.json"')).toBe(true) |
| 290 | + |
| 291 | + // Ensure the spec endpoint works |
| 292 | + const specRes = await app.handle(req('/api/v1/openapi.json')) |
| 293 | + expect(specRes.status).toBe(200) |
| 294 | + }) |
| 295 | + |
| 296 | + it('should use relative path for default specPath pattern', async () => { |
| 297 | + const app = new Elysia().use( |
| 298 | + openapi({ |
| 299 | + path: '/api/docs' |
| 300 | + // specPath defaults to '/api/docs/json' |
| 301 | + }) |
| 302 | + ) |
| 303 | + |
| 304 | + await app.modules |
| 305 | + |
| 306 | + const res = await app.handle(req('/api/docs')).then((x) => x.text()) |
| 307 | + |
| 308 | + // The data-url should be relative for default pattern |
| 309 | + expect(res.includes('data-url="api/docs/json"')).toBe(true) |
| 310 | + |
| 311 | + // Ensure the spec endpoint works |
| 312 | + const specRes = await app.handle(req('/api/docs/json')) |
| 313 | + expect(specRes.status).toBe(200) |
| 314 | + }) |
275 | 315 | }) |
0 commit comments