fastify-swagger
Advanced tools
Comparing version 4.12.0 to 4.12.1
@@ -1,2 +0,2 @@ | ||
import { FastifyPluginCallback } from 'fastify'; | ||
import { FastifyPluginCallback, onRequestHookHandler, preHandlerHookHandler } from 'fastify'; | ||
import { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types'; | ||
@@ -28,3 +28,3 @@ | ||
*/ | ||
operationId?: string; | ||
operationId?: string; | ||
} | ||
@@ -40,3 +40,3 @@ | ||
export const fastifySwagger: FastifyPluginCallback<SwaggerOptions>; | ||
export type SwaggerOptions = (FastifyStaticSwaggerOptions | FastifyDynamicSwaggerOptions); | ||
@@ -65,2 +65,6 @@ export interface FastifySwaggerOptions { | ||
transformStaticCSP?: (header: string) => string | ||
/** | ||
* route hooks | ||
*/ | ||
uiHooks?: FastifySwaggerUiHooksOptions | ||
} | ||
@@ -133,2 +137,7 @@ | ||
export type FastifySwaggerUiHooksOptions = Partial<{ | ||
onRequest?: onRequestHookHandler, | ||
preHandler?: preHandlerHookHandler, | ||
}> | ||
export default fastifySwagger; |
@@ -5,3 +5,3 @@ 'use strict' | ||
const { shouldRouteHide } = require('../../util/common') | ||
const { prepareDefaultOptions, prepareOpenapiObject, prepareOpenapiMethod, normalizeUrl } = require('./utils') | ||
const { prepareDefaultOptions, prepareOpenapiObject, prepareOpenapiMethod, prepareOpenapiSchemas, normalizeUrl } = require('./utils') | ||
@@ -24,6 +24,6 @@ module.exports = function (opts, cache, routes, Ref, done) { | ||
ref = Ref() | ||
openapiObject.components.schemas = { | ||
openapiObject.components.schemas = prepareOpenapiSchemas({ | ||
...openapiObject.components.schemas, | ||
...(ref.definitions().definitions) | ||
} | ||
}, ref) | ||
@@ -33,4 +33,8 @@ // Swagger doesn't accept $id on /definitions schemas. | ||
// to remove them at the end of the process | ||
// definitions are added by resolve but they are replace by components.schemas | ||
Object.values(openapiObject.components.schemas) | ||
.forEach((_) => { delete _.$id }) | ||
.forEach((_) => { | ||
delete _.$id | ||
delete _.definitions | ||
}) | ||
@@ -37,0 +41,0 @@ for (const route of routes) { |
@@ -322,2 +322,14 @@ 'use strict' | ||
function prepareOpenapiSchemas (schemas, ref) { | ||
return Object.entries(schemas) | ||
.reduce((res, [name, schema]) => { | ||
const _ = { ...schema } | ||
const resolved = transformDefsToComponents(ref.resolve(_, { externalSchemas: [schemas] })) | ||
return { | ||
...res, | ||
[name]: resolved | ||
} | ||
}, {}) | ||
} | ||
module.exports = { | ||
@@ -327,3 +339,4 @@ prepareDefaultOptions, | ||
prepareOpenapiMethod, | ||
prepareOpenapiSchemas, | ||
normalizeUrl | ||
} |
{ | ||
"name": "fastify-swagger", | ||
"version": "4.12.0", | ||
"version": "4.12.1", | ||
"description": "Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation", | ||
@@ -49,3 +49,3 @@ "main": "index.js", | ||
"swagger-parser": "^10.0.2", | ||
"swagger-ui-dist": "3.52.2", | ||
"swagger-ui-dist": "3.52.3", | ||
"tap": "^15.0.1", | ||
@@ -52,0 +52,0 @@ "tsd": "^0.17.0" |
@@ -7,29 +7,81 @@ 'use strict' | ||
const fastifySwagger = require('../../../index') | ||
const { openapiOption } = require('../../../examples/options') | ||
test('support $ref schema', t => { | ||
t.plan(3) | ||
const openapiOption = { | ||
openapi: {}, | ||
refResolver: { | ||
buildLocalReference: (json, baseUri, fragment, i) => { | ||
return json.$id || `def-${i}` | ||
} | ||
} | ||
} | ||
test('support $ref schema', async (t) => { | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
fastify.register(function (instance, _, done) { | ||
fastify.register(async (instance) => { | ||
instance.addSchema({ $id: 'Order', type: 'object', properties: { id: { type: 'integer' } } }) | ||
instance.post('/', { schema: { body: { $ref: 'Order#' }, response: { 200: { $ref: 'Order#' } } } }, () => {}) | ||
done() | ||
}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
await fastify.ready() | ||
const openapiObject = fastify.swagger() | ||
t.equal(typeof openapiObject, 'object') | ||
const openapiObject = fastify.swagger() | ||
t.equal(typeof openapiObject, 'object') | ||
t.match(Object.keys(openapiObject.components.schemas), ['Order']) | ||
Swagger.validate(openapiObject) | ||
.then(function (api) { | ||
t.pass('valid swagger object') | ||
}) | ||
.catch(function (err) { | ||
t.fail(err) | ||
}) | ||
await Swagger.validate(openapiObject) | ||
}) | ||
test('support nested $ref schema : simple test', async (t) => { | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
fastify.register(async (instance) => { | ||
instance.addSchema({ $id: 'OrderItem', type: 'object', properties: { id: { type: 'integer' } } }) | ||
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.equal(typeof openapiObject, 'object') | ||
const schemas = openapiObject.components.schemas | ||
t.match(Object.keys(schemas), ['OrderItem', 'ProductItem', 'Order']) | ||
// ref must be prefixed by '#/components/schemas/' | ||
t.equal(schemas.Order.properties.products.items.$ref, '#/components/schemas/OrderItem') | ||
await Swagger.validate(openapiObject) | ||
}) | ||
test('support nested $ref schema : complex case', async (t) => { | ||
const fastify = Fastify() | ||
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' } } }) | ||
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.equal(typeof openapiObject, 'object') | ||
const schemas = openapiObject.components.schemas | ||
t.match(Object.keys(schemas), ['schemaA', 'schemaB', 'schemaC', 'schemaD']) | ||
// ref must be prefixed by '#/components/schemas/' | ||
t.equal(schemas.schemaC.properties.a.items.$ref, '#/components/schemas/schemaA') | ||
t.equal(schemas.schemaD.properties.b.$ref, '#/components/schemas/schemaB') | ||
t.equal(schemas.schemaD.properties.c.$ref, '#/components/schemas/schemaC') | ||
await Swagger.validate(openapiObject) | ||
}) |
import fastify from 'fastify'; | ||
import fastifySwagger, { SwaggerOptions, FastifySwaggerInitOAuthOptions, FastifySwaggerUiConfigOptions } from '../..'; | ||
import fastifySwagger, { | ||
SwaggerOptions, | ||
FastifySwaggerInitOAuthOptions, | ||
FastifySwaggerUiConfigOptions, | ||
FastifySwaggerUiHooksOptions, | ||
} from "../.." | ||
import { minimalOpenApiV3Document } from './minimal-openapiV3-document'; | ||
@@ -15,2 +20,6 @@ | ||
}; | ||
const uiHooks: FastifySwaggerUiHooksOptions = { | ||
onRequest: (request, reply, done) => {done()}, | ||
preHandler: (request, reply, done) => {done()}, | ||
} | ||
@@ -177,1 +186,8 @@ app.register(fastifySwagger); | ||
}) | ||
app.register(fastifySwagger, { | ||
uiHooks, | ||
}) | ||
.ready((err) => { | ||
app.swagger(); | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10823932
14780