From 382ab2936eccd331ad485dda5f43920250af8403 Mon Sep 17 00:00:00 2001 From: Daniel Kocot Date: Thu, 21 Apr 2022 12:42:42 +0200 Subject: [PATCH] feat: add methods and tests for serverVariables (#476) --- lib/models/components.js | 26 ++++++++++ package-lock.json | 88 +++++++++++++++++----------------- package.json | 2 +- test/models/components_test.js | 51 ++++++++++++++++++++ 4 files changed, 122 insertions(+), 45 deletions(-) diff --git a/lib/models/components.js b/lib/models/components.js index db5afc976..ef60825bb 100644 --- a/lib/models/components.js +++ b/lib/models/components.js @@ -10,6 +10,7 @@ const ChannelParameter = require('./channel-parameter'); const CorrelationId = require('./correlation-id'); const OperationTrait = require('./operation-trait'); const MessageTrait = require('./message-trait'); +const ServerVariable = require('./server-variable'); const MixinSpecificationExtensions = require('../mixins/specification-extensions'); @@ -219,6 +220,31 @@ class Components extends Base { messageTrait(name) { return getMapValueOfType(this._json.messageTraits, name, MessageTrait); } + + /** + * + * @returns {Object} + */ + serverVariables() { + return createMapOfType(this._json.serverVariables, ServerVariable); + } + + /** + * + * @returns {boolean} + */ + hasServerVariables() { + return !!this._json.serverVariables; + } + + /** + * + * @param {string} name - Name of the server variable. + * @returns {ServerVariable} + */ + serverVariable(name) { + return getMapValueOfType(this._json.serverVariables, name, ServerVariable); + } } module.exports = mix(Components, MixinSpecificationExtensions); diff --git a/package-lock.json b/package-lock.json index c48e75383..2c1a036dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "dependencies": { "@apidevtools/json-schema-ref-parser": "^9.0.6", - "@asyncapi/specs": "v2.14.0-2022-04-release.1", + "@asyncapi/specs": "^v2.14.0-2022-04-release.2", "@fmvilas/pseudo-yaml-ast": "^0.3.1", "ajv": "^6.10.1", "js-yaml": "^3.13.1", @@ -58,9 +58,9 @@ } }, "node_modules/@asyncapi/specs": { - "version": "2.14.0-2022-04-release.1", - "resolved": "https://registry.npmjs.org/@asyncapi/specs/-/specs-2.14.0-2022-04-release.1.tgz", - "integrity": "sha512-Xq9RVO91lLb7A5O4rN76lvNtoLxsWUnEGRwx5T+LXEfT/USkcDGLOCWK2T2PAUF8vtrJYVm31hB3xXm+sjVALQ==" + "version": "2.14.0-2022-04-release.2", + "resolved": "https://registry.npmjs.org/@asyncapi/specs/-/specs-2.14.0-2022-04-release.2.tgz", + "integrity": "sha512-yl1MYTQNyku9IPWg4vLokRSzn+/6iJrOdmwBckK32eQTCN5jUwvU2zN4aPv5GZuzngxrcoSTYHCtV7jnB8cDow= }, "node_modules/@babel/code-frame": { "version": "7.8.3", @@ -3078,6 +3078,18 @@ "node": ">=8" } }, + "node_modules/dmd/node_modules/marked": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/marked/-/marked-0.7.0.tgz", + "integrity": "sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg==", + "dev": true, + "bin": { + "marked": "bin/marked" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/dmd/node_modules/reduce-flatten": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-3.0.0.tgz", @@ -6193,15 +6205,15 @@ } }, "node_modules/marked": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/marked/-/marked-0.7.0.tgz", - "integrity": "sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/marked/-/marked-2.1.3.tgz", + "integrity": "sha512-/Q+7MGzaETqifOMWYEA7HVMaZb4XbcRfaOzcSsHZEith83KGlvaSG33u0SKu89Mj5h+T8V2hM+8O45Qc5XTgwA==", "dev": true, "bin": { "marked": "bin/marked" }, "engines": { - "node": ">=0.10.0" + "node": ">= 10" } }, "node_modules/marked-terminal": { @@ -9286,7 +9298,7 @@ }, "node_modules/npm/node_modules/lodash._baseindexof": { "version": "3.1.0", - "dev": true, + "extraneous": true, "inBundle": true, "license": "MIT" }, @@ -9302,19 +9314,19 @@ }, "node_modules/npm/node_modules/lodash._bindcallback": { "version": "3.0.1", - "dev": true, + "extraneous": true, "inBundle": true, "license": "MIT" }, "node_modules/npm/node_modules/lodash._cacheindexof": { "version": "3.0.2", - "dev": true, + "extraneous": true, "inBundle": true, "license": "MIT" }, "node_modules/npm/node_modules/lodash._createcache": { "version": "3.1.2", - "dev": true, + "extraneous": true, "inBundle": true, "license": "MIT", "dependencies": { @@ -9329,7 +9341,7 @@ }, "node_modules/npm/node_modules/lodash._getnative": { "version": "3.9.1", - "dev": true, + "extraneous": true, "inBundle": true, "license": "MIT" }, @@ -9347,7 +9359,7 @@ }, "node_modules/npm/node_modules/lodash.restparam": { "version": "3.6.1", - "dev": true, + "extraneous": true, "inBundle": true, "license": "MIT" }, @@ -13232,18 +13244,6 @@ "node": ">=8" } }, - "node_modules/semantic-release/node_modules/marked": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/marked/-/marked-2.0.3.tgz", - "integrity": "sha512-5otztIIcJfPc2qGTN8cVtOJEjNJZ0jwa46INMagrYfk0EvqtRuEHLsEe0LrFS0/q+ZRKT0+kXK7P2T1AN5lWRA==", - "dev": true, - "bin": { - "marked": "bin/marked" - }, - "engines": { - "node": ">= 8.16.2" - } - }, "node_modules/semantic-release/node_modules/p-limit": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", @@ -15320,9 +15320,9 @@ } }, "@asyncapi/specs": { - "version": "2.14.0-2022-04-release.1", - "resolved": "https://registry.npmjs.org/@asyncapi/specs/-/specs-2.14.0-2022-04-release.1.tgz", - "integrity": "sha512-Xq9RVO91lLb7A5O4rN76lvNtoLxsWUnEGRwx5T+LXEfT/USkcDGLOCWK2T2PAUF8vtrJYVm31hB3xXm+sjVALQ==" + "version": "2.14.0-2022-04-release.2", + "resolved": "https://registry.npmjs.org/@asyncapi/specs/-/specs-2.14.0-2022-04-release.2.tgz", + "integrity": "sha512-yl1MYTQNyku9IPWg4vLokRSzn+/6iJrOdmwBckK32eQTCN5jUwvU2zN4aPv5GZuzngxrcoSTYHCtV7jnB8cDow==" }, "@babel/code-frame": { "version": "7.8.3", @@ -17862,6 +17862,12 @@ "walk-back": "^4.0.0" }, "dependencies": { + "marked": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/marked/-/marked-0.7.0.tgz", + "integrity": "sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg==", + "dev": true + }, "reduce-flatten": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-3.0.0.tgz", @@ -20340,9 +20346,9 @@ } }, "marked": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/marked/-/marked-0.7.0.tgz", - "integrity": "sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/marked/-/marked-2.1.3.tgz", + "integrity": "sha512-/Q+7MGzaETqifOMWYEA7HVMaZb4XbcRfaOzcSsHZEith83KGlvaSG33u0SKu89Mj5h+T8V2hM+8O45Qc5XTgwA==", "dev": true }, "marked-terminal": { @@ -22677,7 +22683,7 @@ "lodash._baseindexof": { "version": "3.1.0", "bundled": true, - "dev": true + "extraneous": true }, "lodash._baseuniq": { "version": "4.6.0", @@ -22691,17 +22697,17 @@ "lodash._bindcallback": { "version": "3.0.1", "bundled": true, - "dev": true + "extraneous": true }, "lodash._cacheindexof": { "version": "3.0.2", "bundled": true, - "dev": true + "extraneous": true }, "lodash._createcache": { "version": "3.1.2", "bundled": true, - "dev": true, + "extraneous": true, "requires": { "lodash._getnative": "^3.0.0" } @@ -22714,7 +22720,7 @@ "lodash._getnative": { "version": "3.9.1", "bundled": true, - "dev": true + "extraneous": true }, "lodash._root": { "version": "3.0.1", @@ -22729,7 +22735,7 @@ "lodash.restparam": { "version": "3.6.1", "bundled": true, - "dev": true + "extraneous": true }, "lodash.union": { "version": "4.6.0", @@ -25756,12 +25762,6 @@ "p-locate": "^4.1.0" } }, - "marked": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/marked/-/marked-2.0.3.tgz", - "integrity": "sha512-5otztIIcJfPc2qGTN8cVtOJEjNJZ0jwa46INMagrYfk0EvqtRuEHLsEe0LrFS0/q+ZRKT0+kXK7P2T1AN5lWRA==", - "dev": true - }, "p-limit": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", diff --git a/package.json b/package.json index ed8a16132..11b7fa3a5 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ }, "dependencies": { "@apidevtools/json-schema-ref-parser": "^9.0.6", - "@asyncapi/specs": "v2.14.0-2022-04-release.1", + "@asyncapi/specs": "^v2.14.0-2022-04-release.2", "@fmvilas/pseudo-yaml-ast": "^0.3.1", "ajv": "^6.10.1", "js-yaml": "^3.13.1", diff --git a/test/models/components_test.js b/test/models/components_test.js index 5bdb59b15..5c1cbdede 100644 --- a/test/models/components_test.js +++ b/test/models/components_test.js @@ -469,4 +469,55 @@ describe('Components', function() { assertMixinSpecificationExtensionsInheritance(Components); }); }); + + describe('#serverVariables()', function() { + it('should return a map of ServerVariable objects', function() { + const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } }; + const d = new Components(doc); + expect(typeof d.serverVariables()).to.be.equal('object'); + expect(d.serverVariables().test1.constructor.name).to.equal('ServerVariable'); + expect(d.serverVariables().test1.json()).to.equal(doc.serverVariables.test1); + expect(d.serverVariables().test2.constructor.name).to.equal('ServerVariable'); + expect(d.serverVariables().test2.json()).to.equal(doc.serverVariables.test2); + }); + + it('should return an empty object if the components field has no defined serverVariables', function() { + const doc = {}; + const d = new Components(doc); + expect(typeof d.serverVariables()).to.be.equal('object'); + expect(d.serverVariables()).to.deep.equal({}); + }); + }); + + describe('#hasServerVariables()', function() { + it('should return a boolean indicating if the components field has serverVariables', function() { + const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } }; + const docNoSchemas = { servers: {} }; + const d = new Components(doc); + const d2 = new Components(docNoSchemas); + expect(d.hasServerVariables()).to.equal(true); + expect(d2.hasServerVariables()).to.equal(false); + }); + }); + + describe('#serverVariable()', function() { + it('should return a specific serverVariable object', function() { + const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } }; + const d = new Components(doc); + expect(d.serverVariable('test1').constructor.name).to.equal('ServerVariable'); + expect(d.serverVariable('test1').json()).to.equal(doc.serverVariables.test1); + }); + + it('should return null if a serverVariable name is not provided', function() { + const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } }; + const d = new Components(doc); + expect(d.serverVariable()).to.equal(null); + }); + + it('should return null if a serverVariable name is not found', function() { + const doc = { serverVariables: { test1: {test: 'test1'}, test2: {test: 'test2'} } }; + const d = new Components(doc); + expect(d.serverVariable('not found')).to.equal(null); + }); + }); });