Socket
Socket
Sign inDemoInstall

@fastify/swagger

Package Overview
Dependencies
35
Maintainers
18
Versions
35
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 6.0.1 to 7.0.0

.vscode/settings.json

2

examples/static-json-file.js
'use strict'
const fastify = require('fastify')({ logger: true })
// const swagger = require('fastify-swagger')
// const swagger = require('@fastify/swagger')
const swagger = require('..')

@@ -6,0 +6,0 @@

'use strict'
const fastify = require('fastify')({ logger: true })
// const swagger = require('fastify-swagger')
// const swagger = require('@fastify/swagger')
const swagger = require('..')

@@ -6,0 +6,0 @@

@@ -29,4 +29,4 @@ 'use strict'

module.exports = fp(fastifySwagger, {
fastify: '>=3.x',
name: 'fastify-swagger'
fastify: '4.x',
name: '@fastify/swagger'
})

@@ -49,3 +49,3 @@ 'use strict'

if (route.links) {
throw new Error('Swagger (Open API v2) does not support Links. Upgrade to OpenAPI v3 (see fastify-swagger readme)')
throw new Error('Swagger (Open API v2) does not support Links. Upgrade to OpenAPI v3 (see @fastify/swagger readme)')
}

@@ -52,0 +52,0 @@

'use strict'
const rawRequired = Symbol('fastify-swagger.rawRequired')
const rawRequired = Symbol('@fastify/swagger.rawRequired')

@@ -5,0 +5,0 @@ module.exports = {

@@ -15,2 +15,6 @@ 'use strict'

fastify.addHook('onRoute', (routeOptions) => {
// we need to skip HEAD route
// since it may automatically added by fastify
// and will conflict with the existing route
if (routeOptions.method === 'HEAD') return
routes.push(routeOptions)

@@ -17,0 +21,0 @@ })

@@ -14,2 +14,3 @@ const fs = require('fs')

'oauth2-redirect.html',
'swagger-initializer.js',
'swagger-ui-bundle.js',

@@ -27,57 +28,50 @@ 'swagger-ui-bundle.js.map',

const newIndex = fs.readFileSync(resolve('./static/index.html'), 'utf8')
.replace(/<script>(.*)<\/script>/gs, `
<script>
window.onload = function () {
function resolveUrl (url) {
const anchor = document.createElement('a')
anchor.href = url
return anchor.href
fse.writeFileSync(resolve('./static/swagger-initializer.js'), `window.onload = function () {
function resolveUrl (url) {
const anchor = document.createElement('a')
anchor.href = url
return anchor.href
}
function resolveConfig (cb) {
return fetch(
resolveUrl('./uiConfig').replace('static/uiConfig', 'uiConfig')
)
.then(res => res.json())
.then((config) => {
const resConfig = Object.assign({}, {
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
}, config, {
url: resolveUrl('./json').replace('static/json', 'json'),
oauth2RedirectUrl: resolveUrl('./oauth2-redirect.html')
});
return cb(resConfig);
})
}
function resolveConfig (cb) {
return fetch(
resolveUrl('./uiConfig').replace('static/uiConfig', 'uiConfig')
)
.then(res => res.json())
.then((config) => {
const resConfig = Object.assign({}, {
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
}, config, {
url: resolveUrl('./json').replace('static/json', 'json'),
oauth2RedirectUrl: resolveUrl('./oauth2-redirect.html')
});
return cb(resConfig);
})
}
// Begin Swagger UI call region
const buildUi = function (config) {
const ui = SwaggerUIBundle(config)
window.ui = ui
// Begin Swagger UI call region
const buildUi = function (config) {
const ui = SwaggerUIBundle(config)
window.ui = ui
fetch(resolveUrl('./initOAuth').replace('static/initOAuth', 'initOAuth'))
.then(res => res.json())
.then((config) => {
ui.initOAuth(config);
});
}
// End Swagger UI call region
resolveConfig(buildUi);
fetch(resolveUrl('./initOAuth').replace('static/initOAuth', 'initOAuth'))
.then(res => res.json())
.then((config) => {
ui.initOAuth(config);
});
}
</script>
`)
// End Swagger UI call region
fse.writeFileSync(resolve('./static/index.html'), newIndex)
resolveConfig(buildUi);
}`)

@@ -84,0 +78,0 @@ const sha = {

{
"name": "@fastify/swagger",
"version": "6.0.1",
"version": "7.0.0",
"description": "Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation",
"main": "index.js",
"types": "index.d.ts",
"scripts": {

@@ -44,24 +45,24 @@ "coverage": "npm run unit -- --coverage-report=lcovonly",

"devDependencies": {
"@types/node": "^17.0.0",
"fastify": "^3.7.0",
"fastify-basic-auth": "^2.1.0",
"fastify-helmet": "^5.0.3",
"fluent-json-schema": "^3.0.1",
"fs-extra": "^10.0.0",
"joi": "^14.3.1",
"joi-to-json": "^2.0.0",
"@fastify/basic-auth": "^4.0.0",
"@fastify/helmet": "^9.0.0",
"@types/node": "^17.0.13",
"fastify": "^4.0.0-rc.2",
"fluent-json-schema": "^3.1.0",
"fs-extra": "^10.1.0",
"joi": "^17.6.0",
"joi-to-json": "^2.2.4",
"pre-commit": "^1.2.2",
"qs": "^6.5.2",
"qs": "^6.10.3",
"standard": "^17.0.0",
"swagger-parser": "^10.0.2",
"swagger-ui-dist": "4.6.2",
"tap": "^16.0.0",
"swagger-parser": "^10.0.3",
"swagger-ui-dist": "4.11.1",
"tap": "^16.2.0",
"tsd": "^0.20.0"
},
"dependencies": {
"@fastify/static": "^5.0.0",
"fastify-plugin": "^3.0.0",
"js-yaml": "^4.0.0",
"@fastify/static": "^6.0.0",
"fastify-plugin": "^3.0.1",
"js-yaml": "^4.1.0",
"json-schema-resolver": "^1.3.0",
"openapi-types": "^10.0.0",
"openapi-types": "^11.0.0",
"rfdc": "^1.3.0"

@@ -68,0 +69,0 @@ },

@@ -5,3 +5,2 @@ # @fastify/swagger

![CI workflow](https://github.com/fastify/fastify-swagger/workflows/CI%20workflow/badge.svg)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-swagger/badge.svg)](https://snyk.io/test/github/fastify/fastify-swagger)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

@@ -28,3 +27,3 @@

fastify.register(require('@fastify/swagger'), {
await fastify.register(require('@fastify/swagger'), {
routePrefix: '/documentation',

@@ -132,6 +131,4 @@ swagger: {

fastify.ready(err => {
if (err) throw err
fastify.swagger()
})
await fastify.ready()
fastify.swagger()
```

@@ -264,3 +261,3 @@ <a name="api"></a>

fastify.register(require('@fastify/swagger'), {
await fastify.register(require('@fastify/swagger'), {
swagger: { ... },

@@ -310,3 +307,3 @@ transform: ({ schema, url }) => {

```js
fastify.register(require('@fastify/swagger'), {
await fastify.register(require('@fastify/swagger'), {
swagger: { ... },

@@ -700,3 +697,3 @@ ...

fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
exposeRoute: true,

@@ -703,0 +700,0 @@ uiHooks: {

@@ -1,1 +0,1 @@

{"script":["'sha256-2yQBTLGLI1sDcBILfj/o6b5ufMv6CEwPYOk3RZI/WjE='","'sha256-g6TK8Crx7YtGVUN10j0q8wD3cvwyzlvBMzQx4UjBPg4='"],"style":["'sha256-pyVPiLlnqL9OWVoJPs/E6VVF5hBecRzM2gBiarnaqAo='"]}
{"script":["'sha256-g6TK8Crx7YtGVUN10j0q8wD3cvwyzlvBMzQx4UjBPg4='"],"style":[]}
'use strict'
const t = require('tap')
const test = t.test
const { test } = require('tap')
const Fastify = require('fastify')
const fastifyHelmet = require('fastify-helmet')
const fastifySwagger = require('../index')
const fastifyHelmet = require('@fastify/helmet')
const fastifySwagger = require('..')
const {

@@ -24,7 +23,7 @@ schemaQuerystring,

test('staticCSP = undefined', t => {
t.plan(4)
test('staticCSP = undefined', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -38,18 +37,16 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
t.equal(typeof res.payload, 'string')
})
test('staticCSP = true', t => {
t.plan(7)
test('staticCSP = true', async (t) => {
t.plan(5)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...swaggerOption,

@@ -66,27 +63,27 @@ staticCSP: true

fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], `default-src 'self'; base-uri 'self'; block-all-mixed-content; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data: validator.swagger.io; object-src 'none'; script-src 'self' ${csp.script.join(' ')}; script-src-attr 'none'; style-src 'self' https: ${csp.style.join(' ')}; upgrade-insecure-requests;`)
t.equal(typeof res.payload, 'string')
})
}
fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
})
}
})
test('staticCSP = "default-src \'self\';"', t => {
t.plan(7)
test('staticCSP = "default-src \'self\';"', async (t) => {
t.plan(5)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...swaggerOption,

@@ -103,27 +100,27 @@ staticCSP: "default-src 'self';"

fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self';")
t.equal(typeof res.payload, 'string')
})
}
fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
})
}
})
test('staticCSP = object', t => {
t.plan(7)
test('staticCSP = object', async (t) => {
t.plan(5)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...swaggerOption,

@@ -143,27 +140,27 @@ staticCSP: {

fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.equal(typeof res.payload, 'string')
})
}
fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
})
}
})
test('transformStaticCSP = function', t => {
t.plan(8)
test('transformStaticCSP = function', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...swaggerOption,

@@ -184,31 +181,31 @@ staticCSP: "default-src 'self';",

fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.equal(typeof res.payload, 'string')
})
}
fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
})
}
})
test('transformStaticCSP = function, with fastify-helmet', t => {
t.plan(8)
test('transformStaticCSP = function, with @fastify/helmet', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifyHelmet)
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...swaggerOption,
transformStaticCSP: function (header) {
t.equal(header, "default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
t.equal(header, "default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
return "default-src 'self'; script-src 'self';"

@@ -225,20 +222,20 @@ }

fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/index.html'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.equal(typeof res.payload, 'string')
})
}
fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
})
t.equal(res.headers['content-security-policy'], "default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
}
})

