diff --git a/README.md b/README.md index e857cf7f..5cbb18f3 100644 --- a/README.md +++ b/README.md @@ -318,10 +318,16 @@ Finally, there will be a new `README.md` file, which provides internal informati ### generate-swagger -if your project uses `@fastify/swagger`, `fastify-cli` can generate and write out the resulting Swagger/OpenAPI schema for you. +If your project uses `@fastify/swagger`, `fastify-cli` can generate and write out the resulting Swagger/OpenAPI schema for you. `fastify generate-swagger app.js` +To generate in YAML format add `--yaml=true`, to use the custom options from the +main plugin file add `-o/--options`. To use a custom decorator (The +`@fastify/swagger` `decorator` option), use `--decorator`. + +The schema is written to stdout. + ### linting `fastify-cli` is unopinionated on the choice of linter. We recommend you add a linter, like so: diff --git a/generate-swagger.js b/generate-swagger.js index 9ef87eeb..41149c88 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 @@ -45,10 +48,11 @@ async function generateSwagger (args) { process.exit(1) } + const decorator = extraOpts.decorator || 'swagger' if (extraOpts.yaml) { - return fastify.swagger({ yaml: true }) + return await fastify[decorator]({ yaml: true }) } else { - return JSON.stringify(fastify.swagger(), undefined, 2) + return JSON.stringify(await fastify[decorator](), undefined, 2) } } finally { fastify.close() @@ -66,7 +70,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..fb97dea5 100644 --- a/help/generate-swagger.txt +++ b/help/generate-swagger.txt @@ -6,3 +6,10 @@ OPTS --yaml=true generate in YAML format + + --decorator + Use a custom swagger decorator name + + -o, --options + [env: FASTIFY_OPTIONS] + Use custom options diff --git a/test/generate-swagger.test.js b/test/generate-swagger.test.js index f03ff405..cc82127f 100644 --- a/test/generate-swagger.test.js +++ b/test/generate-swagger.test.js @@ -28,3 +28,36 @@ 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) + } +}) + +test('should use custom decorator', async (t) => { + t.plan(1) + + try { + const swagger = JSON.parse(await generateSwagger(['--decorator', 'alternateSwagger', swaggerplugin])) + t.assert.equal(swagger.info.title, '@fastify/swagger alternate') + } catch (err) { + t.assert.ifError(err) + } +}) diff --git a/test/swaggerplugindir/plugin.js b/test/swaggerplugindir/plugin.js index fee6af1e..08e971af 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: {} @@ -57,5 +59,51 @@ paths: } } }) + + fastify.decorate('alternateSwagger', function (opts) { + if (opts && opts.yaml) { + return `\ +openapi: 3.0.3 +info: + version: 8.1.0 + title: "@fastify/swagger alternate" +components: + schemas: {} +paths: +"/alternate": + get: + responses: + '200': + description: Default Response +` + } else { + return { + openapi: '3.0.3', + info: { + version: '8.1.0', + title: '@fastify/swagger alternate', + }, + components: { + schemas: {} + }, + paths: { + '/alternate': { + get: { + responses: { + 200: { + description: 'Default Response' + } + } + } + }, + } + } + } + }) + next() }) + +module.exports.options = { + bodyLimit: 2097152 +}