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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,14 @@ 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`.

The schema is written to stdout.

### linting

`fastify-cli` is unopinionated on the choice of linter. We recommend you add a linter, like so:
Expand Down
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
}