Skip to content

Commit 74f5f08

Browse files
Merge pull request #82 from advanced-rest-client/feat/W-15520234/security
feat(W-15520234): add security method
2 parents 87cf5e3 + 1f10e54 commit 74f5f08

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

src/ApiMethodDocumentation.js

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,43 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
403403
this._processServerInfo();
404404
}
405405

406+
get asyncSecurityServers(){
407+
if(!this.endpoint){
408+
return []
409+
}
410+
const endpoint = Array.isArray(this.endpoint) ? this.endpoint[0]: this.endpoint
411+
const apiContractServerKey = this._getAmfKey( this.ns.aml.vocabularies.apiContract.server)
412+
const endpointServers = this._ensureArray(endpoint[apiContractServerKey])
413+
const apiSecurityKey = this._getAmfKey( this.ns.aml.vocabularies.security.security)
414+
415+
// try to find servers in channel level
416+
if(endpointServers){
417+
return endpointServers.map((server)=>server[apiSecurityKey] || null).filter(elem=>elem!==null).flat();
418+
}
419+
420+
// try to find root server (only one) that is received by property
421+
if(this.server){
422+
return this._ensureArray(this.server[apiSecurityKey]) || []
423+
}
424+
425+
// in case that async api doesn't have servers
426+
return []
427+
}
428+
429+
/**
430+
* Filters the methodSecurity array to remove elements that have the same '@id' as the elements in the serversSecurity array.
431+
*
432+
* @param {object[]} methodSecurity - The array of method security objects.
433+
* @param {object[]} serversSecurity - The array of server security objects.
434+
* @returns {object[]} The filtered methodSecurity array with unique elements based on the '@id' key.
435+
*/
436+
_computeAsyncSecurityMethod(methodSecurity,serversSecurity){
437+
if(!Array.isArray(methodSecurity) || !Array.isArray(serversSecurity)){
438+
return []
439+
}
440+
return methodSecurity.filter(method => !serversSecurity.some(server=>server['@id']===method['@id']));
441+
}
442+
406443
_processMethodChange() {
407444
this.__methodProcessingDebouncer = false;
408445
const { method } = this;
@@ -411,10 +448,14 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
411448
this.hasCustomProperties = this._computeHasCustomProperties(method);
412449
this.expects = this._computeExpects(method);
413450
this.returns = this._computeReturns(method);
451+
414452
if (this._isAsyncAPI(this.amf)) {
415453
this._overwriteExpects();
454+
this._computeAsyncApiSecurity()
455+
}else{
456+
this.security = this._computeSecurity(method) || this._computeSecurity(this.server);
416457
}
417-
this.security = this._computeSecurity(method) || this._computeSecurity(this.server);
458+
418459
const extendsTypes = this._computeExtends(method);
419460
this.extendsTypes = extendsTypes;
420461
this.traits = this._computeTraits(extendsTypes);
@@ -424,6 +465,19 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
424465
this.deprecated = this._computeIsDeprecated(method);
425466
}
426467

468+
_computeAsyncApiSecurity(){
469+
const { method } = this;
470+
// find security from all servers for this endpoint
471+
this.serversSecurity = this.asyncSecurityServers;
472+
473+
// security that is defined by operation
474+
const methodSecurity = this._computeSecurity(method);
475+
476+
// method security that is defined by operation and is not defined by servers
477+
this.methodSecurity = this._computeAsyncSecurityMethod(methodSecurity,this.serversSecurity);
478+
this.security = this.methodSecurity.length > 0 ? this.methodSecurity : this.serversSecurity;
479+
}
480+
427481
_computeIsDeprecated(method) {
428482
return Boolean(this._getValue(method, this._getAmfKey(this.ns.aml.vocabularies.core.deprecated)));
429483
}
@@ -923,7 +977,12 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
923977
}
924978

925979
_getSecurityTemplate() {
926-
const { renderSecurity, security } = this;
980+
const { renderSecurity, serversSecurity, security: securityNoAsync } = this;
981+
let security = securityNoAsync
982+
if(this._isAsyncAPI(this.amf)){
983+
// when is async api shows security that is defined by servers
984+
security = serversSecurity
985+
}
927986
if (!renderSecurity || !security || !security.length) {
928987
return '';
929988
}
@@ -1033,6 +1092,7 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
10331092

10341093
_getRequestTemplate() {
10351094
return html`<section class="request-documentation">
1095+
${this._getAsyncSecurityMethodTemplate()}
10361096
${this._getMessagesTemplate()}
10371097
${this._getCodeSnippetsTemplate()}
10381098
${this._getSecurityTemplate()}
@@ -1043,6 +1103,27 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
10431103
</section>`
10441104
}
10451105

1106+
_getAsyncSecurityMethodTemplate() {
1107+
const { renderSecurity, methodSecurity } = this;
1108+
if (!renderSecurity || !methodSecurity || !methodSecurity.length || !this._isAsyncAPI(this.amf)) {
1109+
return '';
1110+
}
1111+
const { compatibility, amf, narrow } = this;
1112+
return html`<section class="async-method-security">
1113+
<div
1114+
class="section-title-area"
1115+
>
1116+
<div class="heading3 table-title" role="heading" aria-level="2">Additional security requirements</div>
1117+
1118+
</div>
1119+
${methodSecurity.map((item) => html`<api-security-documentation
1120+
.amf="${amf}"
1121+
.security="${item}"
1122+
?narrow="${narrow}"
1123+
?compatibility="${compatibility}"></api-security-documentation>`)}
1124+
</section>`;
1125+
}
1126+
10461127
_getMessagesTemplate() {
10471128
const { message } = this;
10481129
if (!message || !Array.isArray(message) || message.length <= 1) {

src/Styles.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,8 @@ api-security-documentation:last-of-type {
313313
padding: 0px 8px 0px 8px;
314314
margin-right: 10px;
315315
}
316+
317+
.async-method-security{
318+
margin-top: 17px;
319+
}
316320
`;

test/api-method-documentation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ describe('<api-method-documentation>', () => {
817817
});
818818

819819
it('should set security when security is defined in server node', () => {
820-
assert.lengthOf(element.security, 2);
820+
assert.lengthOf(element.serversSecurity, 2);
821821
});
822822

823823
it('should render Headers section for AsyncAPI headers', async () => {

0 commit comments

Comments
 (0)