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
11 changes: 10 additions & 1 deletion generate-swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const {
} = require('./util')
const fp = require('fastify-plugin')
const { loadEnvQuitely } = require('./env-loader')
const deepmerge = require('@fastify/deepmerge')({
cloneProtoObject (obj) { return obj }
})

let Fastify = null

Expand Down Expand Up @@ -66,7 +69,13 @@ async function runFastify (opts) {
return module.exports.stop(e)
}

const fastify = Fastify(opts.options)
let options = {}

if (opts.options && file.options) {
options = deepmerge(options, file.options)
}

const fastify = Fastify(options)

const pluginOptions = {}
if (opts.prefix) {
Expand Down
4 changes: 4 additions & 0 deletions help/generate-swagger.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ OPTS

--yaml=true
generate in YAML format

-o, --options
[env: FASTIFY_OPTIONS]
Use custom options
22 changes: 22 additions & 0 deletions test/generate-swagger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ test('should generate swagger in yaml format', async (t) => {
t.assert.ifError(err)
}
})

test('should not use custom options by default', async (t) => {
t.plan(1)

try {
const swagger = JSON.parse(await generateSwagger([swaggerplugin]))
t.assert.equal(swagger.info.description, 'Body limit: 1048576')
} catch (err) {
t.assert.ifError(err)
}
})

test('should use custom options', async (t) => {
t.plan(1)

try {
const swagger = JSON.parse(await generateSwagger(['-o', swaggerplugin]))
t.assert.equal(swagger.info.description, 'Body limit: 2097152')
} catch (err) {
t.assert.ifError(err)
}
})
14 changes: 10 additions & 4 deletions test/swaggerplugindir/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ module.exports = fp(function (fastify, opts, next) {
return `\
openapi: 3.0.3
info:
version: 8.1.0
title: "@fastify/swagger"
version: 8.1.0
title: "@fastify/swagger"
description: "Body limit: ${fastify.initialConfig.bodyLimit}"
components:
schemas: {}
schemas: {}
paths:
"/":
get:
Expand All @@ -29,7 +30,8 @@ paths:
openapi: '3.0.3',
info: {
version: '8.1.0',
title: '@fastify/swagger'
title: '@fastify/swagger',
description: `Body limit: ${fastify.initialConfig.bodyLimit}`
},
components: {
schemas: {}
Expand Down Expand Up @@ -59,3 +61,7 @@ paths:
})
next()
})

module.exports.options = {
bodyLimit: 2097152
}