Skip to content

Commit

Permalink
fix(extensions): support multiple webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksey-a-maltsev committed Dec 15, 2022
1 parent e365124 commit e34baf4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 5 deletions.
22 changes: 18 additions & 4 deletions examples/extensions/extensions.spec.js
@@ -1,17 +1,31 @@
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: {
title: 'Example with 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);
});
});
@@ -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"
}
}
}
}
}
}
41 changes: 41 additions & 0 deletions 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
*/
File renamed without changes.
5 changes: 4 additions & 1 deletion src/specification.js
Expand Up @@ -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.
Expand Down

0 comments on commit e34baf4

Please sign in to comment.