Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,25 @@ export const openapi = <
...documentation.info
}

const relativePath = specPath.startsWith('/') ? specPath.slice(1) : specPath
// Determine the correct URL for the OpenAPI spec
// Use absolute path to avoid browser URL resolution issues when paths have complex hierarchies
const getSpecUrl = () => {
if (!specPath.startsWith('/')) {
// Already relative
return specPath
}

// For default case where specPath follows the pattern path + '/json', use relative path
const defaultSpecPath = `${path}/json`
if (specPath === defaultSpecPath) {
return specPath.startsWith('/') ? specPath.slice(1) : specPath
}

// For custom specPath, use absolute path to prevent browser URL resolution issues
return specPath
}

const specUrl = getSpecUrl()

let totalRoutes = 0
let cachedSchema: OpenAPIV3.Document | undefined
Expand All @@ -52,14 +70,14 @@ export const openapi = <
new Response(
provider === 'swagger-ui'
? SwaggerUIRender(info, {
url: relativePath,
url: specUrl,
dom_id: '#swagger-ui',
version: 'latest',
autoDarkMode: true,
...swagger
})
: ScalarRender(info, {
url: relativePath,
url: specUrl,
version: 'latest',
cdn: `https://cdn.jsdelivr.net/npm/@scalar/api-reference@${scalar?.version ?? 'latest'}/dist/browser/standalone.min.js`,
...(scalar as ApiReferenceConfiguration),
Expand Down
44 changes: 42 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Elysia, t } from 'elysia'
import SwaggerParser from '@apidevtools/swagger-parser'
import { Elysia, t } from 'elysia'
import { openapi } from '../src'

import { describe, expect, it } from 'bun:test'
import { fail } from 'assert'
import { describe, expect, it } from 'bun:test'

const req = (path: string) => new Request(`http://localhost${path}`)

Expand Down Expand Up @@ -272,4 +272,44 @@ describe('Swagger', () => {
const response = await res.json()
expect(Object.keys(response.paths['/all'])).toBeArrayOfSize(8)
})

it('should use absolute path for custom specPath to prevent path duplication', async () => {
const app = new Elysia().use(
openapi({
path: '/api/v1/docs',
specPath: '/api/v1/openapi.json'
})
)

await app.modules

const res = await app.handle(req('/api/v1/docs')).then((x) => x.text())

// The data-url should be the absolute path to prevent duplication
expect(res.includes('data-url="/api/v1/openapi.json"')).toBe(true)

// Ensure the spec endpoint works
const specRes = await app.handle(req('/api/v1/openapi.json'))
expect(specRes.status).toBe(200)
})

it('should use relative path for default specPath pattern', async () => {
const app = new Elysia().use(
openapi({
path: '/api/docs'
// specPath defaults to '/api/docs/json'
})
)

await app.modules

const res = await app.handle(req('/api/docs')).then((x) => x.text())

// The data-url should be relative for default pattern
expect(res.includes('data-url="api/docs/json"')).toBe(true)

// Ensure the spec endpoint works
const specRes = await app.handle(req('/api/docs/json'))
expect(specRes.status).toBe(200)
})
})