Projektstart
This commit is contained in:
45
backend/node_modules/@fastify/swagger/test/decorator.test.js
generated
vendored
Normal file
45
backend/node_modules/@fastify/swagger/test/decorator.test.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const fastifySwagger = require('../index')
|
||||
|
||||
test('fastify.swagger should exist', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger)
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.ok(fastify.swagger)
|
||||
})
|
||||
|
||||
test('fastify.swagger should throw if called before ready', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger)
|
||||
|
||||
t.assert.throws(fastify.swagger.bind(fastify))
|
||||
})
|
||||
|
||||
test('fastify.swagger should throw if called before ready (openapi)', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: {}
|
||||
})
|
||||
|
||||
t.assert.throws(fastify.swagger.bind(fastify))
|
||||
})
|
||||
|
||||
test('decorator can be overridden', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, { decorator: 'customSwaggerDecorator' })
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.ok(fastify.customSwaggerDecorator())
|
||||
})
|
||||
14
backend/node_modules/@fastify/swagger/test/esm/esm.mjs
generated
vendored
Normal file
14
backend/node_modules/@fastify/swagger/test/esm/esm.mjs
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
import t from 'node:test'
|
||||
import Fastify from 'fastify'
|
||||
import swaggerDefault from '../../index.js'
|
||||
|
||||
t.test('esm support', async t => {
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(swaggerDefault)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.swagger, '2.0')
|
||||
})
|
||||
11
backend/node_modules/@fastify/swagger/test/esm/index.test.js
generated
vendored
Normal file
11
backend/node_modules/@fastify/swagger/test/esm/index.test.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
// 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
|
||||
})
|
||||
})
|
||||
510
backend/node_modules/@fastify/swagger/test/mode/static.test.js
generated
vendored
Normal file
510
backend/node_modules/@fastify/swagger/test/mode/static.test.js
generated
vendored
Normal file
@@ -0,0 +1,510 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('node:path')
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const fastifySwagger = require('../../index')
|
||||
const fastifySwaggerDynamic = require('../../lib/mode/dynamic')
|
||||
const Swagger = require('@apidevtools/swagger-parser')
|
||||
const readFileSync = require('node:fs').readFileSync
|
||||
const resolve = require('node:path').resolve
|
||||
const yaml = require('yaml')
|
||||
|
||||
test('specification validation check works', async (t) => {
|
||||
const specificationTests = [
|
||||
{
|
||||
specification: '',
|
||||
error: 'Error: specification is missing in the module options'
|
||||
},
|
||||
{
|
||||
specification: '123',
|
||||
error: 'Error: specification is not an object'
|
||||
},
|
||||
{
|
||||
specification: {},
|
||||
error: 'Error: both specification.path and specification.document are missing, should be path to the file or swagger document spec'
|
||||
},
|
||||
{
|
||||
specification: {
|
||||
path: 123
|
||||
},
|
||||
error: 'Error: specification.path is not a string'
|
||||
},
|
||||
{
|
||||
specification: {
|
||||
path: '/hello/lionel.richie'
|
||||
},
|
||||
error: 'Error: /hello/lionel.richie does not exist'
|
||||
},
|
||||
{
|
||||
specification: {
|
||||
path: './examples/example-static-specification.yaml',
|
||||
postProcessor: 'hello'
|
||||
},
|
||||
error: 'Error: specification.postProcessor should be a function'
|
||||
}
|
||||
]
|
||||
|
||||
t.plan(specificationTests.length * 2)
|
||||
|
||||
for (const specificationTest of specificationTests) {
|
||||
try {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, {
|
||||
mode: 'static',
|
||||
specification: specificationTest.specification
|
||||
})
|
||||
} catch (err) {
|
||||
t.assert.notEqual(err, undefined)
|
||||
t.assert.strictEqual(err.toString(), specificationTest.error)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
test('registering plugin with invalid mode throws an error', async (t) => {
|
||||
const config = {
|
||||
mode: 'error'
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
try {
|
||||
await fastify.register(fastifySwagger, config)
|
||||
} catch (err) {
|
||||
t.assert.strictEqual(err.message, 'unsupported mode, should be one of [\'static\', \'dynamic\']')
|
||||
}
|
||||
})
|
||||
|
||||
test('unsupported file extension in specification.path throws an error', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.js'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
try {
|
||||
await fastify.register(fastifySwagger, config)
|
||||
} catch (err) {
|
||||
t.assert.strictEqual(err.message, 'specification.path extension name is not supported, should be one from [\'.yaml\', \'.json\']')
|
||||
}
|
||||
})
|
||||
|
||||
test('non-string specification.baseDir throws an error ', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.yaml',
|
||||
baseDir: 1
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
try {
|
||||
await fastify.register(fastifySwagger, config)
|
||||
} catch (err) {
|
||||
t.assert.strictEqual(err.message, 'specification.baseDir should be string')
|
||||
}
|
||||
})
|
||||
|
||||
test('valid specification.baseDir is handled properly /1', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json',
|
||||
baseDir: __dirname
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
t.assert.deepStrictEqual(
|
||||
JSON.parse(readFileSync(resolve(__dirname, '..', '..', 'examples', 'example-static-specification.json'), 'utf8')),
|
||||
fastify.swagger({ json: true })
|
||||
)
|
||||
})
|
||||
|
||||
test('valid specification.baseDir is handled properly /2', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json',
|
||||
baseDir: __dirname + '/' // eslint-disable-line n/no-path-concat
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
t.assert.deepStrictEqual(
|
||||
JSON.parse(readFileSync(resolve(__dirname, '..', '..', 'examples', 'example-static-specification.json'), 'utf8')),
|
||||
fastify.swagger({ json: true })
|
||||
)
|
||||
})
|
||||
|
||||
test('valid yaml-specification is converted properly to json', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.yaml'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
t.assert.deepStrictEqual(
|
||||
JSON.parse(readFileSync(resolve(__dirname, '..', '..', 'examples', 'example-static-specification.json'), 'utf8')),
|
||||
fastify.swagger()
|
||||
)
|
||||
})
|
||||
|
||||
test('valid specification yaml is properly handled as yaml', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.yaml',
|
||||
baseDir: __dirname
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
t.assert.strictEqual(
|
||||
readFileSync(resolve(__dirname, '..', '..', 'examples', 'example-static-specification.yaml'), 'utf8'),
|
||||
fastify.swagger({ yaml: true })
|
||||
)
|
||||
})
|
||||
|
||||
test('non-object specification.document throws an error', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
document: 'doc'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = new Fastify()
|
||||
|
||||
await t.assert.rejects(async () => await fastify.register(fastifySwagger, config), new Error('specification.document is not an object'))
|
||||
})
|
||||
|
||||
test('object specification.document', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
document: {
|
||||
type: 'object'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = new Fastify()
|
||||
fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
t.assert.deepStrictEqual(fastify.swagger(), { type: 'object' })
|
||||
})
|
||||
|
||||
test('inserts default opts in fastifySwagger', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger)
|
||||
|
||||
t.assert.ok(true, 'Inserted default option for fastifySwagger.')
|
||||
})
|
||||
|
||||
test('inserts default package name', async (t) => {
|
||||
const config = {
|
||||
mode: 'dynamic',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
|
||||
const originalPathJoin = path.join
|
||||
const testPackageJSON = path.join(__dirname, '../../examples/test-package.json')
|
||||
|
||||
path.join = (...args) => {
|
||||
if (args[3] === 'package.json') {
|
||||
return testPackageJSON
|
||||
}
|
||||
return originalPathJoin(...args)
|
||||
}
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
t.assert.deepStrictEqual(fastify.swagger(), {
|
||||
swagger: '2.0',
|
||||
info: { version: '1.0.0', title: 'test' },
|
||||
definitions: {},
|
||||
paths: {}
|
||||
})
|
||||
})
|
||||
|
||||
test('inserts default package name - openapi', async (t) => {
|
||||
const config = {
|
||||
mode: 'dynamic',
|
||||
openapi: {
|
||||
servers: []
|
||||
},
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
|
||||
const originalPathJoin = path.join
|
||||
const testPackageJSON = path.join(__dirname, '../../examples/test-package.json')
|
||||
|
||||
path.join = (...args) => {
|
||||
if (args[3] === 'package.json') {
|
||||
return testPackageJSON
|
||||
}
|
||||
return originalPathJoin(...args)
|
||||
}
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
t.assert.deepStrictEqual(fastify.swagger(), {
|
||||
openapi: '3.0.3',
|
||||
info: { version: '1.0.0', title: 'test' },
|
||||
components: { schemas: {} },
|
||||
paths: {},
|
||||
servers: []
|
||||
})
|
||||
})
|
||||
|
||||
test('throws an error if cannot parse package\'s JSON', async (t) => {
|
||||
const config = {
|
||||
mode: 'dynamic',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
|
||||
const originalPathJoin = path.join
|
||||
const testPackageJSON = path.join(__dirname, '')
|
||||
|
||||
path.join = (...args) => {
|
||||
if (args[3] === 'package.json') {
|
||||
return testPackageJSON
|
||||
}
|
||||
return originalPathJoin(...args)
|
||||
}
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
t.assert.deepStrictEqual(fastify.swagger(), {
|
||||
swagger: '2.0',
|
||||
info: { version: '1.0.0', title: '' },
|
||||
definitions: {},
|
||||
paths: {}
|
||||
})
|
||||
})
|
||||
|
||||
test('throws an error if cannot parse package\'s JSON - openapi', async (t) => {
|
||||
const config = {
|
||||
mode: 'dynamic',
|
||||
openapi: {
|
||||
servers: []
|
||||
},
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
|
||||
const originalPathJoin = path.join
|
||||
const testPackageJSON = path.join(__dirname, '')
|
||||
|
||||
path.join = (...args) => {
|
||||
if (args[3] === 'package.json') {
|
||||
return testPackageJSON
|
||||
}
|
||||
return originalPathJoin(...args)
|
||||
}
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
t.assert.deepStrictEqual(fastify.swagger(), {
|
||||
openapi: '3.0.3',
|
||||
info: { version: '1.0.0', title: '' },
|
||||
components: { schemas: {} },
|
||||
paths: {},
|
||||
servers: []
|
||||
})
|
||||
})
|
||||
|
||||
test('inserts default opts in fastifySwaggerDynamic (dynamic.js)', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwaggerDynamic)
|
||||
|
||||
t.assert.ok(true, 'Inserted default option for fastifySwagger.')
|
||||
})
|
||||
|
||||
test('should still return valid swagger object when missing package.json', async (t) => {
|
||||
const config = {
|
||||
mode: 'dynamic',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
|
||||
const originalPathJoin = path.join
|
||||
const testPackageJSON = path.join(__dirname, 'missing.json')
|
||||
|
||||
path.join = (...args) => {
|
||||
if (args[3] === 'package.json') {
|
||||
return testPackageJSON
|
||||
}
|
||||
return originalPathJoin(...args)
|
||||
}
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerObject, 'object')
|
||||
|
||||
await Swagger.validate(swaggerObject)
|
||||
t.assert.ok(true, 'Swagger object is still valid.')
|
||||
})
|
||||
|
||||
test('.swagger() returns cache.swaggerObject on second request in static mode', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.json'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(3)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerJson1 = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerJson1, 'object')
|
||||
|
||||
const swaggerJson2 = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerJson2, 'object')
|
||||
t.assert.strictEqual(swaggerJson1, swaggerJson2)
|
||||
})
|
||||
|
||||
test('.swagger({ yaml: true }) returns cache.swaggerString on second request in static mode', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.yaml'
|
||||
}
|
||||
}
|
||||
|
||||
t.plan(3)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerYaml1 = fastify.swagger({ yaml: true })
|
||||
t.assert.strictEqual(typeof swaggerYaml1, 'string')
|
||||
|
||||
const swaggerYaml2 = fastify.swagger({ yaml: true })
|
||||
t.assert.strictEqual(typeof swaggerYaml2, 'string')
|
||||
t.assert.strictEqual(swaggerYaml1, swaggerYaml2)
|
||||
})
|
||||
|
||||
test('postProcessor works, swagger route returns updated yaml', async (t) => {
|
||||
const config = {
|
||||
mode: 'static',
|
||||
specification: {
|
||||
path: './examples/example-static-specification.yaml',
|
||||
postProcessor: function (swaggerObject) {
|
||||
swaggerObject.servers[0].url = 'http://localhost:4000/'
|
||||
return swaggerObject
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const expectedYaml = `openapi: 3.0.0
|
||||
info:
|
||||
description: Test swagger specification
|
||||
version: 1.0.0
|
||||
title: Test swagger specification
|
||||
contact:
|
||||
email: super.developer@gmail.com
|
||||
servers:
|
||||
- url: http://localhost:4000/
|
||||
description: Localhost (uses test data)
|
||||
paths:
|
||||
/status:
|
||||
get:
|
||||
description: Status route, so we can check if server is alive
|
||||
tags:
|
||||
- Status
|
||||
responses:
|
||||
"200":
|
||||
description: Server is alive
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
health:
|
||||
type: boolean
|
||||
date:
|
||||
type: string
|
||||
example:
|
||||
health: true
|
||||
date: 2018-02-19T15:36:46.758Z
|
||||
`
|
||||
|
||||
t.plan(3)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, config)
|
||||
await fastify.ready()
|
||||
|
||||
// check that yaml is there
|
||||
const res = fastify.swagger({ yaml: true })
|
||||
|
||||
t.assert.strictEqual(typeof res, 'string')
|
||||
yaml.parse(res)
|
||||
t.assert.strictEqual(res, expectedYaml)
|
||||
t.assert.ok(true, 'valid swagger yaml')
|
||||
})
|
||||
1732
backend/node_modules/@fastify/swagger/test/spec/openapi/option.test.js
generated
vendored
Normal file
1732
backend/node_modules/@fastify/swagger/test/spec/openapi/option.test.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
564
backend/node_modules/@fastify/swagger/test/spec/openapi/refs.test.js
generated
vendored
Normal file
564
backend/node_modules/@fastify/swagger/test/spec/openapi/refs.test.js
generated
vendored
Normal file
@@ -0,0 +1,564 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const Swagger = require('@apidevtools/swagger-parser')
|
||||
const fastifySwagger = require('../../../index')
|
||||
|
||||
const openapiOption = {
|
||||
openapi: {},
|
||||
refResolver: {
|
||||
buildLocalReference: (json, _baseUri, _fragment, i) => {
|
||||
return json.$id || `def-${i}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test('support $ref schema', async (t) => {
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'Order', type: 'object', properties: { id: { type: 'integer', examples: [25] } } })
|
||||
instance.post('/', { schema: { body: { $ref: 'Order#' }, response: { 200: { $ref: 'Order#' } } } }, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
t.assert.deepStrictEqual(Object.keys(openapiObject.components.schemas), ['Order'])
|
||||
t.assert.strictEqual(openapiObject.components.schemas.Order.properties.id.example, 25)
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('support $ref relative pointers in params', async (t) => {
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({
|
||||
$id: 'Order',
|
||||
type: 'object',
|
||||
properties: {
|
||||
OrderId: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.get('/:id', { schema: { params: { $ref: 'Order#/properties/OrderId' }, response: { 200: { $ref: 'Order#' } } } }, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
t.assert.deepStrictEqual(Object.keys(openapiObject.components.schemas), ['Order'])
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('support nested $ref schema : simple test', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'OrderItem', type: 'object', properties: { id: { type: 'integer' } }, examples: [{ id: 1 }] })
|
||||
instance.addSchema({ $id: 'ProductItem', type: 'object', properties: { id: { type: 'integer' } } })
|
||||
instance.addSchema({ $id: 'Order', type: 'object', properties: { products: { type: 'array', items: { $ref: 'OrderItem' } } } })
|
||||
instance.post('/', { schema: { body: { $ref: 'Order' }, response: { 200: { $ref: 'Order' } } } }, () => {})
|
||||
instance.post('/other', { schema: { body: { $ref: 'ProductItem' } } }, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const schemas = openapiObject.components.schemas
|
||||
t.assert.deepStrictEqual(Object.keys(schemas), ['OrderItem', 'ProductItem', 'Order'])
|
||||
|
||||
// ref must be prefixed by '#/components/schemas/'
|
||||
t.assert.strictEqual(schemas.Order.properties.products.items.$ref, '#/components/schemas/OrderItem')
|
||||
t.assert.deepStrictEqual(schemas.OrderItem.example, { id: 1 })
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('support nested $ref schema : complex case', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
|
||||
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string', examples: ['ABC'] } } })
|
||||
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
|
||||
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
|
||||
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
|
||||
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const schemas = openapiObject.components.schemas
|
||||
t.assert.deepStrictEqual(Object.keys(schemas), ['schemaA', 'schemaB', 'schemaC', 'schemaD'])
|
||||
|
||||
// ref must be prefixed by '#/components/schemas/'
|
||||
t.assert.strictEqual(schemas.schemaC.properties.a.items.$ref, '#/components/schemas/schemaA')
|
||||
t.assert.strictEqual(schemas.schemaD.properties.b.$ref, '#/components/schemas/schemaB')
|
||||
t.assert.strictEqual(schemas.schemaD.properties.c.$ref, '#/components/schemas/schemaC')
|
||||
t.assert.strictEqual(schemas.schemaB.properties.id.example, 'ABC')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('support $ref in response schema', async (t) => {
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
fastify.register(function (instance, _, done) {
|
||||
instance.addSchema({ $id: 'order', type: 'string', enum: ['foo'] })
|
||||
instance.post('/', { schema: { response: { 200: { type: 'object', properties: { order: { $ref: 'order' } } } } } }, () => {})
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('support $ref for enums in other schemas', async (t) => {
|
||||
const fastify = Fastify()
|
||||
|
||||
const enumSchema = { $id: 'order', anyOf: [{ type: 'string', const: 'foo' }, { type: 'string', const: 'bar' }] }
|
||||
const enumRef = { $ref: 'order' }
|
||||
const objectWithEnumSchema = { $id: 'object', type: 'object', properties: { type: enumRef }, required: ['type'] }
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
await fastify.register(async (instance) => {
|
||||
instance.addSchema(enumSchema)
|
||||
instance.addSchema(objectWithEnumSchema)
|
||||
instance.post('/', { schema: { body: { type: 'object', properties: { order: { $ref: 'order' } } } } }, async () => ({ result: 'OK' }))
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const responseBeforeSwagger = await fastify.inject({ method: 'POST', url: '/', payload: { order: 'foo' } })
|
||||
|
||||
t.assert.strictEqual(responseBeforeSwagger.statusCode, 200)
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
|
||||
const responseAfterSwagger = await fastify.inject({ method: 'POST', url: '/', payload: { order: 'foo' } })
|
||||
|
||||
t.assert.strictEqual(responseAfterSwagger.statusCode, 200)
|
||||
})
|
||||
|
||||
test('support nested $ref schema : complex case without modifying buildLocalReference', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: {} })
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
|
||||
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
|
||||
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
|
||||
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
|
||||
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
|
||||
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const schemas = openapiObject.components.schemas
|
||||
t.assert.deepStrictEqual(Object.keys(schemas), ['def-0', 'def-1', 'def-2', 'def-3'])
|
||||
|
||||
// ref must be prefixed by '#/components/schemas/'
|
||||
t.assert.strictEqual(schemas['def-2'].properties.a.items.$ref, '#/components/schemas/def-0')
|
||||
t.assert.strictEqual(schemas['def-3'].properties.b.$ref, '#/components/schemas/def-1')
|
||||
t.assert.strictEqual(schemas['def-3'].properties.c.$ref, '#/components/schemas/def-2')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('support nested $ref with patternProperties', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: {} })
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
|
||||
instance.addSchema({ $id: 'schemaB', type: 'object', patternProperties: { '^[A-z]{1,10}$': { $ref: 'schemaA#' } } })
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const schemas = openapiObject.components.schemas
|
||||
t.assert.deepStrictEqual(Object.keys(schemas), ['def-0', 'def-1'])
|
||||
|
||||
// ref must be prefixed by '#/components/schemas/'
|
||||
t.assert.strictEqual(schemas['def-1'].additionalProperties.$ref, '#/components/schemas/def-0')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('support $ref schema in allOf in querystring', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: {} })
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { field1: { type: 'integer' } } })
|
||||
instance.get('/url1', { schema: { query: { type: 'object', allOf: [{ $ref: 'schemaA#' }, { type: 'object', properties: { field3: { type: 'boolean' } } }] }, response: { 200: { type: 'object' } } } }, async () => ({ result: 'OK' }))
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const schemas = openapiObject.components.schemas
|
||||
t.assert.deepStrictEqual(Object.keys(schemas), ['def-0'])
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
|
||||
const responseAfterSwagger = await fastify.inject({ method: 'GET', url: '/url1', query: { field1: 10, field3: false } })
|
||||
|
||||
t.assert.strictEqual(responseAfterSwagger.statusCode, 200)
|
||||
})
|
||||
|
||||
test('support $ref schema in allOf in headers', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: {} })
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'headerA', type: 'object', properties: { 'x-header-1': { type: 'string' } } })
|
||||
instance.get('/url1', { schema: { headers: { allOf: [{ $ref: 'headerA#' }, { type: 'object', properties: { 'x-header-2': { type: 'string' } } }] }, response: { 200: { type: 'object' } } } }, async () => ({ result: 'OK' }))
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const schemas = openapiObject.components.schemas
|
||||
t.assert.deepStrictEqual(Object.keys(schemas), ['def-0'])
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
|
||||
const responseAfterSwagger = await fastify.inject({ method: 'GET', url: '/url1', headers: { 'x-header-1': 'test', 'x-header-2': 'test' } })
|
||||
|
||||
t.assert.strictEqual(responseAfterSwagger.statusCode, 200)
|
||||
})
|
||||
|
||||
test('uses examples if has property required in body', async (t) => {
|
||||
t.plan(3)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/', {
|
||||
schema: {
|
||||
query: {
|
||||
type: 'object',
|
||||
oneOf: [
|
||||
{
|
||||
properties: {
|
||||
bar: { type: 'number' }
|
||||
}
|
||||
},
|
||||
{
|
||||
properties: {
|
||||
foo: { type: 'string' }
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
result: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => ({ result: 'OK' }))
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
const schema = openapiObject.paths['/'].get
|
||||
|
||||
t.assert.ok(schema)
|
||||
t.assert.ok(schema.parameters)
|
||||
t.assert.deepStrictEqual(schema.parameters[0].in, 'query')
|
||||
})
|
||||
|
||||
test('renders $ref schema with enum in headers', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: {} })
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'headerA', type: 'object', properties: { 'x-enum-header': { type: 'string', enum: ['OK', 'NOT_OK'] } } })
|
||||
instance.get('/url1', { schema: { headers: { $ref: 'headerA#' }, response: { 200: { type: 'object' } } } }, async () => ({ result: 'OK' }))
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
|
||||
// the OpenAPI spec should show the enum
|
||||
t.assert.deepStrictEqual(openapiObject.paths['/url1'].get.parameters[0].schema, { type: 'string', enum: ['OK', 'NOT_OK'] })
|
||||
})
|
||||
|
||||
test('renders $ref schema with additional keywords', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: {} })
|
||||
await fastify.register(require('@fastify/cookie'))
|
||||
|
||||
const cookie = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
a: { type: 'string' },
|
||||
b: { type: 'string' },
|
||||
c: { type: 'string' }
|
||||
},
|
||||
minProperties: 2
|
||||
}
|
||||
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({
|
||||
$id: 'headerA',
|
||||
type: 'object',
|
||||
properties: {
|
||||
cookie
|
||||
}
|
||||
})
|
||||
|
||||
instance.get('/url1', {
|
||||
preValidation: async (request) => {
|
||||
request.headers.cookie = request.cookies
|
||||
},
|
||||
schema: {
|
||||
headers: {
|
||||
$ref: 'headerA#'
|
||||
}
|
||||
}
|
||||
}, async (req) => (req.headers))
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
const openapiObject = fastify.swagger()
|
||||
await Swagger.validate(openapiObject)
|
||||
|
||||
t.assert.deepStrictEqual(openapiObject.paths['/url1'].get.parameters[0].schema, cookie)
|
||||
|
||||
let res = await fastify.inject({ method: 'GET', url: 'url1', cookies: { a: 'hi', b: 'asd' } })
|
||||
|
||||
t.assert.deepStrictEqual(res.statusCode, 200)
|
||||
|
||||
res = await fastify.inject({ method: 'GET', url: 'url1', cookies: { a: 'hi' } })
|
||||
|
||||
t.assert.deepStrictEqual(res.statusCode, 400)
|
||||
t.assert.deepStrictEqual(openapiObject.paths['/url1'].get.parameters[0].schema, cookie)
|
||||
})
|
||||
|
||||
test('support $ref in callbacks', async (t) => {
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'Subscription', type: 'object', properties: { callbackUrl: { type: 'string', examples: ['https://example.com'] } } })
|
||||
instance.addSchema({ $id: 'Event', type: 'object', properties: { message: { type: 'string', examples: ['Some event happened'] } } })
|
||||
instance.post('/subscribe', {
|
||||
schema: {
|
||||
body: {
|
||||
$ref: 'Subscription#'
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
$ref: 'Subscription#'
|
||||
}
|
||||
},
|
||||
callbacks: {
|
||||
myEvent: {
|
||||
'{$request.body#/callbackUrl}': {
|
||||
post: {
|
||||
requestBody: {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: { $ref: 'Event#' }
|
||||
}
|
||||
}
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: 'Success'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
t.assert.deepStrictEqual(Object.keys(openapiObject.components.schemas), ['Subscription', 'Event'])
|
||||
t.assert.strictEqual(openapiObject.components.schemas.Subscription.properties.callbackUrl.example, 'https://example.com')
|
||||
t.assert.strictEqual(openapiObject.components.schemas.Event.properties.message.example, 'Some event happened')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
|
||||
test('should return only ref if defs and ref is defined', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: { openapi: '3.1.0' } })
|
||||
|
||||
fastify.addSchema({
|
||||
$id: 'sharedSchema',
|
||||
humanModule: {
|
||||
$defs: {
|
||||
AddressSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
street: {
|
||||
type: 'string',
|
||||
},
|
||||
streetNumber: {
|
||||
type: 'number',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'street',
|
||||
'streetNumber',
|
||||
],
|
||||
$id: 'AddressSchema',
|
||||
},
|
||||
PersonSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: {
|
||||
type: 'string',
|
||||
},
|
||||
homeAddress: {
|
||||
$ref: 'AddressSchema',
|
||||
},
|
||||
workAddress: {
|
||||
$ref: 'AddressSchema',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'name',
|
||||
'homeAddress',
|
||||
'workAddress',
|
||||
],
|
||||
$id: 'PersonSchema',
|
||||
},
|
||||
PostRequestSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
person: {
|
||||
$ref: 'PersonSchema',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'person',
|
||||
],
|
||||
$id: 'PostRequestSchema',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
fastify.get('/person', {
|
||||
schema: {
|
||||
response: {
|
||||
200:
|
||||
{
|
||||
$defs: {
|
||||
AddressSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
street: {
|
||||
type: 'string',
|
||||
},
|
||||
streetNumber: {
|
||||
type: 'number',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'street',
|
||||
'streetNumber',
|
||||
],
|
||||
$id: 'AddressSchema',
|
||||
},
|
||||
PersonSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: {
|
||||
type: 'string',
|
||||
},
|
||||
homeAddress: {
|
||||
$ref: 'AddressSchema',
|
||||
},
|
||||
workAddress: {
|
||||
$ref: 'AddressSchema',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'name',
|
||||
'homeAddress',
|
||||
'workAddress',
|
||||
],
|
||||
$id: 'PersonSchema',
|
||||
},
|
||||
PostRequestSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
person: {
|
||||
$ref: 'PersonSchema',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'person',
|
||||
],
|
||||
$id: 'PostRequestSchema',
|
||||
},
|
||||
},
|
||||
$ref: 'PersonSchema',
|
||||
}
|
||||
}
|
||||
},
|
||||
}, async () => ({ result: 'OK' }))
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const expectedPathContent = { 'application/json': { schema: { $ref: '#/components/schemas/def-2' } } }
|
||||
t.assert.deepStrictEqual(openapiObject.paths['/person'].get.responses[200].content, expectedPathContent)
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
})
|
||||
899
backend/node_modules/@fastify/swagger/test/spec/openapi/route.test.js
generated
vendored
Normal file
899
backend/node_modules/@fastify/swagger/test/spec/openapi/route.test.js
generated
vendored
Normal file
@@ -0,0 +1,899 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const Swagger = require('@apidevtools/swagger-parser')
|
||||
const yaml = require('yaml')
|
||||
const fastifySwagger = require('../../../index')
|
||||
const {
|
||||
openapiOption,
|
||||
openapiRelativeOptions,
|
||||
schemaBody,
|
||||
schemaConsumes,
|
||||
schemaCookies,
|
||||
schemaExtension,
|
||||
schemaHeaders,
|
||||
schemaHeadersParams,
|
||||
schemaParams,
|
||||
schemaProduces,
|
||||
schemaQuerystring,
|
||||
schemaSecurity,
|
||||
schemaOperationId
|
||||
} = require('../../../examples/options')
|
||||
|
||||
test('openapi should return a valid swagger object', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/', () => {})
|
||||
fastify.post('/', () => {})
|
||||
fastify.get('/example', schemaQuerystring, () => {})
|
||||
fastify.post('/example', schemaBody, () => {})
|
||||
fastify.get('/parameters/:id', schemaParams, () => {})
|
||||
fastify.get('/headers', schemaHeaders, () => {})
|
||||
fastify.get('/headers/:id', schemaHeadersParams, () => {})
|
||||
fastify.get('/security', schemaSecurity, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
})
|
||||
|
||||
test('openapi should return a valid swagger yaml', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/', () => {})
|
||||
fastify.post('/', () => {})
|
||||
fastify.get('/example', schemaQuerystring, () => {})
|
||||
fastify.post('/example', schemaBody, () => {})
|
||||
fastify.get('/parameters/:id', schemaParams, () => {})
|
||||
fastify.get('/headers', schemaHeaders, () => {})
|
||||
fastify.get('/headers/:id', schemaHeadersParams, () => {})
|
||||
fastify.get('/security', schemaSecurity, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerYaml = fastify.swagger({ yaml: true })
|
||||
t.assert.strictEqual(typeof swaggerYaml, 'string')
|
||||
yaml.parse(swaggerYaml)
|
||||
t.assert.ok(true, 'valid swagger yaml')
|
||||
})
|
||||
|
||||
test('route options - deprecated', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
deprecated: true,
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.ok(openapiObject.paths['/'])
|
||||
})
|
||||
|
||||
test('route options - meta', async (t) => {
|
||||
t.plan(7)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
operationId: 'doSomething',
|
||||
summary: 'Route summary',
|
||||
tags: ['tag'],
|
||||
description: 'Route description',
|
||||
servers: [
|
||||
{
|
||||
url: 'https://localhost'
|
||||
}
|
||||
],
|
||||
externalDocs: {
|
||||
description: 'Find more info here',
|
||||
url: 'https://swagger.io'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.get('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.strictEqual(opts.schema.operationId, definedPath.operationId)
|
||||
t.assert.strictEqual(opts.schema.summary, definedPath.summary)
|
||||
t.assert.deepEqual(opts.schema.tags, definedPath.tags)
|
||||
t.assert.strictEqual(opts.schema.description, definedPath.description)
|
||||
t.assert.strictEqual(opts.schema.servers, definedPath.servers)
|
||||
t.assert.strictEqual(opts.schema.externalDocs, definedPath.externalDocs)
|
||||
})
|
||||
|
||||
test('route options - produces', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/', schemaProduces, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepEqual(definedPath.responses[200].content, {
|
||||
'*/*': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
description: 'hello',
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
required: ['hello']
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
test('route options - cookies', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/', schemaCookies, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepEqual(definedPath.parameters, [
|
||||
{
|
||||
required: false,
|
||||
in: 'cookie',
|
||||
name: 'bar',
|
||||
schema: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
test('route options - extension', async (t) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { openapi: { 'x-ternal': true } })
|
||||
fastify.get('/', schemaExtension, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
t.assert.ok(api['x-ternal'])
|
||||
t.assert.deepEqual(api['x-ternal'], true)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepEqual(definedPath['x-tension'], true)
|
||||
})
|
||||
|
||||
test('parses form parameters when all api consumes application/x-www-form-urlencoded', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
fastify.post('/', schemaConsumes, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
const definedPath = api.paths['/'].post
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepEqual(definedPath.requestBody.content, {
|
||||
'application/x-www-form-urlencoded': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
description: 'hello',
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
required: ['hello']
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
test('route options - method', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.route({
|
||||
method: ['GET', 'POST'],
|
||||
url: '/',
|
||||
handler: function (request, reply) {
|
||||
reply.send({ hello: 'world' })
|
||||
}
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
})
|
||||
|
||||
test('cookie, query, path description', async (t) => {
|
||||
t.plan(6)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
const schemaCookies = {
|
||||
schema: {
|
||||
cookies: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
bar: { type: 'string', description: 'Bar' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const schemaQuerystring = {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string', description: 'Hello' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// test without description as other test case for params already have description
|
||||
const schemaParams = {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.get('/', schemaCookies, () => {})
|
||||
fastify.get('/example', schemaQuerystring, () => {})
|
||||
fastify.get('/parameters/:id', schemaParams, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
const cookiesPath = api.paths['/'].get
|
||||
t.assert.ok(cookiesPath)
|
||||
t.assert.deepEqual(cookiesPath.parameters, [
|
||||
{
|
||||
required: false,
|
||||
in: 'cookie',
|
||||
name: 'bar',
|
||||
description: 'Bar',
|
||||
schema: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
])
|
||||
const querystringPath = api.paths['/example'].get
|
||||
t.assert.ok(querystringPath)
|
||||
t.assert.deepEqual(querystringPath.parameters, [
|
||||
{
|
||||
required: false,
|
||||
in: 'query',
|
||||
name: 'hello',
|
||||
description: 'Hello',
|
||||
schema: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
])
|
||||
const paramPath = api.paths['/parameters/{id}'].get
|
||||
t.assert.ok(paramPath)
|
||||
t.assert.deepEqual(paramPath.parameters, [
|
||||
{
|
||||
required: true,
|
||||
in: 'path',
|
||||
name: 'id',
|
||||
schema: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
test('cookie and query with serialization type', async (t) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify({
|
||||
ajv: {
|
||||
plugins: [
|
||||
function (ajv) {
|
||||
ajv.addKeyword({
|
||||
keyword: 'x-consume'
|
||||
})
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
const schemaCookies = {
|
||||
schema: {
|
||||
cookies: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
bar: {
|
||||
type: 'object',
|
||||
'x-consume': 'application/json',
|
||||
required: ['foo'],
|
||||
properties: {
|
||||
foo: { type: 'string' },
|
||||
bar: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const schemaQuerystring = {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'object',
|
||||
'x-consume': 'application/json',
|
||||
required: ['bar'],
|
||||
properties: {
|
||||
bar: { type: 'string' },
|
||||
baz: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.get('/', schemaCookies, () => {})
|
||||
fastify.get('/example', schemaQuerystring, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
|
||||
const cookiesPath = api.paths['/'].get
|
||||
t.assert.ok(cookiesPath)
|
||||
t.assert.deepEqual(cookiesPath.parameters, [
|
||||
{
|
||||
[Symbol.for('@fastify/swagger.rawRequired')]: ['foo'],
|
||||
required: false,
|
||||
in: 'cookie',
|
||||
name: 'bar',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['foo'],
|
||||
properties: {
|
||||
foo: { type: 'string' },
|
||||
bar: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
const querystringPath = api.paths['/example'].get
|
||||
t.assert.ok(querystringPath)
|
||||
t.assert.deepEqual(querystringPath.parameters, [
|
||||
{
|
||||
required: false,
|
||||
in: 'query',
|
||||
name: 'hello',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['bar'],
|
||||
properties: {
|
||||
bar: { type: 'string' },
|
||||
baz: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
test('openapi should pass through operationId', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/hello', schemaOperationId, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
})
|
||||
|
||||
test('openapi should pass through Links', async (t) => {
|
||||
t.plan(3)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/user/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'the user identifier, as userId'
|
||||
}
|
||||
},
|
||||
required: ['id']
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
uuid: {
|
||||
type: 'string',
|
||||
format: 'uuid'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
links: {
|
||||
200: {
|
||||
address: {
|
||||
operationId: 'getUserAddress',
|
||||
parameters: {
|
||||
id: '$request.path.id'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/user/:id/address', {
|
||||
schema: {
|
||||
operationId: 'getUserAddress',
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'the user identifier, as userId'
|
||||
}
|
||||
},
|
||||
required: ['id']
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.deepEqual(api.paths['/user/{id}'].get.responses['200'].links, {
|
||||
address: {
|
||||
operationId: 'getUserAddress',
|
||||
parameters: {
|
||||
id: '$request.path.id'
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
test('links without status code', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/user/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'the user identifier, as userId'
|
||||
}
|
||||
},
|
||||
required: ['id']
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
uuid: {
|
||||
type: 'string',
|
||||
format: 'uuid'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
links: {
|
||||
201: {
|
||||
address: {
|
||||
operationId: 'getUserAddress',
|
||||
parameters: {
|
||||
id: '$request.path.id'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/user/:id/address', {
|
||||
schema: {
|
||||
operationId: 'getUserAddress',
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'the user identifier, as userId'
|
||||
}
|
||||
},
|
||||
required: ['id']
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
t.assert.throws(() => fastify.swagger(), new Error('missing status code 201 in route /user/:id'))
|
||||
})
|
||||
|
||||
test('security headers ignored when declared in security and securityScheme', async (t) => {
|
||||
t.plan(6)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiOption)
|
||||
|
||||
fastify.get('/address1/:id', {
|
||||
schema: {
|
||||
headers: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
apiKey: {
|
||||
type: 'string',
|
||||
description: 'api token'
|
||||
},
|
||||
bearerAuth: {
|
||||
type: 'string',
|
||||
description: 'authorization bearer'
|
||||
},
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/address2/:id', {
|
||||
schema: {
|
||||
headers: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
authKey: {
|
||||
type: 'string',
|
||||
description: 'auth token'
|
||||
},
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.strictEqual(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')), undefined)
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
|
||||
})
|
||||
|
||||
test('security querystrings ignored when declared in security and securityScheme', async (t) => {
|
||||
t.plan(6)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: {
|
||||
components: {
|
||||
securitySchemes: {
|
||||
apiKey: {
|
||||
type: 'apiKey',
|
||||
name: 'apiKey',
|
||||
in: 'query'
|
||||
}
|
||||
}
|
||||
},
|
||||
security: [{
|
||||
apiKey: []
|
||||
}]
|
||||
}
|
||||
})
|
||||
|
||||
fastify.get('/address1/:id', {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
apiKey: {
|
||||
type: 'string',
|
||||
description: 'api token'
|
||||
},
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/address2/:id', {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
authKey: {
|
||||
type: 'string',
|
||||
description: 'auth token'
|
||||
},
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.strictEqual(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')), undefined)
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
|
||||
})
|
||||
|
||||
test('security cookies ignored when declared in security and securityScheme', async (t) => {
|
||||
t.plan(6)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: {
|
||||
components: {
|
||||
securitySchemes: {
|
||||
apiKey: {
|
||||
type: 'apiKey',
|
||||
name: 'apiKey',
|
||||
in: 'cookie'
|
||||
}
|
||||
}
|
||||
},
|
||||
security: [{
|
||||
apiKey: []
|
||||
}]
|
||||
}
|
||||
})
|
||||
|
||||
fastify.get('/address1/:id', {
|
||||
schema: {
|
||||
cookies: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
apiKey: {
|
||||
type: 'string',
|
||||
description: 'api token'
|
||||
},
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/address2/:id', {
|
||||
schema: {
|
||||
cookies: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
authKey: {
|
||||
type: 'string',
|
||||
description: 'auth token'
|
||||
},
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof openapiObject, 'object')
|
||||
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.strictEqual(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')), undefined)
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
|
||||
})
|
||||
|
||||
test('path params on relative url', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiRelativeOptions)
|
||||
|
||||
const schemaParams = {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fastify.get('/parameters/:id', schemaParams, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const openapiObject = fastify.swagger()
|
||||
const api = await Swagger.validate(openapiObject)
|
||||
const paramPath = api.paths['/parameters/{id}'].get
|
||||
t.assert.ok(paramPath)
|
||||
t.assert.deepEqual(paramPath.parameters, [
|
||||
{
|
||||
required: true,
|
||||
in: 'path',
|
||||
name: 'id',
|
||||
schema: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
test('verify generated path param definition with route prefixing', async (t) => {
|
||||
const opts = {
|
||||
schema: {}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, openapiRelativeOptions)
|
||||
await fastify.register(function (app, _, done) {
|
||||
app.get('/:userId', opts, () => {})
|
||||
|
||||
done()
|
||||
}, { prefix: '/v1' })
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/v1/{userId}'].get
|
||||
|
||||
t.assert.deepEqual(definedPath.parameters, [{
|
||||
schema: {
|
||||
type: 'string'
|
||||
},
|
||||
in: 'path',
|
||||
name: 'userId',
|
||||
required: true
|
||||
}])
|
||||
})
|
||||
1936
backend/node_modules/@fastify/swagger/test/spec/openapi/schema.test.js
generated
vendored
Normal file
1936
backend/node_modules/@fastify/swagger/test/spec/openapi/schema.test.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
470
backend/node_modules/@fastify/swagger/test/spec/swagger/option.test.js
generated
vendored
Normal file
470
backend/node_modules/@fastify/swagger/test/spec/swagger/option.test.js
generated
vendored
Normal file
@@ -0,0 +1,470 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const Swagger = require('@apidevtools/swagger-parser')
|
||||
const yaml = require('yaml')
|
||||
const fastifySwagger = require('../../../index')
|
||||
const { readPackageJson } = require('../../../lib/util/read-package-json')
|
||||
const { swaggerOption } = require('../../../examples/options')
|
||||
|
||||
test('swagger should have default version', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.swagger, '2.0')
|
||||
})
|
||||
|
||||
test('swagger should have default info properties', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const pkg = readPackageJson()
|
||||
|
||||
t.assert.strictEqual(swaggerObject.info.title, pkg.name)
|
||||
t.assert.strictEqual(swaggerObject.info.version, pkg.version)
|
||||
})
|
||||
|
||||
test('swagger basic properties', async (t) => {
|
||||
t.plan(5)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.info, swaggerOption.swagger.info)
|
||||
t.assert.strictEqual(swaggerObject.host, swaggerOption.swagger.host)
|
||||
t.assert.strictEqual(swaggerObject.schemes, swaggerOption.swagger.schemes)
|
||||
t.assert.ok(swaggerObject.paths)
|
||||
t.assert.ok(swaggerObject.paths['/'])
|
||||
})
|
||||
|
||||
test('swagger definitions', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
swaggerOption.swagger.definitions = {
|
||||
ExampleModel: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'integer',
|
||||
description: 'Some id'
|
||||
},
|
||||
name: {
|
||||
type: 'string',
|
||||
description: 'Name of smthng'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(swaggerObject.definitions)), swaggerOption.swagger.definitions)
|
||||
delete swaggerOption.swagger.definitions // remove what we just added
|
||||
})
|
||||
|
||||
test('swagger paths', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
swaggerOption.swagger.paths = {
|
||||
'/status': {
|
||||
get: {
|
||||
description: 'Status route, so we can check if server is alive',
|
||||
tags: [
|
||||
'Status'
|
||||
],
|
||||
responses: {
|
||||
200: {
|
||||
description: 'Server is alive',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
health: {
|
||||
type: 'boolean'
|
||||
},
|
||||
date: {
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
example: {
|
||||
health: true,
|
||||
date: '2018-02-19T15:36:46.758Z'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.deepStrictEqual(swaggerObject.paths, swaggerOption.swagger.paths)
|
||||
delete swaggerOption.swagger.paths // remove what we just added
|
||||
})
|
||||
|
||||
test('swagger tags', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.tags, swaggerOption.swagger.tags)
|
||||
})
|
||||
|
||||
test('swagger externalDocs', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.externalDocs, swaggerOption.swagger.externalDocs)
|
||||
})
|
||||
|
||||
test('basePath support', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: Object.assign({}, swaggerOption.swagger, {
|
||||
basePath: '/prefix'
|
||||
})
|
||||
})
|
||||
|
||||
fastify.get('/prefix/endpoint', {}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths['/prefix/endpoint'], undefined)
|
||||
t.assert.ok(swaggerObject.paths['/endpoint'])
|
||||
})
|
||||
|
||||
test('basePath support with prefix', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
prefix: '/prefix',
|
||||
swagger: Object.assign({}, swaggerOption.swagger, {
|
||||
basePath: '/prefix'
|
||||
})
|
||||
})
|
||||
|
||||
fastify.get('/endpoint', {}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths['/prefix/endpoint'], undefined)
|
||||
t.assert.ok(swaggerObject.paths['/endpoint'])
|
||||
})
|
||||
|
||||
test('basePath ensure leading slash', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: Object.assign({}, swaggerOption.swagger, {
|
||||
basePath: '/'
|
||||
})
|
||||
})
|
||||
|
||||
fastify.get('/endpoint', {}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths.endpoint, undefined)
|
||||
t.assert.ok(swaggerObject.paths['/endpoint'])
|
||||
})
|
||||
|
||||
test('basePath with prefix ensure leading slash', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
prefix: '/',
|
||||
swagger: Object.assign({}, swaggerOption.swagger, {
|
||||
basePath: '/'
|
||||
})
|
||||
})
|
||||
|
||||
fastify.get('/endpoint', {}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths.endpoint, undefined)
|
||||
t.assert.ok(swaggerObject.paths['/endpoint'])
|
||||
})
|
||||
|
||||
test('basePath maintained when stripBasePath is set to false', async (t) => {
|
||||
t.plan(3)
|
||||
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
stripBasePath: false,
|
||||
swagger: Object.assign({}, swaggerOption.swagger, {
|
||||
basePath: '/foo'
|
||||
})
|
||||
})
|
||||
|
||||
fastify.get('/foo/endpoint', {}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths.endpoint, undefined)
|
||||
t.assert.strictEqual(swaggerObject.paths['/endpoint'], undefined)
|
||||
t.assert.ok(swaggerObject.paths['/foo/endpoint'])
|
||||
})
|
||||
|
||||
// hide testing
|
||||
|
||||
test('hide support - property', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
hide: true,
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths['/'], undefined)
|
||||
})
|
||||
|
||||
test('hide support when property set in transform() - property', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
...swaggerOption,
|
||||
transform: ({ schema, url }) => {
|
||||
return { schema: { ...schema, hide: true }, url }
|
||||
}
|
||||
})
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths['/'], undefined)
|
||||
})
|
||||
|
||||
test('hide support - tags Default', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
tags: ['X-HIDDEN'],
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths['/'], undefined)
|
||||
})
|
||||
|
||||
test('hide support - tags Custom', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, { ...swaggerOption, hiddenTag: 'NOP' })
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
tags: ['NOP'],
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths['/'], undefined)
|
||||
})
|
||||
|
||||
test('hide support - hidden untagged', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, { ...swaggerOption, hideUntagged: true })
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.paths['/'], undefined)
|
||||
})
|
||||
|
||||
test('cache - json', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
fastify.swagger()
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerObject, 'object')
|
||||
|
||||
await Swagger.validate(swaggerObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
})
|
||||
|
||||
test('cache - yaml', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
fastify.swagger({ yaml: true })
|
||||
const swaggerYaml = fastify.swagger({ yaml: true })
|
||||
t.assert.strictEqual(typeof swaggerYaml, 'string')
|
||||
yaml.parse(swaggerYaml)
|
||||
t.assert.ok(true, 'valid swagger yaml')
|
||||
})
|
||||
|
||||
module.exports = { swaggerOption }
|
||||
164
backend/node_modules/@fastify/swagger/test/spec/swagger/refs.test.js
generated
vendored
Normal file
164
backend/node_modules/@fastify/swagger/test/spec/swagger/refs.test.js
generated
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const Swagger = require('@apidevtools/swagger-parser')
|
||||
const fastifySwagger = require('../../../index')
|
||||
const { FST_ERR_SCH_ALREADY_PRESENT } = require('fastify/lib/errors')
|
||||
|
||||
test('support $ref schema', async t => {
|
||||
t.plan(1)
|
||||
|
||||
const fastify = Fastify()
|
||||
fastify.addSchema({
|
||||
$id: 'example',
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' }
|
||||
}
|
||||
})
|
||||
|
||||
await fastify.register(fastifySwagger)
|
||||
|
||||
fastify.register((instance, _opts, next) => {
|
||||
instance.addSchema({
|
||||
$id: 'subschema-two',
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' }
|
||||
}
|
||||
})
|
||||
|
||||
instance.register((subinstance, _opts, next) => {
|
||||
subinstance.addSchema({
|
||||
$id: 'subschema-three',
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' }
|
||||
}
|
||||
})
|
||||
|
||||
subinstance.post('/:hello', {
|
||||
handler () {},
|
||||
schema: {
|
||||
body: { $ref: 'example#/properties/hello' },
|
||||
querystring: { $ref: 'subschema-two#/properties/hello' },
|
||||
params: { $ref: 'subschema-two#/properties/hello' },
|
||||
headers: { $ref: 'subschema-three#/properties/hello' },
|
||||
response: {
|
||||
200: { $ref: 'example#/properties/hello' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
await Swagger.validate(fastify.swagger())
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
})
|
||||
|
||||
test('support nested $ref schema : complex case', async (t) => {
|
||||
const options = {
|
||||
swagger: {},
|
||||
refResolver: {
|
||||
buildLocalReference: (json, _baseUri, _fragment, i) => {
|
||||
return json.$id || `def-${i}`
|
||||
}
|
||||
}
|
||||
}
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, options)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
|
||||
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
|
||||
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
|
||||
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
|
||||
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
|
||||
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerObject, 'object')
|
||||
const definitions = swaggerObject.definitions
|
||||
t.assert.deepStrictEqual(Object.keys(definitions), ['schemaA', 'schemaB', 'schemaC', 'schemaD'])
|
||||
|
||||
// ref must be prefixed by '#/definitions/'
|
||||
t.assert.strictEqual(definitions.schemaC.properties.a.items.$ref, '#/definitions/schemaA')
|
||||
t.assert.strictEqual(definitions.schemaD.properties.b.$ref, '#/definitions/schemaB')
|
||||
t.assert.strictEqual(definitions.schemaD.properties.c.$ref, '#/definitions/schemaC')
|
||||
|
||||
await Swagger.validate(swaggerObject)
|
||||
})
|
||||
|
||||
test('support nested $ref schema : complex case without modifying buildLocalReference', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
|
||||
instance.addSchema({ $id: 'schemaB', type: 'object', properties: { id: { type: 'string' } } })
|
||||
instance.addSchema({ $id: 'schemaC', type: 'object', properties: { a: { type: 'array', items: { $ref: 'schemaA' } } } })
|
||||
instance.addSchema({ $id: 'schemaD', type: 'object', properties: { b: { $ref: 'schemaB' }, c: { $ref: 'schemaC' } } })
|
||||
instance.post('/url1', { schema: { body: { $ref: 'schemaD' }, response: { 200: { $ref: 'schemaB' } } } }, () => {})
|
||||
instance.post('/url2', { schema: { body: { $ref: 'schemaC' }, response: { 200: { $ref: 'schemaA' } } } }, () => {})
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerObject, 'object')
|
||||
|
||||
const definitions = swaggerObject.definitions
|
||||
t.assert.deepStrictEqual(Object.keys(definitions), ['def-0', 'def-1', 'def-2', 'def-3'])
|
||||
|
||||
// ref must be prefixed by '#/definitions/'
|
||||
t.assert.strictEqual(definitions['def-2'].properties.a.items.$ref, '#/definitions/def-0')
|
||||
t.assert.strictEqual(definitions['def-3'].properties.b.$ref, '#/definitions/def-1')
|
||||
t.assert.strictEqual(definitions['def-3'].properties.c.$ref, '#/definitions/def-2')
|
||||
|
||||
await Swagger.validate(swaggerObject)
|
||||
})
|
||||
|
||||
test('trying to overwriting a schema results in a FST_ERR_SCH_ALREADY_PRESENT', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } })
|
||||
t.assert.throws(() => instance.addSchema({ $id: 'schemaA', type: 'object', properties: { id: { type: 'integer' } } }), new FST_ERR_SCH_ALREADY_PRESENT('schemaA'))
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
})
|
||||
|
||||
test('renders $ref schema with enum in headers', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.register(async (instance) => {
|
||||
instance.addSchema({ $id: 'headerA', type: 'object', properties: { 'x-enum-header': { type: 'string', enum: ['OK', 'NOT_OK'] } } })
|
||||
instance.get('/url1', { schema: { headers: { $ref: 'headerA#' }, response: { 200: { type: 'object' } } } }, async () => ({ result: 'OK' }))
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swagger = fastify.swagger()
|
||||
|
||||
await Swagger.validate(swagger)
|
||||
|
||||
t.assert.deepStrictEqual(
|
||||
swagger.paths['/url1'].get.parameters[0],
|
||||
{
|
||||
type: 'string',
|
||||
enum: ['OK', 'NOT_OK'],
|
||||
in: 'header',
|
||||
name: 'x-enum-header',
|
||||
required: false
|
||||
}
|
||||
)
|
||||
})
|
||||
585
backend/node_modules/@fastify/swagger/test/spec/swagger/route.test.js
generated
vendored
Normal file
585
backend/node_modules/@fastify/swagger/test/spec/swagger/route.test.js
generated
vendored
Normal file
@@ -0,0 +1,585 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const Swagger = require('@apidevtools/swagger-parser')
|
||||
const yaml = require('yaml')
|
||||
const fastifySwagger = require('../../../index')
|
||||
const {
|
||||
swaggerOption,
|
||||
schemaBody,
|
||||
schemaConsumes,
|
||||
schemaExtension,
|
||||
schemaHeaders,
|
||||
schemaHeadersParams,
|
||||
schemaParams,
|
||||
schemaQuerystring,
|
||||
schemaSecurity
|
||||
} = require('../../../examples/options')
|
||||
|
||||
test('swagger should return valid swagger object', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
fastify.get('/', () => {})
|
||||
fastify.post('/', () => {})
|
||||
fastify.get('/example', schemaQuerystring, () => {})
|
||||
fastify.post('/example', schemaBody, () => {})
|
||||
fastify.get('/parameters/:id', schemaParams, () => {})
|
||||
fastify.get('/headers', schemaHeaders, () => {})
|
||||
fastify.get('/headers/:id', schemaHeadersParams, () => {})
|
||||
fastify.get('/security', schemaSecurity, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerObject, 'object')
|
||||
|
||||
await Swagger.validate(swaggerObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
})
|
||||
|
||||
test('swagger should return a valid swagger yaml', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
fastify.get('/', () => {})
|
||||
fastify.route({
|
||||
method: ['POST'],
|
||||
url: '/',
|
||||
handler: () => {}
|
||||
})
|
||||
fastify.get('/example', schemaQuerystring, () => {})
|
||||
fastify.post('/example', schemaBody, () => {})
|
||||
fastify.get('/parameters/:id', schemaParams, () => {})
|
||||
fastify.get('/headers', schemaHeaders, () => {})
|
||||
fastify.get('/headers/:id', schemaHeadersParams, () => {})
|
||||
fastify.get('/security', schemaSecurity, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerYaml = fastify.swagger({ yaml: true })
|
||||
t.assert.strictEqual(typeof swaggerYaml, 'string')
|
||||
yaml.parse(swaggerYaml)
|
||||
t.assert.ok(true, 'valid swagger yaml')
|
||||
})
|
||||
|
||||
test('route options - deprecated', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
deprecated: true,
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
some: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.post('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
await Swagger.validate(swaggerObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.ok(swaggerObject.paths['/'])
|
||||
})
|
||||
|
||||
test('route options - meta', async (t) => {
|
||||
t.plan(8)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
operationId: 'doSomething',
|
||||
summary: 'Route summary',
|
||||
tags: ['tag'],
|
||||
description: 'Route description',
|
||||
produces: ['application/octet-stream'],
|
||||
consumes: ['application/x-www-form-urlencoded'],
|
||||
externalDocs: {
|
||||
description: 'Find more info here',
|
||||
url: 'https://swagger.io'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.get('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.strictEqual(opts.schema.operationId, definedPath.operationId)
|
||||
t.assert.strictEqual(opts.schema.summary, definedPath.summary)
|
||||
t.assert.deepStrictEqual(opts.schema.tags, definedPath.tags)
|
||||
t.assert.strictEqual(opts.schema.description, definedPath.description)
|
||||
t.assert.deepStrictEqual(opts.schema.produces, definedPath.produces)
|
||||
t.assert.deepStrictEqual(opts.schema.consumes, definedPath.consumes)
|
||||
t.assert.strictEqual(opts.schema.externalDocs, definedPath.externalDocs)
|
||||
})
|
||||
|
||||
test('route options - consumes', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
fastify.post('/', schemaConsumes, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
const definedPath = api.paths['/'].post
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepStrictEqual(definedPath.parameters, [{
|
||||
in: 'formData',
|
||||
name: 'hello',
|
||||
description: 'hello',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}])
|
||||
})
|
||||
|
||||
test('route options - extension', async (t) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { swagger: { 'x-ternal': true } })
|
||||
fastify.get('/', schemaExtension, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
t.assert.ok(api['x-ternal'])
|
||||
t.assert.deepStrictEqual(api['x-ternal'], true)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepStrictEqual(definedPath['x-tension'], true)
|
||||
})
|
||||
|
||||
test('route options - querystring', async (t) => {
|
||||
t.plan(2)
|
||||
|
||||
const opts = {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' },
|
||||
world: { type: 'string', description: 'world description' }
|
||||
},
|
||||
required: ['hello']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
fastify.get('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepStrictEqual(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', async (t) => {
|
||||
t.plan(2)
|
||||
const opts = {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
enumKey: { type: 'string', enum: ['enum1', 'enum2'] }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
fastify.get('/test/:enumKey', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
const definedPath = api.paths['/test/{enumKey}'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepStrictEqual(definedPath.parameters, [{
|
||||
in: 'path',
|
||||
name: 'enumKey',
|
||||
type: 'string',
|
||||
enum: ['enum1', 'enum2'],
|
||||
required: true
|
||||
}])
|
||||
})
|
||||
|
||||
test('custom verbs should not be interpreted as path params', async (t) => {
|
||||
t.plan(2)
|
||||
const opts = {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
fastify.get('/resource/:id/sub-resource::watch', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
const definedPath = api.paths['/resource/{id}/sub-resource:watch'].get
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepStrictEqual(definedPath.parameters, [{
|
||||
in: 'path',
|
||||
name: 'id',
|
||||
type: 'string',
|
||||
required: true
|
||||
}])
|
||||
})
|
||||
|
||||
test('swagger json output should not omit consume in querystring schema', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify({
|
||||
ajv: {
|
||||
plugins: [
|
||||
function (ajv) {
|
||||
ajv.addKeyword({ keyword: 'x-consume' })
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
const schemaQuerystring = {
|
||||
schema: {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'object',
|
||||
'x-consume': 'application/json',
|
||||
required: ['bar'],
|
||||
properties: {
|
||||
bar: { type: 'string' },
|
||||
baz: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fastify.get('/', schemaQuerystring, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
try {
|
||||
fastify.swagger()
|
||||
t.assert.fail('error was not thrown')
|
||||
} catch (err) {
|
||||
if (err.message.startsWith('Complex serialization is not supported by Swagger')) {
|
||||
t.assert.ok(true, 'error was thrown')
|
||||
} else {
|
||||
t.error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
test('swagger should not support Links', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
fastify.get('/user/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'the user identifier, as userId'
|
||||
}
|
||||
},
|
||||
required: ['id']
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
uuid: {
|
||||
type: 'string',
|
||||
format: 'uuid'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
links: {
|
||||
200: {
|
||||
address: {
|
||||
operationId: 'getUserAddress',
|
||||
parameters: {
|
||||
id: '$request.path.id'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/user/:id/address', {
|
||||
schema: {
|
||||
operationId: 'getUserAddress',
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string',
|
||||
description: 'the user identifier, as userId'
|
||||
}
|
||||
},
|
||||
required: ['id']
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
t.assert.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', async (t) => {
|
||||
t.plan(6)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
|
||||
fastify.get('/address1/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' }
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
apiKey: {
|
||||
type: 'string',
|
||||
description: 'api token'
|
||||
},
|
||||
somethingElse: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/address2/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' }
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
authKey: {
|
||||
type: 'string',
|
||||
description: 'auth token'
|
||||
},
|
||||
somethingElse: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerObject, 'object')
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
|
||||
t.assert.strictEqual(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')), undefined)
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
|
||||
})
|
||||
|
||||
test('security querystrings ignored when declared in security and securityScheme', async (t) => {
|
||||
t.plan(6)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: {
|
||||
securityDefinitions: {
|
||||
apiKey: {
|
||||
type: 'apiKey',
|
||||
name: 'apiKey',
|
||||
in: 'query'
|
||||
}
|
||||
},
|
||||
security: [{
|
||||
apiKey: []
|
||||
}]
|
||||
}
|
||||
})
|
||||
|
||||
fastify.get('/address1/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' }
|
||||
}
|
||||
},
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
apiKey: {
|
||||
type: 'string',
|
||||
description: 'api token'
|
||||
},
|
||||
somethingElse: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
fastify.get('/address2/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' }
|
||||
}
|
||||
},
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
authKey: {
|
||||
type: 'string',
|
||||
description: 'auth token'
|
||||
},
|
||||
somethingElse: {
|
||||
type: 'string',
|
||||
description: 'common field'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(typeof swaggerObject, 'object')
|
||||
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
t.assert.ok(true, 'valid swagger object')
|
||||
t.assert.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'somethingElse')))
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'somethingElse')))
|
||||
t.assert.strictEqual(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')), undefined)
|
||||
t.assert.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
|
||||
})
|
||||
|
||||
test('verify generated path param definition with route prefixing', async (t) => {
|
||||
const opts = {
|
||||
schema: {}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, swaggerOption)
|
||||
await fastify.register(function (app, _, done) {
|
||||
app.get('/:userId', opts, () => {})
|
||||
|
||||
done()
|
||||
}, { prefix: '/v1' })
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/v1/{userId}'].get
|
||||
|
||||
t.assert.deepStrictEqual(definedPath.parameters, [{
|
||||
in: 'path',
|
||||
name: 'userId',
|
||||
type: 'string',
|
||||
required: true
|
||||
}])
|
||||
})
|
||||
790
backend/node_modules/@fastify/swagger/test/spec/swagger/schema.test.js
generated
vendored
Normal file
790
backend/node_modules/@fastify/swagger/test/spec/swagger/schema.test.js
generated
vendored
Normal file
@@ -0,0 +1,790 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const Swagger = require('@apidevtools/swagger-parser')
|
||||
const fastifySwagger = require('../../../index')
|
||||
const S = require('fluent-json-schema')
|
||||
|
||||
test('support file in json schema', async t => {
|
||||
const opts7 = {
|
||||
schema: {
|
||||
consumes: ['application/x-www-form-urlencoded'],
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
description: 'hello',
|
||||
type: 'string',
|
||||
contentEncoding: 'binary'
|
||||
}
|
||||
},
|
||||
required: ['hello']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.post('/', opts7, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].post
|
||||
t.assert.ok(definedPath)
|
||||
t.assert.deepStrictEqual(definedPath.parameters, [{
|
||||
in: 'formData',
|
||||
name: 'hello',
|
||||
description: 'hello',
|
||||
required: true,
|
||||
type: 'file'
|
||||
}])
|
||||
})
|
||||
|
||||
test('support response description', async t => {
|
||||
const opts8 = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
description: 'Response OK!',
|
||||
type: 'object'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opts8, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['200'].description, 'Response OK!')
|
||||
})
|
||||
|
||||
test('support response description fallback to its $ref', async t => {
|
||||
const opts = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
$ref: 'my-ref#'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
fastify.addSchema({
|
||||
$id: 'my-ref',
|
||||
description: 'Response OK!',
|
||||
type: 'string'
|
||||
})
|
||||
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['200'].description, 'Response OK!')
|
||||
})
|
||||
|
||||
test('response default description', async t => {
|
||||
const opts9 = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
type: 'object'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opts9, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['200'].description, 'Default Response')
|
||||
})
|
||||
|
||||
test('response 2xx', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
response: {
|
||||
'2xx': {
|
||||
type: 'object'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['200'].description, 'Default Response')
|
||||
t.assert.strictEqual(definedPath.responses['2XX'], undefined)
|
||||
})
|
||||
|
||||
test('response conflict 2xx and 200', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
response: {
|
||||
'2xx': {
|
||||
type: 'object',
|
||||
description: '2xx'
|
||||
},
|
||||
200: {
|
||||
type: 'object',
|
||||
description: '200'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['200'].description, '200')
|
||||
t.assert.strictEqual(definedPath.responses['2XX'], undefined)
|
||||
})
|
||||
|
||||
test('support status code 204', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
response: {
|
||||
204: {
|
||||
type: 'null',
|
||||
description: 'No Content'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['204'].description, 'No Content')
|
||||
t.assert.strictEqual(definedPath.responses['204'].schema, undefined)
|
||||
})
|
||||
|
||||
test('support empty response body for different status than 204', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
response: {
|
||||
204: {
|
||||
type: 'null',
|
||||
description: 'No Content'
|
||||
},
|
||||
503: {
|
||||
type: 'null',
|
||||
description: 'Service Unavailable'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
|
||||
t.assert.deepStrictEqual(definedPath.responses['204'].description, 'No Content')
|
||||
t.assert.strictEqual(definedPath.responses['204'].content, undefined)
|
||||
t.assert.strictEqual(definedPath.responses['503'].type, undefined)
|
||||
|
||||
t.assert.deepStrictEqual(definedPath.responses['503'].description, 'Service Unavailable')
|
||||
t.assert.strictEqual(definedPath.responses['503'].content, undefined)
|
||||
t.assert.strictEqual(definedPath.responses['503'].type, undefined)
|
||||
})
|
||||
|
||||
test('support response headers', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: {
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
'X-WORLD': {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['200'].headers, opt.schema.response['200'].headers)
|
||||
t.assert.strictEqual(definedPath.responses['200'].schema.headers, undefined)
|
||||
})
|
||||
|
||||
test('response: description and x-response-description', async () => {
|
||||
const description = 'description - always that of response body, sometimes also that of response as a whole'
|
||||
const responseDescription = 'description only for the response as a whole'
|
||||
|
||||
await test('description without x-response-description doubles as response description', async t => {
|
||||
// Given a /description endpoint with only a |description| field in its response schema
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/description', {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
description,
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
await fastify.ready()
|
||||
|
||||
// When the Swagger schema is generated
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
// Then the /description endpoint uses the |description| as both the description of the Response Object as well as of its Schema Object
|
||||
const responseObject = api.paths['/description'].get.responses['200']
|
||||
t.assert.ok(responseObject)
|
||||
t.assert.strictEqual(responseObject.description, description)
|
||||
t.assert.strictEqual(responseObject.schema.description, description)
|
||||
})
|
||||
|
||||
await test('description alongside x-response-description only describes response body', async t => {
|
||||
// Given a /responseDescription endpoint that also has a |'x-response-description'| field in its response schema
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/responseDescription', {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
'x-response-description': responseDescription,
|
||||
description,
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
await fastify.ready()
|
||||
|
||||
// When the Swagger schema is generated
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
// Then the /responseDescription endpoint uses the |responseDescription| only for the Response Object and the |description| only for the Schema Object
|
||||
const responseObject = api.paths['/responseDescription'].get.responses['200']
|
||||
t.assert.ok(responseObject)
|
||||
t.assert.strictEqual(responseObject.description, responseDescription)
|
||||
t.assert.strictEqual(responseObject.schema.description, description)
|
||||
t.assert.strictEqual(responseObject.schema.responseDescription, undefined)
|
||||
})
|
||||
})
|
||||
|
||||
test('support "default" parameter', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
response: {
|
||||
200: {
|
||||
description: 'Expected Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
},
|
||||
default: {
|
||||
description: 'Default Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
bar: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.responses.default)), {
|
||||
description: 'Default Response',
|
||||
schema: {
|
||||
description: 'Default Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
bar: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('fluent-json-schema', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
response: {
|
||||
200: S.object()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { swagger: true })
|
||||
fastify.get('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].get
|
||||
t.assert.deepStrictEqual(definedPath.responses['200'].description, 'Default Response')
|
||||
})
|
||||
|
||||
test('support "patternProperties" in json schema', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
patternProperties: {
|
||||
'^[a-z]{2,3}-[a-zA-Z]{2}$': {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
},
|
||||
response: {
|
||||
200: {
|
||||
description: 'Expected Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'object',
|
||||
patternProperties: {
|
||||
'^[a-z]{2,3}-[a-zA-Z]{2}$': {
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
additionalProperties: false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, { swagger: true })
|
||||
fastify.post('/', opt, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].post
|
||||
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.parameters[0].schema)), {
|
||||
type: 'object',
|
||||
additionalProperties: { type: 'string' }
|
||||
})
|
||||
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.responses[200])), {
|
||||
description: 'Expected Response',
|
||||
schema: {
|
||||
description: 'Expected Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'object',
|
||||
additionalProperties: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('support "const" keyword', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
constantProp: { const: 'my-const' },
|
||||
constantPropNull: { const: null },
|
||||
constantPropZero: { const: 0 },
|
||||
constantPropFalse: { const: false },
|
||||
constantPropEmptyString: { const: '' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.post('/', opt, () => {})
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].post
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.parameters[0].schema)), {
|
||||
type: 'object',
|
||||
properties: {
|
||||
obj: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
constantProp: {
|
||||
enum: ['my-const']
|
||||
},
|
||||
constantPropZero: {
|
||||
enum: [0]
|
||||
},
|
||||
constantPropNull: {
|
||||
enum: [null]
|
||||
},
|
||||
constantPropFalse: {
|
||||
enum: [false]
|
||||
},
|
||||
constantPropEmptyString: {
|
||||
enum: ['']
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('support "description" keyword', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
description: 'Body description',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'number'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.post('/', opt, () => { })
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/'].post
|
||||
t.assert.deepStrictEqual(definedPath.parameters[0].description, 'Body description')
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.parameters[0].schema)), {
|
||||
type: 'object',
|
||||
description: 'Body description',
|
||||
properties: {
|
||||
foo: {
|
||||
type: 'number'
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('no head routes by default', async (t) => {
|
||||
const fastify = Fastify({ exposeHeadRoutes: true })
|
||||
await fastify.register(fastifySwagger, {
|
||||
routePrefix: '/docs',
|
||||
exposeRoute: true
|
||||
})
|
||||
|
||||
fastify.get('/with-head', {
|
||||
schema: {
|
||||
operationId: 'with-head',
|
||||
response: {
|
||||
200: {
|
||||
description: 'Expected Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
t.assert.deepStrictEqual(
|
||||
api.paths['/with-head'].get.responses['200'].description,
|
||||
'Expected Response'
|
||||
)
|
||||
t.assert.deepStrictEqual(
|
||||
api.paths['/with-head'].head,
|
||||
undefined
|
||||
)
|
||||
})
|
||||
|
||||
test('support "exposeHeadRoutes" option', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, {
|
||||
routePrefix: '/docs',
|
||||
exposeHeadRoutes: true,
|
||||
exposeRoute: true
|
||||
})
|
||||
|
||||
fastify.get('/with-head', {
|
||||
schema: {
|
||||
operationId: 'with-head',
|
||||
response: {
|
||||
200: {
|
||||
description: 'Expected Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
t.assert.deepStrictEqual(
|
||||
api.paths['/with-head'].get.responses['200'].description,
|
||||
'Expected Response'
|
||||
)
|
||||
t.assert.deepStrictEqual(
|
||||
api.paths['/with-head'].head.responses['200'].description,
|
||||
'Expected Response'
|
||||
)
|
||||
})
|
||||
|
||||
test('support "exposeHeadRoutes" option at route level', async (t) => {
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger, {
|
||||
routePrefix: '/docs',
|
||||
exposeRoute: true
|
||||
})
|
||||
|
||||
fastify.get('/with-head', {
|
||||
schema: {
|
||||
operationId: 'with-head',
|
||||
response: {
|
||||
200: {
|
||||
description: 'Expected Response',
|
||||
type: 'object',
|
||||
properties: {
|
||||
foo: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
config: {
|
||||
swagger: {
|
||||
exposeHeadRoute: true
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
t.assert.deepStrictEqual(
|
||||
api.paths['/with-head'].get.responses['200'].description,
|
||||
'Expected Response'
|
||||
)
|
||||
t.assert.deepStrictEqual(
|
||||
api.paths['/with-head'].head.responses['200'].description,
|
||||
'Expected Response'
|
||||
)
|
||||
})
|
||||
|
||||
test('add default properties for url params when missing schema', async t => {
|
||||
const opt = {}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.get('/:userId', opt, () => { })
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/{userId}'].get
|
||||
|
||||
t.assert.deepStrictEqual(definedPath.parameters[0], {
|
||||
type: 'string',
|
||||
required: true,
|
||||
in: 'path',
|
||||
name: 'userId'
|
||||
})
|
||||
})
|
||||
|
||||
test('add default properties for url params when missing schema.params', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
bio: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.post('/:userId', opt, () => { })
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
const api = await Swagger.validate(swaggerObject)
|
||||
|
||||
const definedPath = api.paths['/{userId}'].post
|
||||
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.parameters[0].schema)), {
|
||||
type: 'object',
|
||||
properties: {
|
||||
bio: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
})
|
||||
t.assert.deepStrictEqual(definedPath.parameters[1], {
|
||||
in: 'path',
|
||||
name: 'userId',
|
||||
type: 'string',
|
||||
required: true
|
||||
})
|
||||
})
|
||||
|
||||
test('avoid overwriting params when schema.params is provided', async t => {
|
||||
const opt = {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
},
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
bio: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fastify = Fastify()
|
||||
await fastify.register(fastifySwagger)
|
||||
fastify.post('/:userId', opt, () => { })
|
||||
await fastify.ready()
|
||||
|
||||
const swaggerObject = fastify.swagger()
|
||||
|
||||
const definedPath = swaggerObject.paths['/{userId}'].post
|
||||
|
||||
t.assert.deepStrictEqual(JSON.parse(JSON.stringify(definedPath.parameters[0].schema)), {
|
||||
type: 'object',
|
||||
properties: {
|
||||
bio: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
})
|
||||
t.assert.deepStrictEqual(definedPath.parameters[1], {
|
||||
in: 'path',
|
||||
name: 'id',
|
||||
type: 'string',
|
||||
required: true
|
||||
})
|
||||
})
|
||||
414
backend/node_modules/@fastify/swagger/test/transform.test.js
generated
vendored
Normal file
414
backend/node_modules/@fastify/swagger/test/transform.test.js
generated
vendored
Normal file
@@ -0,0 +1,414 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('fastify')
|
||||
const fastifySwagger = require('..')
|
||||
const Joi = require('joi')
|
||||
const Convert = require('joi-to-json')
|
||||
|
||||
const params = Joi
|
||||
.object()
|
||||
.keys({
|
||||
property: Joi.string().required()
|
||||
})
|
||||
const opts = {
|
||||
schema: { params }
|
||||
}
|
||||
const convertible = ['params', 'body', 'querystring']
|
||||
const validTransform = ({ schema, url }) => {
|
||||
const newSchema = Object.keys(schema).reduce((transformed, key) => {
|
||||
transformed[key] = convertible.includes(key)
|
||||
? Convert(schema[key])
|
||||
: schema[key]
|
||||
return transformed
|
||||
},
|
||||
{})
|
||||
return { schema: newSchema, url }
|
||||
}
|
||||
const valid = {
|
||||
transform: validTransform
|
||||
}
|
||||
|
||||
const invalid = {
|
||||
transform: 'wrong type'
|
||||
}
|
||||
|
||||
test('transform should fail with a value other than Function', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, invalid)
|
||||
|
||||
fastify.setValidatorCompiler(({ schema }) => params.validate(schema))
|
||||
fastify.get('/example', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.throws(fastify.swagger)
|
||||
})
|
||||
|
||||
test('transform should work with a Function', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, valid)
|
||||
|
||||
fastify.setValidatorCompiler(({ schema }) => params.validate(schema))
|
||||
fastify.get('/example', opts, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('transform can access route', async (t) => {
|
||||
t.plan(5)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '1.0.0' } },
|
||||
transform: ({ route }) => {
|
||||
t.assert.ok(route)
|
||||
t.assert.strictEqual(route.method, 'GET')
|
||||
t.assert.strictEqual(route.url, '/example')
|
||||
t.assert.strictEqual(route.constraints.version, '1.0.0')
|
||||
return { schema: route.schema, url: route.url }
|
||||
}
|
||||
})
|
||||
fastify.get('/example', { constraints: { version: '1.0.0' } }, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('transform can access openapi object', async (t) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '1.0.0' } },
|
||||
transform: ({ route, openapiObject }) => {
|
||||
t.assert.ok(openapiObject)
|
||||
t.assert.strictEqual(openapiObject.openapi, '3.0.3')
|
||||
t.assert.strictEqual(openapiObject.info.version, '1.0.0')
|
||||
return {
|
||||
schema: route.schema,
|
||||
url: route.url
|
||||
}
|
||||
}
|
||||
})
|
||||
fastify.get('/example', () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('transform can access swagger object', async (t) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: { info: { version: '1.0.0' } },
|
||||
transform: ({ route, swaggerObject }) => {
|
||||
t.assert.ok(swaggerObject)
|
||||
t.assert.strictEqual(swaggerObject.swagger, '2.0')
|
||||
t.assert.strictEqual(swaggerObject.info.version, '1.0.0')
|
||||
return {
|
||||
schema: route.schema,
|
||||
url: route.url
|
||||
}
|
||||
}
|
||||
})
|
||||
fastify.get('/example', () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('transform can hide routes based on openapi version', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '2.0.0' } },
|
||||
transform: ({ schema, route, openapiObject }) => {
|
||||
const transformedSchema = Object.assign({}, schema)
|
||||
if (route?.constraints?.version !== openapiObject.info.version) transformedSchema.hide = true
|
||||
return { schema: transformedSchema, url: route.url }
|
||||
}
|
||||
})
|
||||
fastify.get('/example', { constraints: { version: '1.0.0' } }, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(openapiObject.paths['/example'], undefined)
|
||||
})
|
||||
|
||||
test('endpoint transform should fail with a value other than Function', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {})
|
||||
|
||||
fastify.setValidatorCompiler(({ schema }) => params.validate(schema))
|
||||
fastify.get('/example', {
|
||||
...opts,
|
||||
config: {
|
||||
swaggerTransform: 'wrong type'
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.throws(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform should work with a Function', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, valid)
|
||||
|
||||
fastify.setValidatorCompiler(({ schema }) => params.validate(schema))
|
||||
fastify.get('/example', {
|
||||
...opts,
|
||||
config: { swaggerTransform: validTransform }
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform can access route', async (t) => {
|
||||
t.plan(5)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '1.0.0' } }
|
||||
})
|
||||
fastify.get('/example', {
|
||||
constraints: { version: '1.0.0' },
|
||||
config: {
|
||||
swaggerTransform: ({ route }) => {
|
||||
t.assert.ok(route)
|
||||
t.assert.strictEqual(route.method, 'GET')
|
||||
t.assert.strictEqual(route.url, '/example')
|
||||
t.assert.strictEqual(route.constraints.version, '1.0.0')
|
||||
return { schema: route.schema, url: route.url }
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform can access openapi object', async (t) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '1.0.0' } }
|
||||
})
|
||||
fastify.get('/example', {
|
||||
config: {
|
||||
swaggerTransform: ({ route, openapiObject }) => {
|
||||
t.assert.ok(openapiObject)
|
||||
t.assert.strictEqual(openapiObject.openapi, '3.0.3')
|
||||
t.assert.strictEqual(openapiObject.info.version, '1.0.0')
|
||||
return {
|
||||
schema: route.schema,
|
||||
url: route.url
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform can access swagger object', async (t) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: { info: { version: '1.0.0' } }
|
||||
})
|
||||
fastify.get('/example', {
|
||||
config: {
|
||||
swaggerTransform: ({ route, swaggerObject }) => {
|
||||
t.assert.ok(swaggerObject)
|
||||
t.assert.strictEqual(swaggerObject.swagger, '2.0')
|
||||
t.assert.strictEqual(swaggerObject.info.version, '1.0.0')
|
||||
return {
|
||||
schema: route.schema,
|
||||
url: route.url
|
||||
}
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform can hide routes based on openapi version', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '2.0.0' } }
|
||||
})
|
||||
fastify.get('/example', {
|
||||
constraints: { version: '1.0.0' },
|
||||
config: {
|
||||
swaggerTransform: ({ schema, route, openapiObject }) => {
|
||||
const transformedSchema = Object.assign({}, schema)
|
||||
if (route?.constraints?.version !== openapiObject.info.version) transformedSchema.hide = true
|
||||
return { schema: transformedSchema, url: route.url }
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(openapiObject.paths['/example'], undefined)
|
||||
})
|
||||
|
||||
test('endpoint transform takes precedence over global swagger transform', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: { info: { version: '1.0.0' } },
|
||||
transform: ({ schema, url }) => {
|
||||
t.assert.fail('the global transform function should be ignored')
|
||||
return validTransform({ schema, url })
|
||||
}
|
||||
|
||||
})
|
||||
fastify.get('/example', {
|
||||
config: {
|
||||
swaggerTransform: ({ schema, route }) => {
|
||||
const transformedSchema = Object.assign({}, schema)
|
||||
t.assert.ok(transformedSchema)
|
||||
return { schema: transformedSchema, url: route.url }
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform takes precedence over global openapi transform', async (t) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '2.0.0' } },
|
||||
transform: ({ schema, url }) => {
|
||||
t.assert.fail('the global transform function should be ignored')
|
||||
return validTransform({ schema, url })
|
||||
}
|
||||
|
||||
})
|
||||
fastify.get('/example', {
|
||||
config: {
|
||||
swaggerTransform: ({ schema, route }) => {
|
||||
const transformedSchema = Object.assign({}, schema)
|
||||
t.assert.ok(transformedSchema)
|
||||
return { schema: transformedSchema, url: route.url }
|
||||
}
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform with value "false" disables the global swagger transform', async (t) => {
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: { info: { version: '1.0.0' } },
|
||||
transform: () => { throw Error('should not be run') }
|
||||
})
|
||||
fastify.get('/example/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
config: {
|
||||
swaggerTransform: false
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('endpoint transform with value "false" disables the global openapi transform', async (t) => {
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '2.0.0' } },
|
||||
transform: () => { throw Error('should not be run') }
|
||||
})
|
||||
fastify.get('/example/:id', {
|
||||
schema: {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
config: {
|
||||
swaggerTransform: false
|
||||
}
|
||||
}, () => {})
|
||||
|
||||
await fastify.ready()
|
||||
t.assert.doesNotThrow(fastify.swagger)
|
||||
})
|
||||
|
||||
test('transformObject can modify the openapi object', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: { info: { version: '2.0.0' } },
|
||||
transformObject: ({ openapiObject }) => {
|
||||
openapiObject.info.title = 'Transformed'
|
||||
return openapiObject
|
||||
}
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
const openapiObject = fastify.swagger()
|
||||
t.assert.strictEqual(openapiObject.info.title, 'Transformed')
|
||||
})
|
||||
|
||||
test('transformObject can modify the swagger object', async (t) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
swagger: { info: { version: '2.0.0' } },
|
||||
transformObject: ({ swaggerObject }) => {
|
||||
swaggerObject.info.title = 'Transformed'
|
||||
return swaggerObject
|
||||
}
|
||||
})
|
||||
|
||||
await fastify.ready()
|
||||
const swaggerObject = fastify.swagger()
|
||||
t.assert.strictEqual(swaggerObject.info.title, 'Transformed')
|
||||
})
|
||||
163
backend/node_modules/@fastify/swagger/test/util.test.js
generated
vendored
Normal file
163
backend/node_modules/@fastify/swagger/test/util.test.js
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
'use strict'
|
||||
|
||||
const { test, describe } = require('node:test')
|
||||
const { formatParamUrl } = require('../lib/util/format-param-url')
|
||||
const { hasParams, matchParams } = require('../lib/util/match-params')
|
||||
const { generateParamsSchema, paramName } = require('../lib/util/generate-params-schema')
|
||||
const { shouldRouteHide } = require('../lib/util/should-route-hide')
|
||||
|
||||
const cases = [
|
||||
['/example/:userId', '/example/{userId}'],
|
||||
['/example/:userId/:secretToken', '/example/{userId}/{secretToken}'],
|
||||
['/example/near/:lat-:lng/radius/:r', '/example/near/{lat}-{lng}/radius/{r}'],
|
||||
['/example/near/:lat_1-:lng_1/radius/:r_1', '/example/near/{lat_1}-{lng_1}/radius/{r_1}'],
|
||||
['/example/*', '/example/{*}'],
|
||||
['/example/:file(^\\d+).png', '/example/{file}.png'],
|
||||
['/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', '/example/at/{hour}h{minute}m'],
|
||||
['/example/at/(^\\d{2})h(^\\d{2})m', '/example/at/{regexp1}h{regexp2}m'],
|
||||
['/example/at/(^([0-9]{2})h$)-(^([0-9]{2})m$)', '/example/at/{regexp1}-{regexp2}'],
|
||||
['/name::verb', '/name:verb'],
|
||||
['/api/v1/postalcode-jp/:code(^[0-9]{7}$)', '/api/v1/postalcode-jp/{code}'],
|
||||
['/api/v1/postalcode-jp/(^[0-9]{7}$)', '/api/v1/postalcode-jp/{regexp1}']
|
||||
]
|
||||
|
||||
describe('formatParamUrl', () => {
|
||||
for (const kase of cases) {
|
||||
test(`formatParamUrl ${kase}`, (t) => {
|
||||
t.assert.strictEqual(formatParamUrl(kase[0]), kase[1])
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
describe('hasParams function', () => {
|
||||
test('should return false for empty url', (t) => {
|
||||
const url = ''
|
||||
const result = hasParams(url)
|
||||
t.assert.strictEqual(result, false)
|
||||
})
|
||||
|
||||
test('should return true for url with parameters', (t) => {
|
||||
const url = '/example/{userId}'
|
||||
const result = hasParams(url)
|
||||
t.assert.strictEqual(result, true)
|
||||
})
|
||||
|
||||
test('should return true for url with multiple parameters', (t) => {
|
||||
const url = '/example/{userId}/{secretToken}'
|
||||
const result = hasParams(url)
|
||||
t.assert.strictEqual(result, true)
|
||||
})
|
||||
|
||||
test('should return false for url without parameters', (t) => {
|
||||
const url = '/example/path'
|
||||
const result = hasParams(url)
|
||||
t.assert.strictEqual(result, false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('matchParams function', (t) => {
|
||||
test('should return an empty array for empty url', (t) => {
|
||||
const url = ''
|
||||
const result = matchParams(url)
|
||||
t.assert.deepStrictEqual(result, [])
|
||||
})
|
||||
|
||||
test('should return an array of matched parameters', (t) => {
|
||||
const url = '/example/{userId}/{secretToken}'
|
||||
const result = matchParams(url)
|
||||
t.assert.deepStrictEqual(result, ['{userId}', '{secretToken}'])
|
||||
})
|
||||
|
||||
test('should return an empty array for url without parameters', (t) => {
|
||||
const url = '/example/path'
|
||||
const result = matchParams(url)
|
||||
t.assert.deepStrictEqual(result, [])
|
||||
})
|
||||
})
|
||||
|
||||
describe('generateParamsSchema function', (t) => {
|
||||
const urlsToShemas = [
|
||||
[
|
||||
'/example/{userId}', {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
userId: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
[
|
||||
'/example/{userId}/{secretToken}', {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
userId: {
|
||||
type: 'string'
|
||||
},
|
||||
secretToken: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
[
|
||||
'/example/near/{lat}-{lng}', {
|
||||
params: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
lat: {
|
||||
type: 'string'
|
||||
},
|
||||
lng: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
test('generateParamsSchema', (t) => {
|
||||
for (const [url, expectedSchema] of urlsToShemas) {
|
||||
const result = generateParamsSchema(url)
|
||||
|
||||
t.assert.deepStrictEqual(result, expectedSchema)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('paramName function', () => {
|
||||
test('should return the captured value from the param', (t) => {
|
||||
const param = '{userId}'
|
||||
const result = paramName(param)
|
||||
t.assert.strictEqual(result, 'userId')
|
||||
})
|
||||
|
||||
test('should return the same value if there are no captures', (t) => {
|
||||
const param = 'userId'
|
||||
const result = paramName(param)
|
||||
t.assert.strictEqual(result, 'userId')
|
||||
})
|
||||
})
|
||||
|
||||
describe('shouldRouteHide', () => {
|
||||
test('shouldRouteHide should return true for hidden route', (t) => {
|
||||
t.assert.ok(shouldRouteHide({ hide: true }, {}))
|
||||
})
|
||||
|
||||
test('shouldRouteHide should return true for hideUntagged', (t) => {
|
||||
t.assert.ok(shouldRouteHide({ tags: [] }, { hideUntagged: true }))
|
||||
})
|
||||
|
||||
test('shouldRouteHide should return true for hiddenTag', (t) => {
|
||||
t.assert.ok(shouldRouteHide({ tags: ['x-test'] }, { hiddenTag: 'x-test' }))
|
||||
})
|
||||
|
||||
test('shouldRouteHide should return false for non hidden route', (t) => {
|
||||
t.assert.equal(shouldRouteHide({}, {}), false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user