Skip to content

Commit

Permalink
Add an option to generate YAML to generate-swagger
Browse files Browse the repository at this point in the history
Fixes #661
  • Loading branch information
segevfiner committed Sep 11, 2023
1 parent d16d64a commit 44436c4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
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

0 comments on commit 44436c4

Please sign in to comment.