Skip to content

Commit aa44ef4

Browse files
authored
fix: avoid overriding the prefix option (#224)
1 parent 3e01bb5 commit aa44ef4

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ async function fastifySwaggerUi (fastify, opts) {
1010

1111
// if no logo is provided, read default static logo
1212
let logoContent = opts.logo
13-
if (logoContent == null) {
13+
if (logoContent === undefined) {
1414
const bufferLogoContent = await fsPromises.readFile(path.join(__dirname, './static/logo.svg'))
1515
logoContent = { type: 'image/svg+xml', content: bufferLogoContent }
1616
}
1717

1818
await fastify.register(require('./lib/routes'), {
19+
...opts,
1920
prefix: opts.routePrefix || '/documentation',
2021
uiConfig: opts.uiConfig || {},
2122
initOAuth: opts.initOAuth || {},
2223
hooks: opts.uiHooks,
2324
theme: opts.theme || {},
24-
logo: logoContent,
25-
...opts
25+
logo: logoContent
2626
})
2727
}
2828

test/route.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ test('/api/v1/docs should display index html with correct asset urls', async (t)
623623
t.plan(13)
624624
const fastify = Fastify()
625625
await fastify.register(fastifySwagger, swaggerOption)
626-
await fastify.register(fastifySwaggerUi, { prefix: '/api/v1/docs', theme: { js: [{ filename: 'theme-js.js' }] } })
626+
await fastify.register(fastifySwaggerUi, { routePrefix: '/api/v1/docs', theme: { js: [{ filename: 'theme-js.js' }] } })
627627

628628
const res = await fastify.inject({
629629
method: 'GET',
@@ -750,3 +750,21 @@ test('/docs/ should display index html with correct asset urls when documentatio
750750
t.assert.strictEqual(res.payload.includes('href="./index.css"'), false)
751751
t.assert.strictEqual(res.payload.includes('src="./theme/theme-js.js"'), false)
752752
})
753+
754+
test('should ignore prefix when register plugin', async (t) => {
755+
t.plan(4)
756+
const fastify = Fastify()
757+
await fastify.register(fastifySwagger, swaggerOption)
758+
await fastify.register(fastifySwaggerUi, { routePrefix: '/v1/documentation', prefix: '/' })
759+
760+
fastify.get('/', () => {})
761+
762+
const res = await fastify.inject({
763+
method: 'GET',
764+
url: '/v1/documentation'
765+
})
766+
t.assert.deepStrictEqual(res.statusCode, 200)
767+
t.assert.deepStrictEqual(res.headers.location, undefined)
768+
t.assert.deepStrictEqual(typeof res.payload, 'string')
769+
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
770+
})

0 commit comments

Comments
 (0)