Skip to content

Commit acb9bf1

Browse files
authored
fix: dedupe security schemes when plucking them out of an oas (#222)
1 parent 01ed2b7 commit acb9bf1

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

packages/tooling/__tests__/__fixtures__/multiple-securities.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@
171171
}
172172
}
173173
},
174+
"/multiple-combo-auths-duped": {
175+
"get": {
176+
"responses": {
177+
"200": {
178+
"description": "OK"
179+
},
180+
"400": {
181+
"description": "Bad Request"
182+
}
183+
},
184+
"security": [
185+
{
186+
"apiKeyScheme": [],
187+
"httpBearer": []
188+
},
189+
{
190+
"apiKeyScheme": [],
191+
"apiKeySignature": []
192+
}
193+
]
194+
}
195+
},
174196
"/unknown-auth-type": {
175197
"post": {
176198
"operationId": "unknownAuthType",
@@ -225,11 +247,20 @@
225247
}
226248
}
227249
},
250+
"httpBearer": {
251+
"type": "http",
252+
"scheme": "bearer"
253+
},
228254
"apiKeyScheme": {
229255
"type": "apiKey",
230256
"name": "testKey",
231257
"in": "header"
232258
},
259+
"apiKeySignature": {
260+
"type": "apiKey",
261+
"name": "X-AUTH-SIGNATURE",
262+
"in": "header"
263+
},
233264
"unknownAuthType": {
234265
"type": "demigorgon",
235266
"name": "eleven",

packages/tooling/__tests__/operation.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ describe('#prepareSecurity()', () => {
236236
expect(operation.prepareSecurity().Header).toHaveLength(1);
237237
});
238238

239+
it('should dedupe securities in within an && and || situation', () => {
240+
const operation = new Oas(multipleSecurities).operation('/multiple-combo-auths-duped', 'get');
241+
242+
expect(operation.prepareSecurity().Bearer).toHaveLength(1);
243+
expect(operation.prepareSecurity().Header).toHaveLength(2);
244+
});
245+
239246
it.todo('should set a `key` property');
240247

241248
it.todo('should throw if attempting to use a non-existent scheme');

packages/tooling/src/operation.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-underscore-dangle */
12
const findSchemaDefinition = require('./lib/find-schema-definition');
23

34
class Operation {
@@ -65,7 +66,6 @@ class Operation {
6566
return false;
6667
}
6768

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

7171
return { type, security };
@@ -76,7 +76,12 @@ class Operation {
7676
// Remove non-existent schemes
7777
if (!security) return;
7878
if (!prev[security.type]) prev[security.type] = [];
79-
prev[security.type].push(security.security);
79+
80+
// Only add schemes we haven't seen yet.
81+
const exists = prev[security.type].findIndex(sec => sec._key === security.security._key);
82+
if (exists < 0) {
83+
prev[security.type].push(security.security);
84+
}
8085
});
8186
return prev;
8287
}, {});

0 commit comments

Comments
 (0)