@@ -7,24 +7,20 @@ 'use strict'

test('fastify.swagger should exist', t => {
t.plan(2)
test('fastify.swagger should exist', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger)
await fastify.register(fastifySwagger)
fastify.ready(err => {
t.error(err)
t.ok(fastify.swagger)
})
await fastify.ready()
t.ok(fastify.swagger)
})
test('fastify.swaggerCSP should exist', t => {
t.plan(2)
test('fastify.swaggerCSP should exist', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger)
await fastify.register(fastifySwagger)
fastify.ready(err => {
t.error(err)
t.ok(fastify.swaggerCSP)
})
await fastify.ready()
t.ok(fastify.swaggerCSP)
})
'use strict'
const t = require('tap')
const semver = require('semver')
if (semver.lt(process.versions.node, '13.3.0')) {
t.skip('Skip because Node version <= 13.3.0')
t.end()
} else {
// Node v8 throw a `SyntaxError: Unexpected token import`
// even if this branch is never touch in the code,
// by using `eval` we can avoid this issue.
// eslint-disable-next-line
new Function('module', 'return import(module)')('./esm.mjs').catch((err) => {
process.nextTick(() => {
throw err
})
// Node v8 throw a `SyntaxError: Unexpected token import`
// even if this branch is never touch in the code,
// by using `eval` we can avoid this issue.
// eslint-disable-next-line
new Function('module', 'return import(module)')('./esm.mjs').catch((err) => {
process.nextTick(() => {
throw err
})
}
})

@@ -27,4 +27,4 @@ 'use strict'

const fastify = Fastify()
await fastify.register(require('fastify-basic-auth'), authOptions)
fastify.register(fastifySwagger, {
await fastify.register(require('@fastify/basic-auth'), authOptions)
await fastify.register(fastifySwagger, {
mode: 'static',

@@ -78,5 +78,5 @@ specification: {

const fastify = Fastify()
await fastify.register(require('fastify-basic-auth'), authOptions)
await fastify.register(require('@fastify/basic-auth'), authOptions)
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...swaggerOption,

@@ -89,3 +89,3 @@ exposeRoute: true,

fastify.get('/fooBar123', schemaBody, () => {})
fastify.post('/fooBar123', schemaBody, () => {})

@@ -115,3 +115,3 @@ let res = await fastify.inject('/documentation')

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: {},

@@ -118,0 +118,0 @@ refResolver: {

@@ -5,15 +5,15 @@ 'use strict'

const Fastify = require('fastify')
const fastifySwagger = require('../index')
const fastifyHelmet = require('fastify-helmet')
const fastifySwagger = require('..')
const fastifyHelmet = require('@fastify/helmet')
const swaggerCSP = require('../static/csp.json')
test('fastify will response swagger csp', t => {
t.plan(2)
test('fastify will response swagger csp', async (t) => {
t.plan(1)
const scriptCSP = swaggerCSP.script.length > 0 ? ` ${swaggerCSP.script.join(' ')}` : ''
const styleCSP = swaggerCSP.style.length > 0 ? ` ${swaggerCSP.style.join(' ')}` : ''
const csp = `default-src 'self';img-src 'self' data: validator.swagger.io;script-src 'self'${scriptCSP};style-src 'self' https:${styleCSP}`
const csp = `default-src 'self';img-src 'self' data: validator.swagger.io;script-src 'self'${scriptCSP};style-src 'self' https:${styleCSP};base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';object-src 'none';script-src-attr 'none';upgrade-insecure-requests`
const fastify = Fastify()
fastify.register(fastifySwagger)
await fastify.register(fastifySwagger)
fastify.register(fastifyHelmet, instance => {

@@ -39,9 +39,7 @@ return {

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.same(res.headers['content-security-policy'], csp)
})
t.same(res.headers['content-security-policy'], csp)
})

@@ -14,3 +14,3 @@ 'use strict'

test('specification validation check works', t => {
test('specification validation check works', async (t) => {
const specifications = [

@@ -34,18 +34,18 @@ '',

specifications.forEach(specification => {
const fastify = Fastify()
fastify.register(fastifySwagger, {
mode: 'static',
specification,
exposeRoute: true
})
fastify.ready(err => {
for (const specification of specifications) {
try {
const fastify = Fastify()
await fastify.register(fastifySwagger, {
mode: 'static',
specification,
exposeRoute: true
})
} catch (err) {
t.not(err, undefined)
t.matchSnapshot(err.toString())
})
})
}
}
})
test('registering plugin with invalid mode throws an error', t => {
test('registering plugin with invalid mode throws an error', async (t) => {
const config = {

@@ -58,10 +58,10 @@ mode: 'error'

fastify.register(fastifySwagger, config)
fastify.ready(err => {
try {
await fastify.register(fastifySwagger, config)
} catch (err) {
t.equal(err.message, 'unsupported mode, should be one of [\'static\', \'dynamic\']')
})
}
})
test('unsupported file extension in specification.path throws an error', t => {
test('unsupported file extension in specification.path throws an error', async (t) => {
const config = {

@@ -77,10 +77,11 @@ mode: 'static',

const fastify = Fastify()
fastify.register(fastifySwagger, config)
fastify.ready(err => {
try {
await fastify.register(fastifySwagger, config)
} catch (err) {
t.equal(err.message, 'specification.path extension name is not supported, should be one from [\'.yaml\', \'.json\']')
})
}
})
test('non-string specification.baseDir throws an error ', t => {
test('non-string specification.baseDir throws an error ', async (t) => {
const config = {

@@ -97,10 +98,11 @@ exposeRoute: true,

const fastify = Fastify()
fastify.register(fastifySwagger, config)
fastify.ready(err => {
try {
await fastify.register(fastifySwagger, config)
} catch (err) {
t.equal(err.message, 'specification.baseDir should be string')
})
}
})
test('non-object specification.document throws an error', t => {
test('non-object specification.document throws an error', async (t) => {
const config = {

@@ -116,10 +118,11 @@ exposeRoute: true,

const fastify = new Fastify()
fastify.register(fastifySwagger, config)
fastify.ready(err => {
try {
await fastify.register(fastifySwagger, config)
} catch (err) {
t.equal(err.message, 'specification.document is not an object')
})
}
})
test('swagger route returns yaml', t => {
test('swagger route returns yaml', async (t) => {
const config = {

@@ -133,27 +136,19 @@ mode: 'static',

t.plan(4)
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
// check that yaml is there
fastify.inject(
{
method: 'GET',
url: '/documentation/yaml'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
try {
yaml.load(res.payload)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
}
)
const res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.pass('valid swagger yaml')
})
test('swagger route returns json', t => {
test('swagger route returns json', async (t) => {
const config = {

@@ -167,27 +162,19 @@ mode: 'static',

t.plan(4)
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
// check that json is there
fastify.inject(
{
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
try {
yaml.load(res.payload)
t.pass('valid swagger json')
} catch (err) {
t.fail(err)
}
}
)
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
yaml.load(res.payload)
t.pass('valid swagger json')
})
test('postProcessor works, swagger route returns updated yaml', t => {
test('postProcessor works, swagger route returns updated yaml', async (t) => {
const config = {

@@ -205,28 +192,20 @@ mode: 'static',

t.plan(5)
t.plan(4)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
// check that yaml is there
fastify.inject(
{
method: 'GET',
url: '/documentation/yaml'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
try {
yaml.load(res.payload)
t.matchSnapshot(res.payload)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
}
)
const res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.matchSnapshot(res.payload)
t.pass('valid swagger yaml')
})
test('swagger route returns explicitly passed doc', t => {
test('swagger route returns explicitly passed doc', async (t) => {
const document = {

@@ -248,27 +227,18 @@ info: {

t.plan(3)
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
// check that json is there
fastify.inject(
{
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
})
try {
const payload = JSON.parse(res.payload)
t.matchSnapshot(JSON.stringify(payload, null, 2))
t.pass('valid explicitly passed spec document')
} catch (error) {
t.fail(error)
}
}
)
const payload = JSON.parse(res.payload)
t.matchSnapshot(JSON.stringify(payload, null, 2))
t.pass('valid explicitly passed spec document')
})
test('/documentation/:file should serve static file from the location of main specification file', t => {
test('/documentation/:file should serve static file from the location of main specification file', async (t) => {
const config = {

@@ -282,19 +252,21 @@ exposeRoute: true,

t.plan(7)
t.plan(4)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject({
method: 'GET',
url: '/documentation/non-existing-file'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/non-existing-file'
})
t.equal(res.statusCode, 404)
})
}
fastify.inject({
method: 'GET',
url: '/documentation/example-static-specification.yaml'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/example-static-specification.yaml'
})
t.equal(res.statusCode, 200)

@@ -308,14 +280,15 @@ t.equal(

)
})
}
fastify.inject({
method: 'GET',
url: '/documentation/dynamic-swagger.js'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/dynamic-swagger.js'
})
t.equal(res.statusCode, 200)
})
}
})
test('/documentation/non-existing-file calls custom NotFoundHandler', t => {
test('/documentation/non-existing-file calls custom NotFoundHandler', async (t) => {
const config = {

@@ -329,5 +302,5 @@ exposeRoute: true,

t.plan(2)
t.plan(1)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.setNotFoundHandler((request, reply) => {

@@ -337,12 +310,11 @@ reply.code(410).send()

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/some-file-that-does-not-exist.js'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 410)
})
t.equal(res.statusCode, 410)
})
test('/documentation/:file should be served from custom location', t => {
test('/documentation/:file should be served from custom location', async (t) => {
const config = {

@@ -357,20 +329,19 @@ exposeRoute: true,

t.plan(3)
t.plan(2)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/oauth2-redirect.html'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(
readFileSync(resolve(__dirname, '..', '..', 'static', 'oauth2-redirect.html'), 'utf8'),
res.payload
)
})
t.equal(res.statusCode, 200)
t.equal(
readFileSync(resolve(__dirname, '..', '..', 'static', 'oauth2-redirect.html'), 'utf8'),
res.payload
)
})
test('/documentation/:file should be served from custom location with trailing slash(es)', t => {
test('/documentation/:file should be served from custom location with trailing slash(es)', async (t) => {
const config = {

@@ -385,20 +356,19 @@ exposeRoute: true,

t.plan(3)
t.plan(2)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/oauth2-redirect.html'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(
readFileSync(resolve(__dirname, '..', '..', 'static', 'oauth2-redirect.html'), 'utf8'),
res.payload
)
})
t.equal(res.statusCode, 200)
t.equal(
readFileSync(resolve(__dirname, '..', '..', 'static', 'oauth2-redirect.html'), 'utf8'),
res.payload
)
})
test('/documentation/yaml returns cache.swaggerString on second request in static mode', t => {
test('/documentation/yaml returns cache.swaggerString on second request in static mode', async (t) => {
const config = {

@@ -412,40 +382,32 @@ mode: 'static',

t.plan(8)
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject(
{
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
try {
yaml.load(res.payload)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
}
)
})
fastify.inject(
{
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.pass('valid swagger yaml')
}
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.pass('valid swagger yaml')
}
)
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.pass('valid swagger yaml')
}
})
test('/documentation/json returns cache.swaggerObject on second request in static mode', t => {
test('/documentation/json returns cache.swaggerObject on second request in static mode', async (t) => {
const config = {

@@ -459,34 +421,30 @@ mode: 'static',

t.plan(8)
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject(
{
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
)
})
fastify.inject(
{
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
)
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
})
test('/documentation/yaml returns cache.swaggerString on second request in dynamic mode', t => {
test('/documentation/yaml returns cache.swaggerString on second request in dynamic mode', async (t) => {
const config = {

@@ -499,44 +457,32 @@ specification: {

t.plan(8)
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject(
{
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
try {
yaml.load(res.payload)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
}
)
})
fastify.inject(
{
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.pass('valid swagger yaml')
}
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
try {
yaml.load(res.payload)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
}
)
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.pass('valid swagger yaml')
}
})
test('/documentation/json returns cache.swaggerObject on second request in dynamic mode', t => {
test('/documentation/json returns cache.swaggerObject on second request in dynamic mode', async (t) => {
const config = {

@@ -549,34 +495,30 @@ specification: {

t.plan(8)
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject(
{
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
)
})
fastify.inject(
{
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
)
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
}
})
test('swagger routes are not exposed', t => {
test('swagger routes are not exposed', async (t) => {
const config = {

@@ -590,33 +532,27 @@ mode: 'static',

t.plan(4)
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
// check that yaml is there
fastify.inject(
{
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('routes are not exposed')
}
)
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('routes are not exposed')
})
test('inserts default opts in fastifySwagger', t => {
test('inserts default opts in fastifySwagger', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger)
await fastify.register(fastifySwagger)
fastify.ready(() => {
t.pass('Inserted default option for fastifySwagger.')
})
t.pass('Inserted default option for fastifySwagger.')
})
test('inserts default package name', t => {
test('inserts default package name', async (t) => {
const config = {

@@ -630,5 +566,5 @@ mode: 'dynamic',

t.plan(2)
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)

@@ -645,15 +581,11 @@ const originalPathJoin = path.join

fastify.inject(
{
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.pass('Inserted default package name.')
}
)
await fastify.inject({
method: 'GET',
url: '/documentation/json'
})
t.pass('Inserted default package name.')
})
test('inserts default package name - openapi', t => {
test('inserts default package name - openapi', async (t) => {
const config = {

@@ -670,5 +602,5 @@ mode: 'dynamic',

t.plan(2)
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)

@@ -685,15 +617,11 @@ const originalPathJoin = path.join

fastify.inject(
{
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.pass('Inserted default package name.')
}
)
await fastify.inject({
method: 'GET',
url: '/documentation/json'
})
t.pass('Inserted default package name.')
})
test('throws an error if cannot parse package\'s JSON', t => {
test('throws an error if cannot parse package\'s JSON', async (t) => {
const config = {

@@ -707,5 +635,5 @@ mode: 'dynamic',

t.plan(2)
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)

@@ -722,15 +650,11 @@ const originalPathJoin = path.join

fastify.inject(
{
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(err, null)
}
)
await fastify.inject({
method: 'GET',
url: '/documentation/json'
})
t.pass('no error should throw')
})
test('throws an error if cannot parse package\'s JSON - openapi', t => {
test('throws an error if cannot parse package\'s JSON - openapi', async (t) => {
const config = {

@@ -747,5 +671,5 @@ mode: 'dynamic',

t.plan(2)
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)

@@ -762,26 +686,20 @@ const originalPathJoin = path.join

fastify.inject(
{
method: 'GET',
url: '/documentation/json'
},
(err, res) => {
t.error(err)
t.equal(err, null)
}
)
await fastify.inject({
method: 'GET',
url: '/documentation/json'
})
t.pass('no error should throw')
})
test('inserts default opts in fastifySwaggerDynamic (dynamic.js)', t => {
test('inserts default opts in fastifySwaggerDynamic (dynamic.js)', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwaggerDynamic)
await fastify.register(fastifySwaggerDynamic)
fastify.ready(() => {
t.pass('Inserted default option for fastifySwagger.')
})
t.pass('Inserted default option for fastifySwagger.')
})
test('/documentation/uiConfig should have default', t => {
test('/documentation/uiConfig should have default', async (t) => {
const config = {

@@ -796,17 +714,16 @@ exposeRoute: true,

t.plan(3)
t.plan(2)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/uiConfig'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, '{}')
})
t.equal(res.statusCode, 200)
t.equal(res.payload, '{}')
})
test('/documentation/uiConfig can be customize', t => {
test('/documentation/uiConfig can be customize', async (t) => {
const config = {

@@ -824,17 +741,16 @@ exposeRoute: true,

t.plan(3)
t.plan(2)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/uiConfig'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, '{"docExpansion":"full"}')
})
t.equal(res.statusCode, 200)
t.equal(res.payload, '{"docExpansion":"full"}')
})
test('/documentation/initOAuth should have default', t => {
test('/documentation/initOAuth should have default', async (t) => {
const config = {

@@ -849,17 +765,16 @@ exposeRoute: true,

t.plan(3)
t.plan(2)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/initOAuth'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, '{}')
})
t.equal(res.statusCode, 200)
t.equal(res.payload, '{}')
})
test('/documentation/initOAuth can be customize', t => {
test('/documentation/initOAuth can be customize', async (t) => {
const config = {

@@ -877,17 +792,16 @@ exposeRoute: true,

t.plan(3)
t.plan(2)
const fastify = new Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/initOAuth'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, '{"scopes":["openid","profile","email","offline_access"]}')
})
t.equal(res.statusCode, 200)
t.equal(res.payload, '{"scopes":["openid","profile","email","offline_access"]}')
})
test('should still return valid swagger object when missing package.json', t => {
test('should still return valid swagger object when missing package.json', async (t) => {
const config = {

@@ -901,5 +815,5 @@ mode: 'dynamic',

t.plan(3)
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, config)
await fastify.register(fastifySwagger, config)

@@ -916,16 +830,9 @@ const originalPathJoin = path.join

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
Swagger.validate(swaggerObject)
.then(function (api) {
t.pass('Swagger object is still valid.')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(swaggerObject)
t.pass('Swagger object is still valid.')
})

@@ -58,7 +58,7 @@ 'use strict'

test('/documentation/json route', t => {
t.plan(2)
test('/documentation/json route', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -72,22 +72,15 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/json'
}, (err, res) => {
t.error(err)
})
const payload = JSON.parse(res.payload)
const payload = JSON.parse(res.payload)
Swagger.validate(payload)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(payload)
t.pass('valid swagger object')
})
test('/documentation/uiConfig route', t => {
t.plan(2)
test('/documentation/uiConfig route', async (t) => {
t.plan(1)
const fastify = Fastify()

@@ -104,3 +97,3 @@

fastify.register(fastifySwagger, opts)
await fastify.register(fastifySwagger, opts)

@@ -114,16 +107,14 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/uiConfig'
}, (err, res) => {
t.error(err)
})
const payload = JSON.parse(res.payload)
const payload = JSON.parse(res.payload)
t.match(payload, uiConfig, 'uiConfig should be valid')
})
t.match(payload, uiConfig, 'uiConfig should be valid')
})
test('/documentation/initOAuth route', t => {
t.plan(2)
test('/documentation/initOAuth route', async (t) => {
t.plan(1)
const fastify = Fastify()

@@ -140,3 +131,3 @@

fastify.register(fastifySwagger, opts)
await fastify.register(fastifySwagger, opts)

@@ -150,19 +141,17 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/initOAuth'
}, (err, res) => {
t.error(err)
})
const payload = JSON.parse(res.payload)
const payload = JSON.parse(res.payload)
t.match(payload, initOAuth, 'initOAuth should be valid')
})
t.match(payload, initOAuth, 'initOAuth should be valid')
})
test('fastify.swagger should return a valid swagger yaml', t => {
t.plan(4)
test('fastify.swagger should return a valid swagger yaml', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -177,22 +166,17 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml'
}, (err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
try {
yaml.load(res.payload)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
yaml.load(res.payload)
t.pass('valid swagger yaml')
})
test('/documentation should redirect to ./documentation/static/index.html', t => {
t.plan(4)
test('/documentation should redirect to ./documentation/static/index.html', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -206,17 +190,15 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './documentation/static/index.html')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './documentation/static/index.html')
t.equal(typeof res.payload, 'string')
})
test('/documentation/ should redirect to ./static/index.html', t => {
t.plan(4)
test('/documentation/ should redirect to ./static/index.html', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -230,19 +212,17 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
test('/v1/documentation should redirect to ./documentation/static/index.html', t => {
t.plan(4)
test('/v1/documentation should redirect to ./documentation/static/index.html', async (t) => {
t.plan(3)
const fastify = Fastify()
const opts = JSON.parse(JSON.stringify(swaggerOption))
opts.routePrefix = '/v1/documentation'
fastify.register(fastifySwagger, opts)
await fastify.register(fastifySwagger, opts)

@@ -256,19 +236,17 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/v1/documentation'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './documentation/static/index.html')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './documentation/static/index.html')
t.equal(typeof res.payload, 'string')
})
test('/v1/documentation/ should redirect to ./static/index.html', t => {
t.plan(4)
test('/v1/documentation/ should redirect to ./static/index.html', async (t) => {
t.plan(3)
const fastify = Fastify()
const opts = JSON.parse(JSON.stringify(swaggerOption))
opts.routePrefix = '/v1/documentation'
fastify.register(fastifySwagger, opts)
await fastify.register(fastifySwagger, opts)

@@ -282,21 +260,19 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/v1/documentation/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
test('/v1/foobar should redirect to ./foobar/static/index.html - in plugin', t => {
t.plan(4)
test('/v1/foobar should redirect to ./foobar/static/index.html - in plugin', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(function (fastify, options, next) {
fastify.register(async function (fastify, options) {
const opts = JSON.parse(JSON.stringify(swaggerOption))
opts.routePrefix = '/foobar'
fastify.register(fastifySwagger, opts)
await fastify.register(fastifySwagger, opts)

@@ -309,25 +285,21 @@ fastify.get('/', () => {})

fastify.get('/example1', schemaSecurity, () => {})
next()
}, { prefix: '/v1' })
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/v1/foobar'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './foobar/static/index.html')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './foobar/static/index.html')
t.equal(typeof res.payload, 'string')
})
test('/v1/foobar/ should redirect to ./static/index.html - in plugin', t => {
t.plan(4)
test('/v1/foobar/ should redirect to ./static/index.html - in plugin', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(function (fastify, options, next) {
fastify.register(async function (fastify, options) {
const opts = JSON.parse(JSON.stringify(swaggerOption))
opts.routePrefix = '/foobar'
fastify.register(fastifySwagger, opts)
await fastify.register(fastifySwagger, opts)

@@ -340,19 +312,15 @@ fastify.get('/', () => {})

fastify.get('/example1', schemaSecurity, () => {})
next()
}, { prefix: '/v1' })
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/v1/foobar/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
test('with routePrefix: \'/\' should redirect to ./static/index.html', t => {
t.plan(4)
test('with routePrefix: \'/\' should redirect to ./static/index.html', async (t) => {
t.plan(3)
const fastify = Fastify()

@@ -362,22 +330,20 @@

opts.routePrefix = '/'
fastify.register(fastifySwagger, opts)
await fastify.register(fastifySwagger, opts)
fastify.get('/foo', () => {})
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
t.equal(typeof res.payload, 'string')
})
test('/documentation/static/:file should send back the correct file', t => {
t.plan(24)
test('/documentation/static/:file should send back the correct file', async (t) => {
t.plan(22)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -391,36 +357,53 @@ fastify.get('/', () => {})

fastify.inject({
method: 'GET',
url: '/documentation/'
}, (err, res) => {
t.error(err)
await fastify.ready()
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/'
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
})
}
fastify.ready(() => {
fastify.inject({
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/'
}, (err, res) => {
t.error(err)
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'text/html; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'static', 'index.html'),
'utf8'
),
res.payload
)
t.ok(res.payload.indexOf('resolveUrl') !== -1)
})
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'text/html; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'static', 'index.html'),
'utf8'
),
res.payload
)
t.ok(res.payload.indexOf('swagger-initializer.js') !== -1)
}
fastify.inject({
method: 'GET',
url: '/documentation/static/oauth2-redirect.html'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-initializer.js'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'static', 'swagger-initializer.js'),
'utf8'
),
res.payload
)
t.ok(res.payload.indexOf('resolveUrl') !== -1)
}
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/oauth2-redirect.html'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'text/html; charset=UTF-8')

@@ -434,9 +417,9 @@ t.equal(

)
})
}
fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui.css'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui.css'
})
t.equal(typeof res.payload, 'string')

@@ -451,9 +434,9 @@ t.equal(res.headers['content-type'], 'text/css; charset=UTF-8')

)
})
}
fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui-bundle.js'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui-bundle.js'
})
t.equal(typeof res.payload, 'string')

@@ -468,9 +451,9 @@ t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')

)
})
}
fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui-standalone-preset.js'
}, (err, res) => {
t.error(err)
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/swagger-ui-standalone-preset.js'
})
t.equal(typeof res.payload, 'string')

@@ -485,10 +468,10 @@ t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')

)
})
}
})
test('/documentation/static/:file 404', t => {
t.plan(3)
test('/documentation/static/:file 404', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -502,22 +485,20 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/stuff.css'
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 404)
t.match(payload, {
error: 'Not Found',
statusCode: 404
})
})
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 404)
t.match(payload, {
error: 'Not Found',
statusCode: 404
})
})
test('/documentation2/json route (overwrite)', t => {
t.plan(2)
test('/documentation2/json route (overwrite)', async (t) => {
t.plan(1)
const fastify = Fastify()
const swaggerOptionWithRouteOverwrite = JSON.parse(JSON.stringify(swaggerOption))
swaggerOptionWithRouteOverwrite.routePrefix = '/documentation2'
fastify.register(fastifySwagger, swaggerOptionWithRouteOverwrite)
await fastify.register(fastifySwagger, swaggerOptionWithRouteOverwrite)

@@ -532,36 +513,27 @@ fastify.get('/', () => {})

fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation2/json'
}, (err, res) => {
t.error(err)
})
const payload = JSON.parse(res.payload)
const payload = JSON.parse(res.payload)
Swagger.validate(payload)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(payload)
t.pass('valid swagger object')
})
test('/documentation/:myfile should return 404 in dynamic mode', t => {
t.plan(2)
test('/documentation/:myfile should return 404 in dynamic mode', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/swagger-ui.js'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
})
t.equal(res.statusCode, 404)
})
test('/documentation/:myfile should run custom NotFoundHandler in dynamic mode', t => {
t.plan(2)
test('/documentation/:myfile should run custom NotFoundHandler in dynamic mode', async (t) => {
t.plan(1)
const fastify = Fastify()

@@ -572,26 +544,22 @@ const notFoundHandler = function (req, reply) {

fastify.setNotFoundHandler(notFoundHandler)
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/swagger-ui.js'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 410)
})
t.equal(res.statusCode, 410)
})
test('/documentation/ should redirect to ./static/index.html', t => {
t.plan(3)
test('/documentation/ should redirect to ./static/index.html', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.inject({
const res = await fastify.inject({
method: 'GET',
url: '/documentation/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, './static/index.html')
})

@@ -11,37 +11,33 @@ 'use strict'

test('openapi should have default version', t => {
t.plan(2)
test('openapi should have default version', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: {} })
await fastify.register(fastifySwagger, { openapi: {} })
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(openapiObject.openapi, '3.0.3')
})
const openapiObject = fastify.swagger()
t.equal(openapiObject.openapi, '3.0.3')
})
test('openapi should have default info properties', t => {
t.plan(3)
test('openapi should have default info properties', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: {} })
await fastify.register(fastifySwagger, { openapi: {} })
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const pkg = readPackageJson(function () {})
t.equal(openapiObject.info.title, pkg.name)
t.equal(openapiObject.info.version, pkg.version)
})
const openapiObject = fastify.swagger()
const pkg = readPackageJson()
t.equal(openapiObject.info.title, pkg.name)
t.equal(openapiObject.info.version, pkg.version)
})
test('openapi basic properties', t => {
t.plan(5)
test('openapi basic properties', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -65,17 +61,15 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(openapiObject.info, openapiOption.openapi.info)
t.equal(openapiObject.servers, openapiOption.openapi.servers)
t.ok(openapiObject.paths)
t.ok(openapiObject.paths['/'])
})
const openapiObject = fastify.swagger()
t.equal(openapiObject.info, openapiOption.openapi.info)
t.equal(openapiObject.servers, openapiOption.openapi.servers)
t.ok(openapiObject.paths)
t.ok(openapiObject.paths['/'])
})
test('openapi components', t => {
t.plan(2)
test('openapi components', async (t) => {
t.plan(1)
const fastify = Fastify()

@@ -99,20 +93,18 @@

fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.get('/', () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.same(openapiObject.components.schemas, openapiOption.openapi.components.schemas)
delete openapiOption.openapi.components.schemas // remove what we just added
})
const openapiObject = fastify.swagger()
t.same(openapiObject.components.schemas, openapiOption.openapi.components.schemas)
delete openapiOption.openapi.components.schemas // remove what we just added
})
test('hide support when property set in transform() - property', t => {
t.plan(2)
test('hide support when property set in transform() - property', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...openapiOption,

@@ -141,17 +133,15 @@ transform: ({ schema, url }) => {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
test('hide support - tags Default', t => {
t.plan(2)
test('hide support - tags Default', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -176,17 +166,15 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
test('hide support - tags Custom', t => {
t.plan(2)
test('hide support - tags Custom', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, { ...openapiOption, hiddenTag: 'NOP' })
await fastify.register(fastifySwagger, { ...openapiOption, hiddenTag: 'NOP' })

@@ -211,17 +199,15 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
test('hide support - hidden untagged', t => {
t.plan(2)
test('hide support - hidden untagged', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, { ...openapiOption, hideUntagged: true })
await fastify.register(fastifySwagger, { ...openapiOption, hideUntagged: true })

@@ -245,17 +231,15 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/'])
})
test('basePath support', t => {
t.plan(3)
test('basePath support', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: Object.assign({}, openapiOption.openapi, {

@@ -272,17 +256,15 @@ servers: [

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/prefix/endpoint'])
t.ok(openapiObject.paths['/endpoint'])
})
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths['/prefix/endpoint'])
t.ok(openapiObject.paths['/endpoint'])
})
test('basePath maintained when stripBasePath is set to false', t => {
t.plan(4)
test('basePath maintained when stripBasePath is set to false', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
stripBasePath: false,

@@ -300,62 +282,46 @@ openapi: Object.assign({}, openapiOption.openapi, {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths.endpoint)
t.notOk(openapiObject.paths['/endpoint'])
t.ok(openapiObject.paths['/foo/endpoint'])
})
const openapiObject = fastify.swagger()
t.notOk(openapiObject.paths.endpoint)
t.notOk(openapiObject.paths['/endpoint'])
t.ok(openapiObject.paths['/foo/endpoint'])
})
test('cache - json', t => {
t.plan(3)
test('cache - json', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.ready(err => {
t.error(err)
await fastify.ready()
fastify.swagger()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
fastify.swagger()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(openapiObject)
t.pass('valid swagger object')
})
test('cache - yaml', t => {
t.plan(3)
test('cache - yaml', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.ready(err => {
t.error(err)
await fastify.ready()
fastify.swagger({ yaml: true })
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
try {
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
})
fastify.swagger({ yaml: true })
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
})
test('transforms examples in example if single string example', t => {
t.plan(4)
test('transforms examples in example if single string example', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -377,21 +343,19 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema
t.ok(schema)
t.notOk(schema.properties.hello.examples)
t.equal(schema.properties.hello.example, 'world')
})
t.ok(schema)
t.notOk(schema.properties.hello.examples)
t.equal(schema.properties.hello.example, 'world')
})
test('transforms examples in example if single object example', t => {
t.plan(4)
test('transforms examples in example if single object example', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -418,21 +382,19 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema
t.ok(schema)
t.notOk(schema.properties.hello.examples)
t.same(schema.properties.hello.example, { lorem: 'ipsum' })
})
t.ok(schema)
t.notOk(schema.properties.hello.examples)
t.same(schema.properties.hello.example, { lorem: 'ipsum' })
})
test('uses examples if has multiple string examples', t => {
t.plan(4)
test('uses examples if has multiple string examples', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -454,28 +416,26 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
hello: {
value: 'hello'
},
world: {
value: 'world'
}
})
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
hello: {
value: 'hello'
},
world: {
value: 'world'
}
})
})
test('uses examples if has multiple numbers examples', t => {
t.plan(4)
test('uses examples if has multiple numbers examples', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -497,28 +457,26 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
1: {
value: 1
},
2: {
value: 2
}
})
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
1: {
value: 1
},
2: {
value: 2
}
})
})
test('uses examples if has multiple object examples', t => {
t.plan(4)
test('uses examples if has multiple object examples', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -545,32 +503,30 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
example1: {
value: {
lorem: 'ipsum'
}
},
example2: {
value: {
hello: 'world'
}
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
example1: {
value: {
lorem: 'ipsum'
}
})
},
example2: {
value: {
hello: 'world'
}
}
})
})
test('uses examples if has multiple array examples', t => {
t.plan(4)
test('uses examples if has multiple array examples', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -595,36 +551,34 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
example1: {
value: [
'a',
'b',
'c'
]
},
example2: {
value: [
'd',
'f',
'g'
]
}
})
t.ok(schema)
t.ok(schema.properties.hello.examples)
t.same(schema.properties.hello.examples, {
example1: {
value: [
'a',
'b',
'c'
]
},
example2: {
value: [
'd',
'f',
'g'
]
}
})
})
test('uses examples if has property required in body', t => {
t.plan(5)
test('uses examples if has property required in body', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -647,18 +601,16 @@ const body = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema
const requestBody = openapiObject.paths['/'].get.requestBody
const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema
const requestBody = openapiObject.paths['/'].post.requestBody
t.ok(schema)
t.ok(schema.properties)
t.same(body.required, ['hello'])
t.same(requestBody.required, true)
})
t.ok(schema)
t.ok(schema.properties)
t.same(body.required, ['hello'])
t.same(requestBody.required, true)
})
module.exports = { openapiOption }

@@ -20,3 +20,3 @@ 'use strict'

fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.register(async (instance) => {

@@ -39,3 +39,3 @@ instance.addSchema({ $id: 'Order', type: 'object', properties: { id: { type: 'integer' } } })

fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.register(async (instance) => {

@@ -70,3 +70,3 @@ instance.addSchema({

const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.register(async (instance) => {

@@ -96,3 +96,3 @@ instance.addSchema({ $id: 'OrderItem', type: 'object', properties: { id: { type: 'integer' } } })

const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.register(async (instance) => {

@@ -126,3 +126,3 @@ instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })

fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.register(function (instance, _, done) {

@@ -175,3 +175,3 @@ instance.addSchema({ $id: 'order', type: 'string', enum: ['foo'] })

const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: {} })
await fastify.register(fastifySwagger, { openapi: {} })
fastify.register(async (instance) => {

@@ -178,0 +178,0 @@ instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })

@@ -24,7 +24,7 @@ 'use strict'

test('openapi should return a valid swagger object', t => {
t.plan(3)
test('openapi should return a valid swagger object', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -40,23 +40,16 @@ fastify.get('/', () => {})

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(openapiObject)
t.pass('valid swagger object')
})
test('openapi should return a valid swagger yaml', t => {
t.plan(3)
test('openapi should return a valid swagger yaml', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -72,22 +65,15 @@ fastify.get('/', () => {})

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
try {
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
})
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
})
test('route options - deprecated', t => {
t.plan(3)
test('route options - deprecated', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -112,24 +98,18 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
const openapiObject = fastify.swagger()
await fastify.ready()
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
t.ok(openapiObject.paths['/'])
})
.catch(function (err) {
t.fail(err)
})
})
const openapiObject = fastify.swagger()
await Swagger.validate(openapiObject)
t.pass('valid swagger object')
t.ok(openapiObject.paths['/'])
})
test('route options - meta', t => {
t.plan(8)
test('route options - meta', async (t) => {
t.plan(7)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -156,157 +136,129 @@ const opts = {

fastify.ready(err => {
t.error(err)
const openapiObject = fastify.swagger()
await fastify.ready()
Swagger.validate(openapiObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.equal(opts.schema.operationId, definedPath.operationId)
t.equal(opts.schema.summary, definedPath.summary)
t.same(opts.schema.tags, definedPath.tags)
t.equal(opts.schema.description, definedPath.description)
t.equal(opts.schema.servers, definedPath.servers)
t.equal(opts.schema.externalDocs, definedPath.externalDocs)
})
.catch(function (err) {
t.fail(err)
})
})
const openapiObject = fastify.swagger()
const api = await Swagger.validate(openapiObject)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.equal(opts.schema.operationId, definedPath.operationId)
t.equal(opts.schema.summary, definedPath.summary)
t.same(opts.schema.tags, definedPath.tags)
t.equal(opts.schema.description, definedPath.description)
t.equal(opts.schema.servers, definedPath.servers)
t.equal(opts.schema.externalDocs, definedPath.externalDocs)
})
test('route options - produces', t => {
t.plan(3)
test('route options - produces', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.get('/', schemaProduces, () => {})
fastify.ready(err => {
t.error(err)
const openapiObject = fastify.swagger()
await fastify.ready()
Swagger.validate(openapiObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.responses[200].content, {
'*/*': {
schema: {
type: 'object',
properties: {
hello: {
description: 'hello',
type: 'string'
}
},
required: ['hello']
}
const openapiObject = fastify.swagger()
const api = await Swagger.validate(openapiObject)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.responses[200].content, {
'*/*': {
schema: {
type: 'object',
properties: {
hello: {
description: 'hello',
type: 'string'
}
})
})
.catch(function (err) {
t.fail(err)
})
},
required: ['hello']
}
}
})
})
test('route options - cookies', t => {
t.plan(3)
test('route options - cookies', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.get('/', schemaCookies, () => {})
fastify.ready(err => {
t.error(err)
const openapiObject = fastify.swagger()
Swagger.validate(openapiObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.parameters, [
{
required: false,
in: 'cookie',
name: 'bar',
schema: {
type: 'string'
}
}
])
})
.catch(function (err) {
t.fail(err)
})
})
await fastify.ready()
const openapiObject = fastify.swagger()
const api = await Swagger.validate(openapiObject)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.parameters, [
{
required: false,
in: 'cookie',
name: 'bar',
schema: {
type: 'string'
}
}
])
})
test('route options - extension', t => {
t.plan(5)
test('route options - extension', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: { 'x-ternal': true } })
await fastify.register(fastifySwagger, { openapi: { 'x-ternal': true } })
fastify.get('/', schemaExtension, () => {})
fastify.ready(err => {
t.error(err)
const openapiObject = fastify.swagger()
await fastify.ready()
Swagger.validate(openapiObject)
.then(function (api) {
t.ok(api['x-ternal'])
t.same(api['x-ternal'], true)
const openapiObject = fastify.swagger()
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath['x-tension'], true)
})
.catch(function (err) {
t.fail(err)
})
})
const api = await Swagger.validate(openapiObject)
t.ok(api['x-ternal'])
t.same(api['x-ternal'], true)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath['x-tension'], true)
})
test('parses form parameters when all api consumes application/x-www-form-urlencoded', t => {
t.plan(3)
test('parses form parameters when all api consumes application/x-www-form-urlencoded', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
fastify.get('/', schemaConsumes, () => {})
await fastify.register(fastifySwagger, openapiOption)
fastify.post('/', schemaConsumes, () => {})
fastify.ready(err => {
t.error(err)
const openapiObject = fastify.swagger()
await fastify.ready()
Swagger.validate(openapiObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.requestBody.content, {
'application/x-www-form-urlencoded': {
schema: {
type: 'object',
properties: {
hello: {
description: 'hello',
type: 'string'
}
},
required: ['hello']
}
const openapiObject = fastify.swagger()
const api = await Swagger.validate(openapiObject)
const definedPath = api.paths['/'].post
t.ok(definedPath)
t.same(definedPath.requestBody.content, {
'application/x-www-form-urlencoded': {
schema: {
type: 'object',
properties: {
hello: {
description: 'hello',
type: 'string'
}
})
})
.catch(function (err) {
t.fail(err)
})
},
required: ['hello']
}
}
})
})
test('route options - method', t => {
t.plan(3)
test('route options - method', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -321,23 +273,16 @@ fastify.route({

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(openapiObject)
t.pass('valid swagger object')
})
test('cookie, query, path description', t => {
t.plan(7)
test('cookie, query, path description', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -380,51 +325,44 @@ const schemaCookies = {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
Swagger.validate(openapiObject)
.then(function (api) {
const cookiesPath = api.paths['/'].get
t.ok(cookiesPath)
t.same(cookiesPath.parameters, [
{
required: false,
in: 'cookie',
name: 'bar',
description: 'Bar',
schema: {
type: 'string'
}
}
])
const querystringPath = api.paths['/example'].get
t.ok(querystringPath)
t.same(querystringPath.parameters, [
{
required: false,
in: 'query',
name: 'hello',
description: 'Hello',
schema: {
type: 'string'
}
}
])
const paramPath = api.paths['/parameters/{id}'].get
t.ok(paramPath)
t.same(paramPath.parameters, [
{
required: true,
in: 'path',
name: 'id',
schema: {
type: 'string'
}
}
])
})
.catch(function (err) {
t.fail(err)
})
})
const openapiObject = fastify.swagger()
const api = await Swagger.validate(openapiObject)
const cookiesPath = api.paths['/'].get
t.ok(cookiesPath)
t.same(cookiesPath.parameters, [
{
required: false,
in: 'cookie',
name: 'bar',
description: 'Bar',
schema: {
type: 'string'
}
}
])
const querystringPath = api.paths['/example'].get
t.ok(querystringPath)
t.same(querystringPath.parameters, [
{
required: false,
in: 'query',
name: 'hello',
description: 'Hello',
schema: {
type: 'string'
}
}
])
const paramPath = api.paths['/parameters/{id}'].get
t.ok(paramPath)
t.same(paramPath.parameters, [
{
required: true,
in: 'path',
name: 'id',
schema: {
type: 'string'
}
}
])
})

@@ -434,5 +372,15 @@

t.plan(4)
const fastify = Fastify()
const fastify = Fastify({
ajv: {
plugins: [
function (ajv) {
ajv.addKeyword({
keyword: 'x-consume'
})
}
]
}
})
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -529,31 +477,24 @@ const schemaCookies = {

test('openapi should pass through operationId', t => {
t.plan(3)
test('openapi should pass through operationId', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.get('/hello', schemaOperationId, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(openapiObject)
t.pass('valid swagger object')
})
test('openapi should pass through Links', t => {
t.plan(4)
test('openapi should pass through Links', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -617,31 +558,25 @@ fastify.get('/user/:id', {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
t.same(api.paths['/user/{id}'].get.responses['200'].links, {
address: {
operationId: 'getUserAddress',
parameters: {
id: '$request.path.id'
}
}
})
})
.catch(function (err) {
t.fail(err)
})
const api = await Swagger.validate(openapiObject)
t.pass('valid swagger object')
t.same(api.paths['/user/{id}'].get.responses['200'].links, {
address: {
operationId: 'getUserAddress',
parameters: {
id: '$request.path.id'
}
}
})
})
test('links without status code', t => {
t.plan(2)
test('links without status code', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -705,13 +640,12 @@ fastify.get('/user/:id', {

fastify.ready(err => {
t.error(err)
t.throws(() => fastify.swagger(), new Error('missing status code 201 in route /user/:id'))
})
await fastify.ready()
t.throws(() => fastify.swagger(), new Error('missing status code 201 in route /user/:id'))
})
test('security headers ignored when declared in security and securityScheme', t => {
t.plan(7)
test('security headers ignored when declared in security and securityScheme', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)

@@ -754,27 +688,20 @@ fastify.get('/address1/:id', {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
.catch(function (err) {
t.error(err)
})
})
const api = await Swagger.validate(openapiObject)
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
test('security querystrings ignored when declared in security and securityScheme', t => {
t.plan(7)
test('security querystrings ignored when declared in security and securityScheme', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: {

@@ -832,27 +759,20 @@ components: {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
.catch(function (err) {
t.error(err)
})
})
const api = await Swagger.validate(openapiObject)
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
test('security cookies ignored when declared in security and securityScheme', t => {
t.plan(7)
test('security cookies ignored when declared in security and securityScheme', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: {

@@ -910,27 +830,20 @@ components: {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
const openapiObject = fastify.swagger()
t.equal(typeof openapiObject, 'object')
Swagger.validate(openapiObject)
.then(function (api) {
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
.catch(function (err) {
t.error(err)
})
})
const api = await Swagger.validate(openapiObject)
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
test('path params on relative url', t => {
t.plan(3)
test('path params on relative url', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiRelativeOptions)
await fastify.register(fastifySwagger, openapiRelativeOptions)

@@ -949,25 +862,18 @@ const schemaParams = {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const openapiObject = fastify.swagger()
Swagger.validate(openapiObject)
.then(function (api) {
const paramPath = api.paths['/parameters/{id}'].get
t.ok(paramPath)
t.same(paramPath.parameters, [
{
required: true,
in: 'path',
name: 'id',
schema: {
type: 'string'
}
}
])
})
.catch(function (err) {
t.fail(err)
})
})
const openapiObject = fastify.swagger()
const api = await Swagger.validate(openapiObject)
const paramPath = api.paths['/parameters/{id}'].get
t.ok(paramPath)
t.same(paramPath.parameters, [
{
required: true,
in: 'path',
name: 'id',
schema: {
type: 'string'
}
}
])
})

@@ -13,32 +13,26 @@ 'use strict'

test('support - oneOf, anyOf, allOf', t => {
t.plan(3)
test('support - oneOf, anyOf, allOf', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.get('/', schemaAllOf, () => {})
fastify.ready(err => {
t.error(err)
const openapiObject = fastify.swagger()
Swagger.validate(openapiObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.parameters, [
{
required: false,
in: 'query',
name: 'foo',
schema: {
type: 'string'
}
}
])
})
.catch(function (err) {
t.fail(err)
})
})
await fastify.ready()
const openapiObject = fastify.swagger()
const api = await Swagger.validate(openapiObject)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.parameters, [
{
required: false,
in: 'query',
name: 'foo',
schema: {
type: 'string'
}
}
])
})

@@ -61,3 +55,3 @@

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -92,3 +86,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -127,3 +121,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -175,3 +169,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -210,3 +204,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.get('/description', {

@@ -242,3 +236,3 @@ schema: {

const fastify = Fastify()
fastify.register(fastifySwagger, openapiOption)
await fastify.register(fastifySwagger, openapiOption)
fastify.get('/responseDescription', {

@@ -287,3 +281,3 @@ schema: {

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -313,3 +307,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: true, routePrefix: '/docs', exposeRoute: true })
await fastify.register(fastifySwagger, { openapi: true, routePrefix: '/docs', exposeRoute: true })
fastify.addSchema({ ...schema, $id: 'requiredUniqueSchema' })

@@ -334,3 +328,3 @@ fastify.get('/', { schema: { query: { $ref: 'requiredUniqueSchema' } } }, () => {})

const fastify = Fastify()
fastify.register(fastifySwagger, { openapi: true, routePrefix: '/docs', exposeRoute: true })
await fastify.register(fastifySwagger, { openapi: true, routePrefix: '/docs', exposeRoute: true })
fastify.addSchema({ ...schema, $id: 'requiredUniqueSchema' })

@@ -372,3 +366,3 @@ fastify.get('/', { schema: { query: { $ref: 'requiredUniqueSchema' } } }, () => {})

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -415,3 +409,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -456,3 +450,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -517,3 +511,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -574,3 +568,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -580,3 +574,3 @@ routePrefix: '/docs',

})
fastify.get('/', opt, () => {})
fastify.post('/', opt, () => {})
await fastify.ready()

@@ -587,3 +581,3 @@

const definedPath = api.paths['/'].get
const definedPath = api.paths['/'].post
t.same(definedPath.requestBody, {

@@ -629,3 +623,3 @@ content: {

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -635,3 +629,3 @@ routePrefix: '/docs',

})
fastify.get('/', opt, () => { })
fastify.post('/', opt, () => { })
await fastify.ready()

@@ -642,3 +636,3 @@

const definedPath = api.paths['/'].get
const definedPath = api.paths['/'].post
t.same(definedPath.requestBody, {

@@ -693,3 +687,3 @@ content: {

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
openapi: true,

@@ -699,3 +693,3 @@ routePrefix: '/docs',

})
fastify.get('/', opt, () => { })
fastify.post('/', opt, () => { })
await fastify.ready()

@@ -706,3 +700,3 @@

const definedPath = api.paths['/'].get
const definedPath = api.paths['/'].post
t.same(definedPath.requestBody, {

@@ -752,4 +746,13 @@ content: {

const fastify = Fastify()
fastify.register(fastifySwagger, {
const fastify = Fastify({
ajv: {
plugins: [
function (ajv) {
ajv.addKeyword({ keyword: 'style' })
ajv.addKeyword({ keyword: 'explode' })
}
]
}
})
await fastify.register(fastifySwagger, {
openapi: true,

@@ -764,4 +767,4 @@ routePrefix: '/docs',

const api = await Swagger.validate(swaggerObject)
t.is(api.paths['/'].get.parameters[0].style, 'deepObject')
t.is(api.paths['/'].get.parameters[0].explode, false)
t.equal(api.paths['/'].get.parameters[0].style, 'deepObject')
t.equal(api.paths['/'].get.parameters[0].explode, false)
})

@@ -11,37 +11,34 @@ 'use strict'

test('swagger should have default version', t => {
t.plan(2)
test('swagger should have default version', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger)
await fastify.register(fastifySwagger)
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.swagger, '2.0')
})
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.swagger, '2.0')
})
test('swagger should have default info properties', t => {
t.plan(3)
test('swagger should have default info properties', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger)
await fastify.register(fastifySwagger)
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
const pkg = readPackageJson(function () {})
t.equal(swaggerObject.info.title, pkg.name)
t.equal(swaggerObject.info.version, pkg.version)
})
const swaggerObject = fastify.swagger()
const pkg = readPackageJson()
t.equal(swaggerObject.info.title, pkg.name)
t.equal(swaggerObject.info.version, pkg.version)
})
test('swagger basic properties', t => {
t.plan(6)
test('swagger basic properties', async (t) => {
t.plan(5)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -65,18 +62,16 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.info, swaggerOption.swagger.info)
t.equal(swaggerObject.host, swaggerOption.swagger.host)
t.equal(swaggerObject.schemes, swaggerOption.swagger.schemes)
t.ok(swaggerObject.paths)
t.ok(swaggerObject.paths['/'])
})
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.info, swaggerOption.swagger.info)
t.equal(swaggerObject.host, swaggerOption.swagger.host)
t.equal(swaggerObject.schemes, swaggerOption.swagger.schemes)
t.ok(swaggerObject.paths)
t.ok(swaggerObject.paths['/'])
})
test('swagger definitions', t => {
t.plan(2)
test('swagger definitions', async (t) => {
t.plan(1)
const fastify = Fastify()

@@ -100,46 +95,40 @@

fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.same(swaggerObject.definitions, swaggerOption.swagger.definitions)
delete swaggerOption.swagger.definitions // remove what we just added
})
const swaggerObject = fastify.swagger()
t.same(swaggerObject.definitions, swaggerOption.swagger.definitions)
delete swaggerOption.swagger.definitions // remove what we just added
})
test('swagger tags', t => {
t.plan(2)
test('swagger tags', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.tags, swaggerOption.swagger.tags)
})
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.tags, swaggerOption.swagger.tags)
})
test('swagger externalDocs', t => {
t.plan(2)
test('swagger externalDocs', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.externalDocs, swaggerOption.swagger.externalDocs)
})
const swaggerObject = fastify.swagger()
t.equal(swaggerObject.externalDocs, swaggerOption.swagger.externalDocs)
})
test('basePath support', t => {
t.plan(3)
test('basePath support', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
swagger: Object.assign({}, swaggerOption.swagger, {

@@ -152,16 +141,14 @@ basePath: '/prefix'

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/prefix/endpoint'])
t.ok(swaggerObject.paths['/endpoint'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/prefix/endpoint'])
t.ok(swaggerObject.paths['/endpoint'])
})
test('basePath support with prefix', t => {
t.plan(3)
test('basePath support with prefix', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
prefix: '/prefix',

@@ -175,16 +162,14 @@ swagger: Object.assign({}, swaggerOption.swagger, {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/prefix/endpoint'])
t.ok(swaggerObject.paths['/endpoint'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/prefix/endpoint'])
t.ok(swaggerObject.paths['/endpoint'])
})
test('basePath ensure leading slash', t => {
t.plan(3)
test('basePath ensure leading slash', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
swagger: Object.assign({}, swaggerOption.swagger, {

@@ -197,16 +182,14 @@ basePath: '/'

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths.endpoint)
t.ok(swaggerObject.paths['/endpoint'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths.endpoint)
t.ok(swaggerObject.paths['/endpoint'])
})
test('basePath with prefix ensure leading slash', t => {
t.plan(3)
test('basePath with prefix ensure leading slash', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
prefix: '/',

@@ -220,17 +203,15 @@ swagger: Object.assign({}, swaggerOption.swagger, {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths.endpoint)
t.ok(swaggerObject.paths['/endpoint'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths.endpoint)
t.ok(swaggerObject.paths['/endpoint'])
})
test('basePath maintained when stripBasePath is set to false', t => {
t.plan(4)
test('basePath maintained when stripBasePath is set to false', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
stripBasePath: false,

@@ -244,10 +225,8 @@ swagger: Object.assign({}, swaggerOption.swagger, {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths.endpoint)
t.notOk(swaggerObject.paths['/endpoint'])
t.ok(swaggerObject.paths['/foo/endpoint'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths.endpoint)
t.notOk(swaggerObject.paths['/endpoint'])
t.ok(swaggerObject.paths['/foo/endpoint'])
})

@@ -257,7 +236,7 @@

test('hide support - property', t => {
t.plan(2)
test('hide support - property', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -282,17 +261,15 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
test('hide support when property set in transform() - property', t => {
t.plan(2)
test('hide support when property set in transform() - property', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
...swaggerOption,

@@ -321,17 +298,15 @@ transform: ({ schema, url }) => {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
test('hide support - tags Default', t => {
t.plan(2)
test('hide support - tags Default', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -356,17 +331,15 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
test('hide support - tags Custom', t => {
t.plan(2)
test('hide support - tags Custom', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, { ...swaggerOption, hiddenTag: 'NOP' })
await fastify.register(fastifySwagger, { ...swaggerOption, hiddenTag: 'NOP' })

@@ -391,17 +364,15 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
test('hide support - hidden untagged', t => {
t.plan(2)
test('hide support - hidden untagged', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, { ...swaggerOption, hideUntagged: true })
await fastify.register(fastifySwagger, { ...swaggerOption, hideUntagged: true })

@@ -425,57 +396,41 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
const swaggerObject = fastify.swagger()
t.notOk(swaggerObject.paths['/'])
})
test('cache - json', t => {
t.plan(3)
test('cache - json', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.ready(err => {
t.error(err)
await fastify.ready()
fastify.swagger()
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
fastify.swagger()
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
Swagger.validate(swaggerObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(swaggerObject)
t.pass('valid swagger object')
})
test('cache - yaml', t => {
t.plan(3)
test('cache - yaml', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.ready(err => {
t.error(err)
await fastify.ready()
fastify.swagger({ yaml: true })
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
try {
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
})
fastify.swagger({ yaml: true })
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
})
module.exports = { swaggerOption }

@@ -20,3 +20,3 @@ 'use strict'

fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -79,3 +79,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, options)
await fastify.register(fastifySwagger, options)
fastify.register(async (instance) => {

@@ -107,3 +107,3 @@ instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -110,0 +110,0 @@ exposeRoute: true

@@ -20,7 +20,7 @@ 'use strict'

test('swagger should return valid swagger object', t => {
t.plan(3)
test('swagger should return valid swagger object', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -36,23 +36,16 @@ fastify.get('/', () => {})

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
Swagger.validate(swaggerObject)
.then(function (api) {
t.pass('valid swagger object')
})
.catch(function (err) {
t.fail(err)
})
})
await Swagger.validate(swaggerObject)
t.pass('valid swagger object')
})
test('swagger should return a valid swagger yaml', t => {
t.plan(3)
test('swagger should return a valid swagger yaml', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -68,22 +61,15 @@ fastify.get('/', () => {})

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
try {
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
} catch (err) {
t.fail(err)
}
})
const swaggerYaml = fastify.swagger({ yaml: true })
t.equal(typeof swaggerYaml, 'string')
yaml.load(swaggerYaml)
t.pass('valid swagger yaml')
})
test('route options - deprecated', t => {
t.plan(3)
test('route options - deprecated', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -108,24 +94,18 @@ const opts = {

fastify.get('/', opts, () => {})
fastify.post('/', opts, () => {})
fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
await fastify.ready()
Swagger.validate(swaggerObject)
.then(function (api) {
t.pass('valid swagger object')
t.ok(swaggerObject.paths['/'])
})
.catch(function (err) {
t.fail(err)
})
})
const swaggerObject = fastify.swagger()
await Swagger.validate(swaggerObject)
t.pass('valid swagger object')
t.ok(swaggerObject.paths['/'])
})
test('route options - meta', t => {
t.plan(9)
test('route options - meta', async (t) => {
t.plan(8)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -149,79 +129,61 @@ const opts = {

fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
await fastify.ready()
Swagger.validate(swaggerObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.equal(opts.schema.operationId, definedPath.operationId)
t.equal(opts.schema.summary, definedPath.summary)
t.same(opts.schema.tags, definedPath.tags)
t.equal(opts.schema.description, definedPath.description)
t.same(opts.schema.produces, definedPath.produces)
t.same(opts.schema.consumes, definedPath.consumes)
t.equal(opts.schema.externalDocs, definedPath.externalDocs)
})
.catch(function (err) {
t.fail(err)
})
})
const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.equal(opts.schema.operationId, definedPath.operationId)
t.equal(opts.schema.summary, definedPath.summary)
t.same(opts.schema.tags, definedPath.tags)
t.equal(opts.schema.description, definedPath.description)
t.same(opts.schema.produces, definedPath.produces)
t.same(opts.schema.consumes, definedPath.consumes)
t.equal(opts.schema.externalDocs, definedPath.externalDocs)
})
test('route options - consumes', t => {
t.plan(3)
test('route options - consumes', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
fastify.get('/', schemaConsumes, () => {})
await fastify.register(fastifySwagger, swaggerOption)
fastify.post('/', schemaConsumes, () => {})
fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
await fastify.ready()
Swagger.validate(swaggerObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.parameters, [{
in: 'formData',
name: 'hello',
description: 'hello',
required: true,
type: 'string'
}])
})
.catch(function (err) {
t.fail(err)
})
})
const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)
const definedPath = api.paths['/'].post
t.ok(definedPath)
t.same(definedPath.parameters, [{
in: 'formData',
name: 'hello',
description: 'hello',
required: true,
type: 'string'
}])
})
test('route options - extension', t => {
t.plan(5)
test('route options - extension', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifySwagger, { swagger: { 'x-ternal': true } })
await fastify.register(fastifySwagger, { swagger: { 'x-ternal': true } })
fastify.get('/', schemaExtension, () => {})
fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
await fastify.ready()
Swagger.validate(swaggerObject)
.then(function (api) {
t.ok(api['x-ternal'])
t.same(api['x-ternal'], true)
const swaggerObject = fastify.swagger()
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath['x-tension'], true)
})
.catch(function (err) {
t.fail(err)
})
})
const api = await Swagger.validate(swaggerObject)
t.ok(api['x-ternal'])
t.same(api['x-ternal'], true)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath['x-tension'], true)
})
test('route options - querystring', t => {
t.plan(3)
test('route options - querystring', async (t) => {
t.plan(2)

@@ -242,37 +204,31 @@ const opts = {

const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.get('/', opts, () => {})
fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
await fastify.ready()
Swagger.validate(swaggerObject)
.then(function (api) {
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.parameters, [
{
in: 'query',
name: 'hello',
type: 'string',
required: true
},
{
in: 'query',
name: 'world',
type: 'string',
required: false,
description: 'world description'
}
])
})
.catch(function (err) {
t.fail(err)
})
})
const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)
const definedPath = api.paths['/'].get
t.ok(definedPath)
t.same(definedPath.parameters, [
{
in: 'query',
name: 'hello',
type: 'string',
required: true
},
{
in: 'query',
name: 'world',
type: 'string',
required: false,
description: 'world description'
}
])
})
test('swagger json output should not omit enum part in params config', t => {
t.plan(3)
test('swagger json output should not omit enum part in params config', async (t) => {
t.plan(2)
const opts = {

@@ -290,28 +246,23 @@ schema: {

const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.get('/test/:enumKey', opts, () => {})
fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
Swagger.validate(swaggerObject)
.then((api) => {
const definedPath = api.paths['/test/{enumKey}'].get
t.ok(definedPath)
t.same(definedPath.parameters, [{
in: 'path',
name: 'enumKey',
type: 'string',
enum: ['enum1', 'enum2'],
required: true
}])
})
.catch(err => {
t.error(err)
})
})
await fastify.ready()
const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)
const definedPath = api.paths['/test/{enumKey}'].get
t.ok(definedPath)
t.same(definedPath.parameters, [{
in: 'path',
name: 'enumKey',
type: 'string',
enum: ['enum1', 'enum2'],
required: true
}])
})
test('custom verbs should not be interpreted as path params', t => {
t.plan(3)
test('custom verbs should not be interpreted as path params', async (t) => {
t.plan(2)
const opts = {

@@ -329,25 +280,18 @@ schema: {

const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)
fastify.get('/resource/:id/sub-resource::watch', opts, () => {})
fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
await fastify.ready()
Swagger.validate(swaggerObject)
.then((api) => {
const definedPath = api.paths['/resource/{id}/sub-resource:watch'].get
t.ok(definedPath)
t.same(definedPath.parameters, [{
in: 'path',
name: 'id',
type: 'string',
required: true
}])
})
.catch(err => {
console.log(err)
t.error(err)
})
})
const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)
const definedPath = api.paths['/resource/{id}/sub-resource:watch'].get
t.ok(definedPath)
t.same(definedPath.parameters, [{
in: 'path',
name: 'id',
type: 'string',
required: true
}])
})

@@ -357,5 +301,13 @@

t.plan(1)
const fastify = Fastify()
const fastify = Fastify({
ajv: {
plugins: [
function (ajv) {
ajv.addKeyword({ keyword: 'x-consume' })
}
]
}
})
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -397,7 +349,7 @@ const schemaQuerystring = {

test('swagger should not support Links', t => {
t.plan(2)
test('swagger should not support Links', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -461,14 +413,12 @@ fastify.get('/user/:id', {

fastify.ready(err => {
t.error(err)
await fastify.ready()
t.throws(() => fastify.swagger(), new Error('Swagger (Open API v2) does not support Links. Upgrade to OpenAPI v3 (see fastify-swagger readme)'))
})
t.throws(() => fastify.swagger(), new Error('Swagger (Open API v2) does not support Links. Upgrade to OpenAPI v3 (see @fastify/swagger readme)'))
})
test('security headers ignored when declared in security and securityScheme', t => {
t.plan(7)
test('security headers ignored when declared in security and securityScheme', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwagger, swaggerOption)

@@ -523,27 +473,20 @@ fastify.get('/address1/:id', {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
Swagger.validate(swaggerObject)
.then(function (api) {
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
.catch(function (err) {
t.error(err)
})
})
const api = await Swagger.validate(swaggerObject)
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
test('security querystrings ignored when declared in security and securityScheme', t => {
t.plan(7)
test('security querystrings ignored when declared in security and securityScheme', async (t) => {
t.plan(6)
const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
swagger: {

@@ -611,20 +554,13 @@ securityDefinitions: {

fastify.ready(err => {
t.error(err)
await fastify.ready()
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')
Swagger.validate(swaggerObject)
.then(function (api) {
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'somethingElse')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'somethingElse')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})
.catch(function (err) {
t.error(err)
})
})
const api = await Swagger.validate(swaggerObject)
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'somethingElse')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'somethingElse')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})

@@ -28,7 +28,7 @@ 'use strict'

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',
exposeRoute: true
})
fastify.get('/', opts7, () => {})
fastify.post('/', opts7, () => {})

@@ -40,3 +40,3 @@ await fastify.ready()

const definedPath = api.paths['/'].get
const definedPath = api.paths['/'].post
t.ok(definedPath)

@@ -65,3 +65,3 @@ t.same(definedPath.parameters, [{

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -93,3 +93,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -121,3 +121,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -155,3 +155,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -185,3 +185,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -219,3 +219,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -264,3 +264,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -288,3 +288,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -319,3 +319,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -377,3 +377,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',

@@ -415,3 +415,3 @@ exposeRoute: true

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
swagger: true,

@@ -464,3 +464,3 @@ routePrefix: '/docs',

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
swagger: true,

@@ -470,3 +470,3 @@ routePrefix: '/docs',

})
fastify.get('/', opt, () => {})
fastify.post('/', opt, () => {})

@@ -478,3 +478,3 @@ await fastify.ready()

const definedPath = api.paths['/'].get
const definedPath = api.paths['/'].post

@@ -519,7 +519,7 @@ t.same(definedPath.parameters[0].schema, {

const fastify = Fastify()
fastify.register(fastifySwagger, {
await fastify.register(fastifySwagger, {
routePrefix: '/docs',
exposeRoute: true
})
fastify.get('/', opt, () => {})
fastify.post('/', opt, () => {})
await fastify.ready()

@@ -530,3 +530,3 @@

const definedPath = api.paths['/'].get
const definedPath = api.paths['/'].post
t.same(definedPath.parameters[0].schema, {

@@ -533,0 +533,0 @@ type: 'object',

@@ -5,3 +5,3 @@ 'use strict'

const Fastify = require('fastify')
const fastifySwagger = require('../index')
const fastifySwagger = require('..')
const Joi = require('joi')

@@ -34,29 +34,27 @@ const Convert = require('joi-to-json')

}
test('transform should fail with a value other than Function', t => {
t.plan(2)
test('transform should fail with a value other than Function', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, invalid)
await fastify.register(fastifySwagger, invalid)
fastify.setValidatorCompiler(({ schema }) => Joi.validate(schema))
fastify.setValidatorCompiler(({ schema }) => params.validate(schema))
fastify.get('/example', opts, () => {})
fastify.ready(err => {
t.error(err)
t.throws(fastify.swagger)
})
await fastify.ready()
t.throws(fastify.swagger)
})
test('transform should work with a Function', t => {
t.plan(2)
test('transform should work with a Function', async (t) => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifySwagger, valid)
await fastify.register(fastifySwagger, valid)
fastify.setValidatorCompiler(({ schema }) => Joi.validate(schema))
fastify.setValidatorCompiler(({ schema }) => params.validate(schema))
fastify.get('/example', opts, () => {})
fastify.ready(err => {
t.error(err)
t.doesNotThrow(fastify.swagger)
})
await fastify.ready()
t.doesNotThrow(fastify.swagger)
})

@@ -21,3 +21,3 @@ 'use strict'

test('formatParamUrl', t => {
test('formatParamUrl', async (t) => {
t.plan(cases.length)

@@ -24,0 +24,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc