Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to generate YAML to generate-swagger #662

Merged
merged 2 commits into from
Sep 14, 2023
Merged
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
8 changes: 7 additions & 1 deletion generate-swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict'

const parseArgs = require('./args')
const argv = require('yargs-parser')
const log = require('./log')
const {
exit,
Expand All @@ -24,6 +25,7 @@ function loadModules (opts) {

async function generateSwagger (args) {
const opts = parseArgs(args)
const extraOpts = argv(args)
if (opts.help) {
return showHelpForCommand('generate-swagger')
}
Expand All @@ -45,7 +47,11 @@ async function generateSwagger (args) {
process.exit(1)
}

return JSON.stringify(fastify.swagger(), undefined, 2)
if (extraOpts.yaml) {
return fastify.swagger({ yaml: true })
} else {
return JSON.stringify(fastify.swagger(), undefined, 2)
}
} finally {
fastify.close()
}
Expand Down
7 changes: 6 additions & 1 deletion help/generate-swagger.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Usage: fastify generate-plugin [opts] <file> [--] [<plugin-options>]

Generate Swagger/OpenAPI schema for a project using @fastify/cli.
Generate Swagger/OpenAPI schema for a project using @fastify/swagger.

OPTS

--yaml=true
generate in YAML format
11 changes: 11 additions & 0 deletions test/generate-swagger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ test('should generate swagger', async (t) => {
t.error(err)
}
})

test('should generate swagger in yaml format', async (t) => {
t.plan(1)

try {
const swagger = await generateSwagger(['--yaml=true', swaggerplugin])
t.ok(swagger.startsWith('openapi: 3.0.3'))
} catch (err) {
t.error(err)
}
})
68 changes: 45 additions & 23 deletions test/swaggerplugindir/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,53 @@
const fp = require('fastify-plugin')

module.exports = fp(function (fastify, opts, next) {
fastify.decorate('swagger', function () {
return {
openapi: '3.0.3',
info: {
version: '8.1.0',
title: '@fastify/swagger'
},
components: {
schemas: {}
},
paths: {
'/': {
get: {
responses: {
200: {
description: 'Default Response'
fastify.decorate('swagger', function (opts) {
if (opts && opts.yaml) {
return `\
openapi: 3.0.3
info:
version: 8.1.0
title: "@fastify/swagger"
components:
schemas: {}
paths:
"/":
get:
responses:
'200':
description: Default Response
"/example/":
get:
responses:
'200':
description: Default Response
`
} else {
return {
openapi: '3.0.3',
info: {
version: '8.1.0',
title: '@fastify/swagger'
},
components: {
schemas: {}
},
paths: {
'/': {
get: {
responses: {
200: {
description: 'Default Response'
}
}
}
}
},
'/example/': {
get: {
responses: {
200: {
description: 'Default Response'
},
'/example/': {
get: {
responses: {
200: {
description: 'Default Response'
}
}
}
}
Expand Down