Skip to content

Commit 180de62

Browse files
feat: improve generate-arazzo command output (#1962)
1 parent 8b4816a commit 180de62

File tree

13 files changed

+895
-82
lines changed

13 files changed

+895
-82
lines changed

.changeset/silver-crews-eat.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@redocly/respect-core": patch
3+
"@redocly/cli": patch
4+
---
5+
6+
Added support for `basic`, `bearer`, and `apiKey` security schemes in workflow generation with `generate-arazzo` command.

packages/core/src/typings/openapi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,12 @@ export interface Oas3SecurityRequirement {
292292
}
293293

294294
export interface Oas3SecurityScheme {
295-
type: 'apiKey' | 'http' | 'oauth2' | 'openIdConnect';
295+
type: 'apiKey' | 'http' | 'oauth2' | 'openIdConnect' | 'mutualTLS';
296296
description?: string;
297297
name?: string;
298298
in?: 'query' | 'header' | 'cookie';
299299
scheme?: string;
300-
bearerFormat: string;
300+
bearerFormat?: string;
301301
flows: {
302302
implicit?: {
303303
refreshUrl?: string;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { Oas3SecurityScheme } from 'core/src/typings/openapi';
2+
import { generateSecurityInputsArazzoComponents } from '../../arazzo-description-generator';
3+
4+
describe('generateSecurityInputsArazzoComponents', () => {
5+
it('should generate empty inputs for the security schemes if there are no security schemes', () => {
6+
const securitySchemes = {};
7+
const result = generateSecurityInputsArazzoComponents(securitySchemes);
8+
expect(result).toEqual({ inputs: {} });
9+
});
10+
11+
it('should generate the correct inputs for the Basic auth security scheme', () => {
12+
const securitySchemes = {
13+
basicAuth: {
14+
type: 'http' as const,
15+
scheme: 'basic',
16+
} as Oas3SecurityScheme,
17+
};
18+
const result = generateSecurityInputsArazzoComponents(securitySchemes);
19+
expect(result).toEqual({
20+
inputs: {
21+
basicAuth: {
22+
type: 'object',
23+
properties: {
24+
basicAuth: {
25+
type: 'string',
26+
description: 'Basic authentication',
27+
format: 'password',
28+
},
29+
},
30+
},
31+
},
32+
});
33+
});
34+
35+
it('should generate the correct inputs for the Bearer auth security scheme', () => {
36+
const securitySchemes = {
37+
bearerAuth: {
38+
type: 'http' as const,
39+
scheme: 'bearer',
40+
} as Oas3SecurityScheme,
41+
};
42+
const result = generateSecurityInputsArazzoComponents(securitySchemes);
43+
expect(result).toEqual({
44+
inputs: {
45+
bearerAuth: {
46+
type: 'object',
47+
properties: {
48+
bearerAuth: {
49+
type: 'string',
50+
description: 'JWT Authentication token for ${name}',
51+
format: 'password',
52+
},
53+
},
54+
},
55+
},
56+
});
57+
});
58+
59+
it('should generate the correct inputs for the ApiKey auth security scheme', () => {
60+
const securitySchemes = {
61+
apiKey: {
62+
type: 'apiKey' as const,
63+
name: 'X-API-Key',
64+
in: 'header',
65+
} as Oas3SecurityScheme,
66+
};
67+
const result = generateSecurityInputsArazzoComponents(securitySchemes);
68+
expect(result).toEqual({
69+
inputs: {
70+
apiKey: {
71+
type: 'object',
72+
properties: {
73+
apiKey: {
74+
type: 'string',
75+
description: 'Authentication token for apiKey',
76+
format: 'password',
77+
},
78+
},
79+
},
80+
},
81+
});
82+
});
83+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { generateWorkflowSecurityInputs } from '../../arazzo-description-generator';
2+
3+
describe('generateWorkflowSecurityInputs', () => {
4+
it('should return undefined if there are no security requirements', () => {
5+
const inputsComponents = {};
6+
const security = [] as any[];
7+
const result = generateWorkflowSecurityInputs(inputsComponents, security);
8+
expect(result).toBeUndefined();
9+
});
10+
11+
it('should return the correct workflow security inputs reference', () => {
12+
const inputsComponents = {
13+
inputs: {
14+
basicAuth: {
15+
type: 'object',
16+
},
17+
bearerAuth: {
18+
type: 'string',
19+
},
20+
},
21+
};
22+
const security = [{ basicAuth: [] }];
23+
const result = generateWorkflowSecurityInputs(inputsComponents, security);
24+
expect(result).toEqual({
25+
$ref: '#/components/inputs/basicAuth',
26+
});
27+
});
28+
29+
it('should return undefined if the security requirement is not found in the inputs components', () => {
30+
const inputsComponents = {
31+
inputs: {
32+
basicAuth: {
33+
type: 'object',
34+
},
35+
bearerAuth: {
36+
type: 'string',
37+
},
38+
},
39+
};
40+
const security = [{ apiKey: [] }];
41+
const result = generateWorkflowSecurityInputs(inputsComponents, security);
42+
expect(result).toBeUndefined();
43+
});
44+
});
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { generateWorkflowSecurityParameters } from '../../arazzo-description-generator';
2+
3+
describe('generateWorkflowSecurityParameters', () => {
4+
it('should return the correct workflow security parameters for Basic authentication', () => {
5+
const inputsComponents = {
6+
inputs: {
7+
basicAuth: {
8+
type: 'object',
9+
properties: {
10+
basicAuth: {
11+
type: 'string',
12+
description: 'Basic authentication',
13+
format: 'password',
14+
},
15+
},
16+
},
17+
},
18+
};
19+
20+
const security = [{ basicAuth: [] }];
21+
22+
const securitySchemes = {
23+
basicAuth: {
24+
type: 'http',
25+
scheme: 'basic',
26+
},
27+
} as any;
28+
29+
const result = generateWorkflowSecurityParameters(inputsComponents, security, securitySchemes);
30+
31+
expect(result).toEqual([
32+
{
33+
name: 'Authorization',
34+
value: `Basic {$inputs.basicAuth}`,
35+
in: 'header',
36+
},
37+
]);
38+
});
39+
40+
it('should return the correct workflow security parameters for Bearer authentication', () => {
41+
const inputsComponents = {
42+
inputs: {
43+
bearerAuth: {
44+
type: 'string',
45+
description: 'Bearer token',
46+
format: 'password',
47+
},
48+
},
49+
};
50+
51+
const security = [{ bearerAuth: [] }];
52+
53+
const securitySchemes = {
54+
bearerAuth: {
55+
type: 'http',
56+
scheme: 'bearer',
57+
},
58+
} as any;
59+
60+
const result = generateWorkflowSecurityParameters(inputsComponents, security, securitySchemes);
61+
62+
expect(result).toEqual([
63+
{
64+
name: 'Authorization',
65+
value: `Bearer {$inputs.bearerAuth}`,
66+
in: 'header',
67+
},
68+
]);
69+
});
70+
71+
it('should return the correct workflow security parameters for ApiKey authentication', () => {
72+
const inputsComponents = {
73+
inputs: {
74+
apiKey: {
75+
type: 'string',
76+
description: 'ApiKey token',
77+
format: 'password',
78+
},
79+
},
80+
};
81+
82+
const security = [{ apiKey: [] }];
83+
84+
const securitySchemes = {
85+
apiKey: {
86+
type: 'apiKey',
87+
name: 'X-API-Key',
88+
in: 'header',
89+
},
90+
} as any;
91+
92+
const result = generateWorkflowSecurityParameters(inputsComponents, security, securitySchemes);
93+
94+
expect(result).toEqual([
95+
{
96+
name: 'X-API-Key',
97+
value: `$inputs.apiKey`,
98+
in: 'header',
99+
},
100+
]);
101+
});
102+
103+
it('should return an empty array if there are no security parameters', () => {
104+
const inputsComponents = {};
105+
const security = [] as any;
106+
const securitySchemes = {};
107+
108+
const result = generateWorkflowSecurityParameters(inputsComponents, security, securitySchemes);
109+
110+
expect(result).toEqual([]);
111+
});
112+
113+
it('should return an empty array if the security scheme is not supported', () => {
114+
const inputsComponents = {
115+
inputs: {
116+
oauth2Auth: {
117+
type: 'string',
118+
description: 'OAuth2 token',
119+
},
120+
},
121+
};
122+
const security = [{ oauth2Auth: [] }] as any;
123+
const securitySchemes = {
124+
oauth2Auth: {
125+
type: 'oauth2',
126+
},
127+
} as any;
128+
129+
const result = generateWorkflowSecurityParameters(inputsComponents, security, securitySchemes);
130+
131+
expect(result).toEqual([]);
132+
});
133+
});

0 commit comments

Comments
 (0)