Skip to content

Commit

Permalink
feat: support for webhooks option according to Open Api 3.1.0 (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
bun913 authored Oct 5, 2023
1 parent ff02608 commit 8be4a0d
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
75 changes: 75 additions & 0 deletions examples/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -287,6 +361,7 @@ const schemaOperationId = {
module.exports = {
openapiOption,
openapiRelativeOptions,
openapiWebHookOption,
swaggerOption,
schemaQuerystring,
schemaBody,
Expand Down
1 change: 1 addition & 0 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 70 additions & 2 deletions test/spec/openapi/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 }

0 comments on commit 8be4a0d

Please sign in to comment.