From e34baf44e393edae949207f8ab5a2ee87c9b6784 Mon Sep 17 00:00:00 2001 From: Aleksey Maltsev Date: Thu, 15 Dec 2022 17:14:36 +0100 Subject: [PATCH] fix(extensions): support multiple webhooks --- examples/extensions/extensions.spec.js | 22 +++++++-- ...ooks-multiple-reference-specification.json | 48 +++++++++++++++++++ examples/extensions/x-webhooks-multiple.js | 41 ++++++++++++++++ ...hooks-single-reference-specification.json} | 0 .../{example.js => x-webhooks-single.js} | 0 src/specification.js | 5 +- 6 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 examples/extensions/x-webhooks-multiple-reference-specification.json create mode 100644 examples/extensions/x-webhooks-multiple.js rename examples/extensions/{reference-specification.json => x-webhooks-single-reference-specification.json} (100%) rename examples/extensions/{example.js => x-webhooks-single.js} (100%) diff --git a/examples/extensions/extensions.spec.js b/examples/extensions/extensions.spec.js index 4479cbae..7001a7fc 100644 --- a/examples/extensions/extensions.spec.js +++ b/examples/extensions/extensions.spec.js @@ -1,8 +1,9 @@ const swaggerJsdoc = require('../..'); -const referenceSpecification = require('./reference-specification.json'); +const webhooksSingleSpecification = require('./x-webhooks-single-reference-specification.json'); +const webhooksMultipleSpecification = require('./x-webhooks-multiple-reference-specification.json'); describe('Example for using extensions', () => { - it('should support x-webhooks', () => { + it('should support single entry in x-webhooks', () => { const result = swaggerJsdoc({ swaggerDefinition: { info: { @@ -10,8 +11,21 @@ describe('Example for using extensions', () => { version: '0.0.1', }, }, - apis: ['./examples/extensions/example.js'], + apis: ['./examples/extensions/x-webhooks-single.js'], }); - expect(result).toEqual(referenceSpecification); + expect(result).toEqual(webhooksSingleSpecification); + }); + + it('should support multiple entries in x-webhooks', () => { + const result = swaggerJsdoc({ + swaggerDefinition: { + info: { + title: 'Example with extensions', + version: '0.0.1', + }, + }, + apis: ['./examples/extensions/x-webhooks-multiple.js'], + }); + expect(result).toEqual(webhooksMultipleSpecification); }); }); diff --git a/examples/extensions/x-webhooks-multiple-reference-specification.json b/examples/extensions/x-webhooks-multiple-reference-specification.json new file mode 100644 index 00000000..6f7d73d5 --- /dev/null +++ b/examples/extensions/x-webhooks-multiple-reference-specification.json @@ -0,0 +1,48 @@ +{ + "info": { "title": "Example with extensions", "version": "0.0.1" }, + "swagger": "2.0", + "paths": {}, + "definitions": {}, + "responses": {}, + "parameters": {}, + "securityDefinitions": {}, + "tags": [], + "x-webhooks": { + "newCat": { + "post": { + "description": "Information about a new cat in the systems", + "tags": ["pet"], + "requestBody": { + "content": { + "application/json": { + "schema": { "$ref": "#/components/schemas/Cat" } + } + } + }, + "responses": { + "200": { + "description": "Return a 200 status to indicate that the data was received successfully" + } + } + } + }, + "newDog": { + "post": { + "description": "Information about a new dog in the systems", + "tags": ["pet"], + "requestBody": { + "content": { + "application/json": { + "schema": { "$ref": "#/components/schemas/Dog" } + } + } + }, + "responses": { + "200": { + "description": "Return a 200 status to indicate that the data was received successfully" + } + } + } + } + } +} diff --git a/examples/extensions/x-webhooks-multiple.js b/examples/extensions/x-webhooks-multiple.js new file mode 100644 index 00000000..eacf9b33 --- /dev/null +++ b/examples/extensions/x-webhooks-multiple.js @@ -0,0 +1,41 @@ +/* istanbul ignore file */ + +/** + * Example of cat + * + * @swagger + * x-webhooks: + * newCat: + * post: + * description: Information about a new cat in the systems + * tags: + * - pet + * requestBody: + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/Cat" + * responses: + * "200": + * description: Return a 200 status to indicate that the data was received successfully + */ + +/** + * Example of dog + * + * @swagger + * x-webhooks: + * newDog: + * post: + * description: Information about a new dog in the systems + * tags: + * - pet + * requestBody: + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/Dog" + * responses: + * "200": + * description: Return a 200 status to indicate that the data was received successfully + */ diff --git a/examples/extensions/reference-specification.json b/examples/extensions/x-webhooks-single-reference-specification.json similarity index 100% rename from examples/extensions/reference-specification.json rename to examples/extensions/x-webhooks-single-reference-specification.json diff --git a/examples/extensions/example.js b/examples/extensions/x-webhooks-single.js similarity index 100% rename from examples/extensions/example.js rename to examples/extensions/x-webhooks-single.js diff --git a/src/specification.js b/src/specification.js index bf0dedb1..48dee79b 100644 --- a/src/specification.js +++ b/src/specification.js @@ -128,7 +128,10 @@ function organize(swaggerObject, annotation, property) { // Root property on purpose. // @see https://github.com/OAI/OpenAPI-Specification/blob/master/proposals/002_Webhooks.md#proposed-solution if (property === 'x-webhooks') { - swaggerObject[property] = annotation[property]; + swaggerObject[property] = mergeDeep( + swaggerObject[property], + annotation[property] + ) } // Other extensions can be in varying places depending on different vendors and opinions.