Skip to content

Commit

Permalink
fix: dedupe security schemes when plucking them out of an oas (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Jul 14, 2020
1 parent 01ed2b7 commit acb9bf1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/tooling/__tests__/__fixtures__/multiple-securities.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@
}
}
},
"/multiple-combo-auths-duped": {
"get": {
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Bad Request"
}
},
"security": [
{
"apiKeyScheme": [],
"httpBearer": []
},
{
"apiKeyScheme": [],
"apiKeySignature": []
}
]
}
},
"/unknown-auth-type": {
"post": {
"operationId": "unknownAuthType",
Expand Down Expand Up @@ -225,11 +247,20 @@
}
}
},
"httpBearer": {
"type": "http",
"scheme": "bearer"
},
"apiKeyScheme": {
"type": "apiKey",
"name": "testKey",
"in": "header"
},
"apiKeySignature": {
"type": "apiKey",
"name": "X-AUTH-SIGNATURE",
"in": "header"
},
"unknownAuthType": {
"type": "demigorgon",
"name": "eleven",
Expand Down
7 changes: 7 additions & 0 deletions packages/tooling/__tests__/operation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ describe('#prepareSecurity()', () => {
expect(operation.prepareSecurity().Header).toHaveLength(1);
});

it('should dedupe securities in within an && and || situation', () => {
const operation = new Oas(multipleSecurities).operation('/multiple-combo-auths-duped', 'get');

expect(operation.prepareSecurity().Bearer).toHaveLength(1);
expect(operation.prepareSecurity().Header).toHaveLength(2);
});

it.todo('should set a `key` property');

it.todo('should throw if attempting to use a non-existent scheme');
Expand Down
9 changes: 7 additions & 2 deletions packages/tooling/src/operation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
const findSchemaDefinition = require('./lib/find-schema-definition');

class Operation {
Expand Down Expand Up @@ -65,7 +66,6 @@ class Operation {
return false;
}

// eslint-disable-next-line no-underscore-dangle
security._key = key;

return { type, security };
Expand All @@ -76,7 +76,12 @@ class Operation {
// Remove non-existent schemes
if (!security) return;
if (!prev[security.type]) prev[security.type] = [];
prev[security.type].push(security.security);

// Only add schemes we haven't seen yet.
const exists = prev[security.type].findIndex(sec => sec._key === security.security._key);
if (exists < 0) {
prev[security.type].push(security.security);
}
});
return prev;
}, {});
Expand Down

0 comments on commit acb9bf1

Please sign in to comment.