diff --git a/generate-swagger.js b/generate-swagger.js index 9ef87eeb..090c8227 100644 --- a/generate-swagger.js +++ b/generate-swagger.js @@ -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 @@ -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) { diff --git a/help/generate-swagger.txt b/help/generate-swagger.txt index 06ebf355..c641f846 100644 --- a/help/generate-swagger.txt +++ b/help/generate-swagger.txt @@ -6,3 +6,7 @@ OPTS --yaml=true generate in YAML format + + -o, --options + [env: FASTIFY_OPTIONS] + Use custom options diff --git a/test/generate-swagger.test.js b/test/generate-swagger.test.js index f03ff405..792f19a8 100644 --- a/test/generate-swagger.test.js +++ b/test/generate-swagger.test.js @@ -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) + } +}) diff --git a/test/swaggerplugindir/plugin.js b/test/swaggerplugindir/plugin.js index fee6af1e..ac7278a3 100644 --- a/test/swaggerplugindir/plugin.js +++ b/test/swaggerplugindir/plugin.js @@ -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: @@ -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: {} @@ -59,3 +61,7 @@ paths: }) next() }) + +module.exports.options = { + bodyLimit: 2097152 +}