From 8be4a0d69b13df83af7e9c8629e63158325be72b Mon Sep 17 00:00:00 2001 From: bun <73948280+bun913@users.noreply.github.com> Date: Thu, 5 Oct 2023 16:08:30 +0900 Subject: [PATCH] feat: support for webhooks option according to Open Api 3.1.0 (#760) --- examples/options.js | 75 +++++++++++++++++++++++++++++++++++++ lib/spec/openapi/utils.js | 1 + test/spec/openapi/option.js | 72 ++++++++++++++++++++++++++++++++++- 3 files changed, 146 insertions(+), 2 deletions(-) diff --git a/examples/options.js b/examples/options.js index 06dba5ce..f10bdabe 100644 --- a/examples/options.js +++ b/examples/options.js @@ -63,6 +63,80 @@ const openapiOption = { } } +const openapiWebHookOption = { + openapi: { + openapi: '3.1.0', + info: { + title: 'Test swagger', + description: 'testing the fastify swagger api', + version: '0.1.0' + }, + servers: [ + { + url: 'http://localhost' + } + ], + tags: [{ name: 'tag' }], + components: { + securitySchemes: { + apiKey: { + type: 'apiKey', + name: 'apiKey', + in: 'header' + } + }, + schemas: { + Pet: { + require: ['id', 'name'], + properties: { + id: { + type: 'integer', + format: 'int64' + }, + name: { + type: 'string' + }, + tag: { + type: 'string' + } + } + } + } + }, + security: [ + { + apiKey: [] + } + ], + externalDocs: { + description: 'Find more info here', + url: 'https://swagger.io' + }, + webhooks: { + newPet: { + post: { + requestBody: { + description: 'Information about a new pet in the system', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Pet' + } + } + } + }, + responses: { + 200: { + description: + 'Return a 200 status to indicate that the data was received successfully' + } + } + } + } + } + } +} + const openapiRelativeOptions = { openapi: { info: { @@ -287,6 +361,7 @@ const schemaOperationId = { module.exports = { openapiOption, openapiRelativeOptions, + openapiWebHookOption, swaggerOption, schemaQuerystring, schemaBody, diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index 30c2d55e..a6b8a762 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -60,6 +60,7 @@ function prepareOpenapiObject (opts) { if (opts.info) openapiObject.info = opts.info if (opts.servers) openapiObject.servers = opts.servers if (opts.components) openapiObject.components = Object.assign({}, opts.components, { schemas: Object.assign({}, opts.components.schemas) }) + if (opts.webhooks) openapiObject.webhooks = opts.webhooks if (opts.security) openapiObject.security = opts.security if (opts.tags) openapiObject.tags = opts.tags if (opts.externalDocs) openapiObject.externalDocs = opts.externalDocs diff --git a/test/spec/openapi/option.js b/test/spec/openapi/option.js index d870c52a..b9a6357e 100644 --- a/test/spec/openapi/option.js +++ b/test/spec/openapi/option.js @@ -6,7 +6,7 @@ const Swagger = require('@apidevtools/swagger-parser') const yaml = require('yaml') const fastifySwagger = require('../../../index') const { readPackageJson } = require('../../../lib/util/read-package-json') -const { openapiOption } = require('../../../examples/options') +const { openapiOption, openapiWebHookOption } = require('../../../examples/options') test('openapi should have default version', async (t) => { t.plan(1) @@ -936,7 +936,6 @@ test('uses examples if has property required in body', async (t) => { fastify.post('/', opts, () => {}) await fastify.ready() - const openapiObject = fastify.swagger() const schema = openapiObject.paths['/'].post.requestBody.content['application/json'].schema const requestBody = openapiObject.paths['/'].post.requestBody @@ -947,4 +946,73 @@ test('uses examples if has property required in body', async (t) => { t.same(requestBody.required, true) }) +test('openapi webhooks properties', async (t) => { + t.plan(1) + const fastify = Fastify() + + await fastify.register(fastifySwagger, openapiWebHookOption) + + const opts = { + schema: { + body: { + type: 'object', + properties: { + hello: { type: 'string' }, + obj: { + type: 'object', + properties: { + some: { type: 'string' } + } + } + } + } + }, + webhooks: { + newPet: { + post: { + requestBody: { + description: 'Information about a new pet in the system', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Pet' + } + } + } + }, + responses: { + 200: { + description: + 'Return a 200 status to indicate that the data was received successfully' + } + } + } + } + } + } + + fastify.post('/', opts, () => {}) + + await fastify.ready() + + const openapiObject = fastify.swagger() + t.equal(openapiObject.webhooks, openapiWebHookOption.openapi.webhooks) +}) + +test('webhooks options for openapi 3.1.0 must valid format', async (t) => { + t.plan(2) + const fastify = Fastify() + + await fastify.register(fastifySwagger, openapiWebHookOption) + + await fastify.ready() + + fastify.swagger() + const openapiObject = fastify.swagger() + t.equal(typeof openapiObject, 'object') + + await Swagger.validate(openapiObject) + t.pass('valid swagger object') +}) + module.exports = { openapiOption }