Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add security field at the operation level #505

Merged
merged 4 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
* [.extension(key)](#module_@asyncapi/parser+OAuthFlow+extension) ⇒ <code>any</code>
* [.hasExt(key)](#module_@asyncapi/parser+OAuthFlow+hasExt) ⇒ <code>boolean</code>
* [.ext(key)](#module_@asyncapi/parser+OAuthFlow+ext) ⇒ <code>any</code>
* [.OperationSecurityRequirement](#module_@asyncapi/parser+OperationSecurityRequirement) ⇐ <code>Base</code>
* [.OperationTrait](#module_@asyncapi/parser+OperationTrait) ⇐ <code>OperationTraitable</code>
* [.OperationTraitable](#module_@asyncapi/parser+OperationTraitable) ⇐ <code>Base</code>
* [.id()](#module_@asyncapi/parser+OperationTraitable+id) ⇒ <code>string</code>
Expand Down Expand Up @@ -265,6 +266,7 @@
* [.hasTraits()](#module_@asyncapi/parser+Operation+hasTraits) ⇒ <code>boolean</code>
* [.messages()](#module_@asyncapi/parser+Operation+messages) ⇒ <code>Array.&lt;Message&gt;</code>
* [.message()](#module_@asyncapi/parser+Operation+message) ⇒ <code>Message</code>
* [.security()](#module_@asyncapi/parser+Operation+security) ⇒ <code>Array.&lt;OperationSecurityRequirement&gt;</code>
* [.PublishOperation](#module_@asyncapi/parser+PublishOperation) ⇐ <code>Operation</code>
* [.isPublish()](#module_@asyncapi/parser+PublishOperation+isPublish) ⇒ <code>boolean</code>
* [.isSubscribe()](#module_@asyncapi/parser+PublishOperation+isSubscribe) ⇒ <code>boolean</code>
Expand Down Expand Up @@ -1912,6 +1914,13 @@ Implements functions to deal with a OAuthFlow object.
| --- | --- | --- |
| key | <code>string</code> | Extension key. |

<a name="module_@asyncapi/parser+OperationSecurityRequirement"></a>

### @asyncapi/parser.OperationSecurityRequirement ⇐ <code>Base</code>
Implements functions to deal with a OperationSecurityRequirement object.

**Kind**: instance class of [<code>@asyncapi/parser</code>](#module_@asyncapi/parser)
**Extends**: <code>Base</code>
<a name="module_@asyncapi/parser+OperationTrait"></a>

### @asyncapi/parser.OperationTrait ⇐ <code>OperationTraitable</code>
Expand Down Expand Up @@ -2126,6 +2135,7 @@ Implements functions to deal with an Operation object.
* [.hasTraits()](#module_@asyncapi/parser+Operation+hasTraits) ⇒ <code>boolean</code>
* [.messages()](#module_@asyncapi/parser+Operation+messages) ⇒ <code>Array.&lt;Message&gt;</code>
* [.message()](#module_@asyncapi/parser+Operation+message) ⇒ <code>Message</code>
* [.security()](#module_@asyncapi/parser+Operation+security) ⇒ <code>Array.&lt;OperationSecurityRequirement&gt;</code>

<a name="module_@asyncapi/parser+Operation+hasMultipleMessages"></a>

Expand All @@ -2147,6 +2157,10 @@ Implements functions to deal with an Operation object.

#### operation.message() ⇒ <code>Message</code>
**Kind**: instance method of [<code>Operation</code>](#module_@asyncapi/parser+Operation)
<a name="module_@asyncapi/parser+Operation+security"></a>

#### operation.security() ⇒ <code>Array.&lt;OperationSecurityRequirement&gt;</code>
**Kind**: instance method of [<code>Operation</code>](#module_@asyncapi/parser+Operation)
<a name="module_@asyncapi/parser+PublishOperation"></a>

### @asyncapi/parser.PublishOperation ⇐ <code>Operation</code>
Expand Down
13 changes: 13 additions & 0 deletions lib/models/operation-security-requirement.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions lib/models/operation.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
14 changes: 13 additions & 1 deletion test/models/operation_test.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to test that the array is not empty. Otherwise, in the case it is empty, the logic inside the following forEach won't ever be executed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smoya Thanks for the review! I have incorporated the suggestion.

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]);
});
});
});
});
6 changes: 6 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down