forked from asteasolutions/zod-to-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-components.spec.ts
107 lines (93 loc) · 2.71 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
97
98
99
100
101
102
103
104
105
106
107
import { OpenAPIGenerator } from '../src/openapi-generator';
import { OpenAPIRegistry } from '../src/openapi-registry';
import { z } from 'zod';
import { extendZodWithOpenApi } from '../src/zod-extensions';
extendZodWithOpenApi(z);
const testDocConfig = {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'Swagger Petstore',
description: 'A sample API',
termsOfService: 'http://swagger.io/terms/',
license: {
name: 'Apache 2.0',
url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
},
},
servers: [{ url: 'v1' }],
};
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 OpenAPIGenerator(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 OpenAPIGenerator(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',
},
});
});
});