Skip to content

Commit 1fdf359

Browse files
dracomithrilmichalgrezel
authored andcommitted
reduce duplicates
1 parent 7f3308a commit 1fdf359

File tree

6 files changed

+277
-12
lines changed

6 files changed

+277
-12
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
basicAuthRule: [],
29+
},
30+
{
31+
apiKeyAuth: [],
32+
oauthRule: [],
33+
},
34+
],
35+
summary: 'Test Operation',
36+
};
37+
const path = '/test';
38+
const securitySchemesMap = new Map<string, SecuritySchemeObject>([
39+
['apiKeyAuth', { in: 'header', name: 'Auth', type: 'apiKey' }],
40+
['basicAuthRule', { description: 'Basic Auth', type: 'basic' }],
41+
[
42+
'oauthRule',
43+
{
44+
description: 'OAuth2',
45+
flow: 'password',
46+
scopes: {
47+
read: 'Grants read access',
48+
write: 'Grants write access',
49+
},
50+
tokenUrl: 'https://example.com/oauth/token',
51+
type: 'oauth2',
52+
},
53+
],
54+
]);
55+
const state: ParseOperationProps['state'] = {
56+
ids: new Map<string, string>(),
57+
};
58+
59+
parseOperation({
60+
context,
61+
method,
62+
operation,
63+
path,
64+
securitySchemesMap,
65+
state,
66+
});
67+
68+
expect(context.ir.paths?.[path]?.[method]).toEqual({
69+
id: 'testOperation',
70+
method,
71+
operationId: 'testOperation',
72+
path,
73+
security: [
74+
{ in: 'header', name: 'Auth', type: 'apiKey' },
75+
{ description: 'Basic Auth', scheme: 'basic', type: 'http' },
76+
{
77+
description: 'OAuth2',
78+
flows: {
79+
password: {
80+
scopes: {
81+
read: 'Grants read access',
82+
write: 'Grants write access',
83+
},
84+
tokenUrl: 'https://example.com/oauth/token',
85+
},
86+
},
87+
type: 'oauth2',
88+
},
89+
],
90+
summary: 'Test Operation',
91+
});
92+
});
93+
});

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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
oauthRule: ['read'],
34+
},
35+
{
36+
oauthRule: ['write'],
37+
},
38+
],
39+
summary: 'Test Operation',
40+
};
41+
const path = '/test';
42+
43+
const oauth2: SecuritySchemeObject = {
44+
description: 'OAuth2',
45+
flows: {
46+
password: {
47+
scopes: {
48+
read: 'Grants read access',
49+
write: 'Grants write access',
50+
},
51+
tokenUrl: 'https://example.com/oauth/token',
52+
},
53+
},
54+
type: 'oauth2',
55+
};
56+
const securitySchemesMap = new Map<string, SecuritySchemeObject>([
57+
['apiKeyAuth', { in: 'header', name: 'Auth', type: 'apiKey' }],
58+
[
59+
'basicAuthRule',
60+
{ description: 'Basic Auth', scheme: 'basic', type: 'http' },
61+
],
62+
['oauthRule', oauth2],
63+
]);
64+
const state: ParseOperationProps['state'] = {
65+
ids: new Map<string, string>(),
66+
};
67+
68+
parseOperation({
69+
context,
70+
method,
71+
operation,
72+
path,
73+
securitySchemesMap,
74+
state,
75+
});
76+
77+
expect(context.ir.paths?.[path]?.[method]).toEqual({
78+
id: 'testOperation',
79+
method,
80+
operationId: 'testOperation',
81+
path,
82+
security: [{ in: 'header', name: 'Auth', type: 'apiKey' }, oauth2],
83+
summary: 'Test Operation',
84+
});
85+
});
86+
});

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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
oauthRule: ['read'],
34+
},
35+
{
36+
oauthRule: ['write'],
37+
},
38+
],
39+
summary: 'Test Operation',
40+
};
41+
const path = '/test';
42+
43+
const oauth2: SecuritySchemeObject = {
44+
description: 'OAuth2',
45+
flows: {
46+
password: {
47+
scopes: {
48+
read: 'Grants read access',
49+
write: 'Grants write access',
50+
},
51+
tokenUrl: 'https://example.com/oauth/token',
52+
},
53+
},
54+
type: 'oauth2',
55+
};
56+
const securitySchemesMap = new Map<string, SecuritySchemeObject>([
57+
['apiKeyAuth', { in: 'header', name: 'Auth', type: 'apiKey' }],
58+
[
59+
'basicAuthRule',
60+
{ description: 'Basic Auth', scheme: 'basic', type: 'http' },
61+
],
62+
['oauthRule', oauth2],
63+
]);
64+
const state: ParseOperationProps['state'] = {
65+
ids: new Map<string, string>(),
66+
};
67+
68+
parseOperation({
69+
context,
70+
method,
71+
operation,
72+
path,
73+
securitySchemesMap,
74+
state,
75+
});
76+
77+
expect(context.ir.paths?.[path]?.[method]).toEqual({
78+
id: 'testOperation',
79+
method,
80+
operationId: 'testOperation',
81+
path,
82+
security: [{ in: 'header', name: 'Auth', type: 'apiKey' }, oauth2],
83+
summary: 'Test Operation',
84+
});
85+
});
86+
});

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)