Skip to content

Commit 2e36dcd

Browse files
dracomithrilmichalgrezel
authored andcommitted
reduce duplicates
1 parent 8c43c02 commit 2e36dcd

File tree

5 files changed

+134
-12
lines changed

5 files changed

+134
-12
lines changed

packages/openapi-ts/src/openApi/2.0.x/parser/operation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ const operationToIrOperation = ({
261261
}
262262

263263
if (operation.security) {
264-
const securitySchemeObjects: Array<IR.SecurityObject> = [];
264+
const securitySchemeObjects: Map<string, IR.SecurityObject> = new Map();
265265

266266
for (const securityRequirementObject of operation.security) {
267267
for (const name in securityRequirementObject) {
@@ -325,12 +325,12 @@ const operationToIrOperation = ({
325325
continue;
326326
}
327327

328-
securitySchemeObjects.push(irSecuritySchemeObject);
328+
securitySchemeObjects.set(name, irSecuritySchemeObject);
329329
}
330330
}
331331

332-
if (securitySchemeObjects.length) {
333-
irOperation.security = securitySchemeObjects;
332+
if (securitySchemeObjects.size) {
333+
irOperation.security = Array.from(securitySchemeObjects.values());
334334
}
335335
}
336336

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import type { IR } from '../../../../ir/types';
4+
import type { SecuritySchemeObject } from '../../types/spec';
5+
import { parseOperation } from '../operation';
6+
7+
type ParseOperationProps = Parameters<typeof parseOperation>[0];
8+
9+
describe('operation', () => {
10+
const context = {
11+
config: {
12+
plugins: {},
13+
},
14+
ir: {
15+
paths: {},
16+
servers: [],
17+
},
18+
} as unknown as IR.Context;
19+
20+
it('should parse operation correctly', () => {
21+
const method = 'get';
22+
const operation: ParseOperationProps['operation'] = {
23+
operationId: 'testOperation',
24+
responses: {},
25+
security: [
26+
{
27+
apiKeyAuth: [],
28+
},
29+
{
30+
apiKeyAuth: [],
31+
},
32+
],
33+
summary: 'Test Operation',
34+
};
35+
const path = '/test';
36+
const securitySchemesMap = new Map<string, SecuritySchemeObject>([
37+
['apiKeyAuth', { in: 'header', name: 'apiKey', type: 'apiKey' }],
38+
]);
39+
const state: ParseOperationProps['state'] = {
40+
ids: new Map<string, string>(),
41+
};
42+
43+
parseOperation({
44+
context,
45+
method,
46+
operation,
47+
path,
48+
securitySchemesMap,
49+
state,
50+
});
51+
52+
expect(context.ir.paths?.[path]?.[method]).toEqual({
53+
id: 'testOperation',
54+
method,
55+
operationId: 'testOperation',
56+
path,
57+
security: [{ in: 'header', name: 'apiKey', type: 'apiKey' }],
58+
summary: 'Test Operation',
59+
});
60+
});
61+
});

packages/openapi-ts/src/openApi/3.0.x/parser/operation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ const operationToIrOperation = ({
203203
}
204204

205205
if (operation.security) {
206-
const securitySchemeObjects: Array<IR.SecurityObject> = [];
206+
const securitySchemeObjects: Map<string, IR.SecurityObject> = new Map();
207207

208208
for (const securityRequirementObject of operation.security) {
209209
for (const name in securityRequirementObject) {
@@ -213,12 +213,12 @@ const operationToIrOperation = ({
213213
continue;
214214
}
215215

216-
securitySchemeObjects.push(securitySchemeObject);
216+
securitySchemeObjects.set(name, securitySchemeObject);
217217
}
218218
}
219219

220-
if (securitySchemeObjects.length) {
221-
irOperation.security = securitySchemeObjects;
220+
if (securitySchemeObjects.size) {
221+
irOperation.security = Array.from(securitySchemeObjects.values());
222222
}
223223
}
224224

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import type { IR } from '../../../../ir/types';
4+
import type { SecuritySchemeObject } from '../../types/spec';
5+
import { parseOperation } from '../operation';
6+
7+
type ParseOperationProps = Parameters<typeof parseOperation>[0];
8+
9+
describe('operation', () => {
10+
const context = {
11+
config: {
12+
plugins: {},
13+
},
14+
ir: {
15+
paths: {},
16+
servers: [],
17+
},
18+
} as unknown as IR.Context;
19+
20+
it('should parse operation correctly', () => {
21+
const method = 'get';
22+
const operation: ParseOperationProps['operation'] = {
23+
operationId: 'testOperation',
24+
responses: {},
25+
security: [
26+
{
27+
apiKeyAuth: [],
28+
},
29+
{
30+
apiKeyAuth: [],
31+
},
32+
],
33+
summary: 'Test Operation',
34+
};
35+
const path = '/test';
36+
const securitySchemesMap = new Map<string, SecuritySchemeObject>([
37+
['apiKeyAuth', { in: 'header', name: 'apiKey', type: 'apiKey' }],
38+
]);
39+
const state: ParseOperationProps['state'] = {
40+
ids: new Map<string, string>(),
41+
};
42+
43+
parseOperation({
44+
context,
45+
method,
46+
operation,
47+
path,
48+
securitySchemesMap,
49+
state,
50+
});
51+
52+
expect(context.ir.paths?.[path]?.[method]).toEqual({
53+
id: 'testOperation',
54+
method,
55+
operationId: 'testOperation',
56+
path,
57+
security: [{ in: 'header', name: 'apiKey', type: 'apiKey' }],
58+
summary: 'Test Operation',
59+
});
60+
});
61+
});

packages/openapi-ts/src/openApi/3.1.x/parser/operation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ const operationToIrOperation = ({
188188
}
189189

190190
if (operation.security) {
191-
const securitySchemeObjects: Array<IR.SecurityObject> = [];
191+
const securitySchemeObjects: Map<string, IR.SecurityObject> = new Map();
192192

193193
for (const securityRequirementObject of operation.security) {
194194
for (const name in securityRequirementObject) {
@@ -198,12 +198,12 @@ const operationToIrOperation = ({
198198
continue;
199199
}
200200

201-
securitySchemeObjects.push(securitySchemeObject);
201+
securitySchemeObjects.set(name, securitySchemeObject);
202202
}
203203
}
204204

205-
if (securitySchemeObjects.length) {
206-
irOperation.security = securitySchemeObjects;
205+
if (securitySchemeObjects.size) {
206+
irOperation.security = Array.from(securitySchemeObjects.values());
207207
}
208208
}
209209

0 commit comments

Comments
 (0)