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 1 commit
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
6 changes: 6 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,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>
* [.OperationSecurityRequirement](#module_@asyncapi/parser+OperationSecurityRequirement) ⇐ <code>Base</code>
Copy link
Member

Choose a reason for hiding this comment

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

🤔 where is this defined?

Copy link
Member

Choose a reason for hiding this comment

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

This doc should be generated automatically iirc.
Indeed OperationSecurityRequirementis not the method you declared, but Security.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @fmvilas and @smoya
If its generated automatically, would you suggest reverting my changes?

Copy link
Member

Choose a reason for hiding this comment

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

Would you mind trying npm run docs? This command should build the right API doc.

Copy link
Member

Choose a reason for hiding this comment

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

The whole PR is ok, just delete the above and I can accept :)

Copy link
Author

Choose a reason for hiding this comment

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

@smoya @magicmatatjahu I deleted my changes to the API doc and regenerated it using the above command.

* [.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 @@ -2126,6 +2127,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 +2149,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;
13 changes: 12 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,15 @@ 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.

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