From 20b9017e6dd7f379e19922eb29b26f6250e712c0 Mon Sep 17 00:00:00 2001
From: Lokesh Rishi <4687963+LokeshRishi@users.noreply.github.com>
Date: Thu, 14 Apr 2022 03:15:52 -0700
Subject: [PATCH] feat: add security field at the operation level (#505)
---
API.md | 14 ++++++++++++++
lib/models/operation-security-requirement.js | 13 +++++++++++++
lib/models/operation.js | 9 +++++++++
test/models/operation_test.js | 14 +++++++++++++-
types.d.ts | 6 ++++++
5 files changed, 55 insertions(+), 1 deletion(-)
create mode 100644 lib/models/operation-security-requirement.js
diff --git a/API.md b/API.md
index 2e6f7d19d..639a19414 100644
--- a/API.md
+++ b/API.md
@@ -233,6 +233,7 @@
* [.extension(key)](#module_@asyncapi/parser+OAuthFlow+extension) ⇒ any
* [.hasExt(key)](#module_@asyncapi/parser+OAuthFlow+hasExt) ⇒ boolean
* [.ext(key)](#module_@asyncapi/parser+OAuthFlow+ext) ⇒ any
+ * [.OperationSecurityRequirement](#module_@asyncapi/parser+OperationSecurityRequirement) ⇐ Base
* [.OperationTrait](#module_@asyncapi/parser+OperationTrait) ⇐ OperationTraitable
* [.OperationTraitable](#module_@asyncapi/parser+OperationTraitable) ⇐ Base
* [.id()](#module_@asyncapi/parser+OperationTraitable+id) ⇒ string
@@ -265,6 +266,7 @@
* [.hasTraits()](#module_@asyncapi/parser+Operation+hasTraits) ⇒ boolean
* [.messages()](#module_@asyncapi/parser+Operation+messages) ⇒ Array.<Message>
* [.message()](#module_@asyncapi/parser+Operation+message) ⇒ Message
+ * [.security()](#module_@asyncapi/parser+Operation+security) ⇒ Array.<OperationSecurityRequirement>
* [.PublishOperation](#module_@asyncapi/parser+PublishOperation) ⇐ Operation
* [.isPublish()](#module_@asyncapi/parser+PublishOperation+isPublish) ⇒ boolean
* [.isSubscribe()](#module_@asyncapi/parser+PublishOperation+isSubscribe) ⇒ boolean
@@ -1912,6 +1914,13 @@ Implements functions to deal with a OAuthFlow object.
| --- | --- | --- |
| key | string
| Extension key. |
+
+
+### @asyncapi/parser.OperationSecurityRequirement ⇐ Base
+Implements functions to deal with a OperationSecurityRequirement object.
+
+**Kind**: instance class of [@asyncapi/parser
](#module_@asyncapi/parser)
+**Extends**: Base
### @asyncapi/parser.OperationTrait ⇐ OperationTraitable
@@ -2126,6 +2135,7 @@ Implements functions to deal with an Operation object.
* [.hasTraits()](#module_@asyncapi/parser+Operation+hasTraits) ⇒ boolean
* [.messages()](#module_@asyncapi/parser+Operation+messages) ⇒ Array.<Message>
* [.message()](#module_@asyncapi/parser+Operation+message) ⇒ Message
+ * [.security()](#module_@asyncapi/parser+Operation+security) ⇒ Array.<OperationSecurityRequirement>
@@ -2147,6 +2157,10 @@ Implements functions to deal with an Operation object.
#### operation.message() ⇒ Message
**Kind**: instance method of [Operation
](#module_@asyncapi/parser+Operation)
+
+
+#### operation.security() ⇒ Array.<OperationSecurityRequirement>
+**Kind**: instance method of [Operation
](#module_@asyncapi/parser+Operation)
### @asyncapi/parser.PublishOperation ⇐ Operation
diff --git a/lib/models/operation-security-requirement.js b/lib/models/operation-security-requirement.js
new file mode 100644
index 000000000..e9cb89669
--- /dev/null
+++ b/lib/models/operation-security-requirement.js
@@ -0,0 +1,13 @@
+const Base = require('./base');
+
+/**
+ * Implements functions to deal with a OperationSecurityRequirement object.
+ * @class
+ * @alias module:@asyncapi/parser#OperationSecurityRequirement
+ * @extends Base
+ * @returns {OperationSecurityRequirement}
+ */
+class OperationSecurityRequirement extends Base {
+}
+
+module.exports = OperationSecurityRequirement;
\ No newline at end of file
diff --git a/lib/models/operation.js b/lib/models/operation.js
index 58f479ca0..92b6e94dd 100644
--- a/lib/models/operation.js
+++ b/lib/models/operation.js
@@ -1,6 +1,7 @@
const OperationTraitable = require('./operation-traitable');
const Message = require('./message');
const OperationTrait = require('./operation-trait');
+const OperationSecurityRequirement = require('./operation-security-requirement');
/**
* Implements functions to deal with an Operation object.
@@ -54,6 +55,14 @@ class Operation extends OperationTraitable {
if (index > this._json.message.oneOf.length - 1) return null;
return new Message(this._json.message.oneOf[+index]);
}
+
+ /**
+ * @returns {OperationSecurityRequirement[]}
+ */
+ security() {
+ if (!this._json.security) return null;
+ return this._json.security.map(sec => new OperationSecurityRequirement(sec));
+ }
}
module.exports = Operation;
diff --git a/test/models/operation_test.js b/test/models/operation_test.js
index dd80c5ccd..6b7bc23e8 100644
--- a/test/models/operation_test.js
+++ b/test/models/operation_test.js
@@ -1,5 +1,5 @@
const { expect } = require('chai');
-const js = { summary: 't', description: 'test', traits: [{bindings: {kafka: {clientId: 'my-app-id'}}}], operationId: 'test', tags: [{name: 'tag1'}], externalDocs: { url: 'somewhere' }, bindings: { amqp: { test: true } }, message: { test: true }, 'x-test': 'testing' };
+const js = { summary: 't', description: 'test', traits: [{bindings: {kafka: {clientId: 'my-app-id'}}}], operationId: 'test', tags: [{name: 'tag1'}], externalDocs: { url: 'somewhere' }, bindings: { amqp: { test: true } }, message: { test: true }, 'x-test': 'testing', security: [{ oauth2: ['user:read'] }]};
const Operation = require('../../lib/models/operation');
@@ -122,4 +122,16 @@ describe('Operation', function() {
assertMixinSpecificationExtensionsInheritance(Operation);
});
});
+
+ describe('#security()', function() {
+ it('should return an array of security requirements objects', function() {
+ const d = new Operation(js);
+ expect(Array.isArray(d.security())).to.equal(true);
+ expect(d.security()).to.have.lengthOf(1);
+ d.security().forEach((s, i) => {
+ expect(s.constructor.name).to.equal('OperationSecurityRequirement');
+ expect(s.json()).to.equal(js.security[i]);
+ });
+ });
+ });
});
diff --git a/types.d.ts b/types.d.ts
index 1bb7a936a..efde75185 100644
--- a/types.d.ts
+++ b/types.d.ts
@@ -839,6 +839,12 @@ declare module "@asyncapi/parser" {
hasTraits(): boolean;
messages(): Message[];
message(): Message;
+ security(): OperationSecurityRequirement[];
+ }
+ /**
+ * Implements functions to deal with a OperationSecurityRequirement object.
+ */
+ class OperationSecurityRequirement extends Base {
}
/**
* Implements functions to deal with a PublishOperation object.