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

fix: dedupe security schemes when plucking them out of an oas #222

Merged
merged 1 commit into from
Jul 14, 2020
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
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