This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
forked from asteasolutions/zod-to-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-components.spec.ts
96 lines (83 loc) · 2.49 KB
/
custom-components.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { OpenAPIRegistry } from '../src/openapi-registry';
import { z } from 'zod';
import { extendZodWithOpenApi } from '../src/zod-extensions';
import { OpenApiGeneratorV3 } from '../src/v3.0/openapi-generator';
import { testDocConfig } from './lib/helpers';
extendZodWithOpenApi(z);
// TODO: Tests with both generators
describe('Custom components', () => {
it('can register and generate security schemes', () => {
const registry = new OpenAPIRegistry();
const bearerAuth = registry.registerComponent(
'securitySchemes',
'bearerAuth',
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
}
);
registry.registerPath({
path: '/units',
method: 'get',
security: [{ [bearerAuth.name]: [] }],
responses: {
200: {
description: 'Sample response',
content: {
'application/json': {
schema: z.string(),
},
},
},
},
});
const builder = new OpenApiGeneratorV3(registry.definitions);
const document = builder.generateDocument(testDocConfig);
expect(document.paths['/units']?.get?.security).toEqual([
{ bearerAuth: [] },
]);
expect(document.components!.securitySchemes).toEqual({
bearerAuth: {
bearerFormat: 'JWT',
scheme: 'bearer',
type: 'http',
},
});
});
it('can register and generate headers', () => {
const registry = new OpenAPIRegistry();
const apiKeyHeader = registry.registerComponent('headers', 'api-key', {
example: '1234',
required: true,
description: 'The API Key you were given in the developer portal',
});
registry.registerPath({
path: '/units',
method: 'get',
responses: {
200: {
description: 'Sample response',
headers: { 'x-api-key': apiKeyHeader.ref },
content: {
'application/json': {
schema: z.string(),
},
},
},
},
});
const builder = new OpenApiGeneratorV3(registry.definitions);
const document = builder.generateDocument(testDocConfig);
expect(document.paths['/units']?.get?.responses['200'].headers).toEqual({
'x-api-key': { $ref: '#/components/headers/api-key' },
});
expect(document.components!.headers).toEqual({
'api-key': {
example: '1234',
required: true,
description: 'The API Key you were given in the developer portal',
},
});
});
});