@amritk/helpers 0.1.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build-dynamic-ref-map.js +5 -3
- package/dist/generate-type-definition.js +5 -3
- package/dist/has-ref.d.ts +4 -0
- package/dist/has-ref.js +7 -0
- package/dist/schema-guards.d.ts +1 -5
- package/dist/schema-guards.js +5 -3
- package/package.json +13 -3
- package/src/build-dynamic-ref-map.test.ts +65 -0
- package/src/build-dynamic-ref-map.ts +42 -0
- package/src/extract-refs.test.ts +183 -0
- package/src/extract-refs.ts +73 -0
- package/src/generate-type-definition.test.ts +1210 -0
- package/src/generate-type-definition.ts +487 -0
- package/src/has-ref.ts +10 -0
- package/src/is-object.test.ts +65 -0
- package/src/is-object.ts +15 -0
- package/src/parse-documentation.test.ts +313 -0
- package/src/parse-documentation.ts +173 -0
- package/src/ref-to-filename.test.ts +64 -0
- package/src/ref-to-filename.ts +116 -0
- package/src/ref-to-name.test.ts +48 -0
- package/src/ref-to-name.ts +38 -0
- package/src/resolve-dynamic-refs.test.ts +94 -0
- package/src/resolve-dynamic-refs.ts +48 -0
- package/src/resolve-ref.test.ts +186 -0
- package/src/resolve-ref.ts +82 -0
- package/src/safe-accessor.test.ts +41 -0
- package/src/safe-accessor.ts +48 -0
- package/src/schema-guards.test.ts +406 -0
- package/src/schema-guards.ts +160 -0
- package/src/upgrade-draft07-schema.ts +179 -0
- package/src/validate-array.test.ts +52 -0
- package/src/validate-array.ts +15 -0
- package/src/validate-record.test.ts +56 -0
- package/src/validate-record.ts +19 -0
|
@@ -0,0 +1,1210 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { generateTypeDefinition } from './generate-type-definition'
|
|
5
|
+
|
|
6
|
+
describe('generateTypeDefinition', () => {
|
|
7
|
+
it('generates type for deeply nested objects', () => {
|
|
8
|
+
const schema = {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
user: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
profile: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
address: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
street: { type: 'string' },
|
|
21
|
+
city: { type: 'string' },
|
|
22
|
+
zipCode: { type: 'string' },
|
|
23
|
+
},
|
|
24
|
+
required: ['street', 'city'],
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const result = generateTypeDefinition(schema, 'DeeplyNested')
|
|
34
|
+
|
|
35
|
+
expect(result).toStrictEqual(
|
|
36
|
+
'export type DeeplyNested = {\n' +
|
|
37
|
+
' user?: { profile?: { address?: { street: string; city: string; zipCode?: string } } };\n' +
|
|
38
|
+
'};',
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('generates type for array of nested objects', () => {
|
|
43
|
+
const schema = {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: {
|
|
46
|
+
users: {
|
|
47
|
+
type: 'array',
|
|
48
|
+
items: {
|
|
49
|
+
type: 'object',
|
|
50
|
+
properties: {
|
|
51
|
+
id: { type: 'number' },
|
|
52
|
+
name: { type: 'string' },
|
|
53
|
+
email: { type: 'string' },
|
|
54
|
+
},
|
|
55
|
+
required: ['id', 'name'],
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
required: ['users'],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const result = generateTypeDefinition(schema, 'UserList')
|
|
63
|
+
|
|
64
|
+
expect(result).toStrictEqual(
|
|
65
|
+
'export type UserList = {\n' + ' users: { id: number; name: string; email?: string }[];\n' + '};',
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('generates type for nested arrays', () => {
|
|
70
|
+
const schema = {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
matrix: {
|
|
74
|
+
type: 'array',
|
|
75
|
+
items: {
|
|
76
|
+
type: 'array',
|
|
77
|
+
items: {
|
|
78
|
+
type: 'number',
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const result = generateTypeDefinition(schema, 'Matrix')
|
|
86
|
+
|
|
87
|
+
expect(result).toStrictEqual('export type Matrix = {\n' + ' matrix?: number[][];\n' + '};')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('generates type for mixed required and optional fields', () => {
|
|
91
|
+
const schema = {
|
|
92
|
+
type: 'object',
|
|
93
|
+
properties: {
|
|
94
|
+
id: { type: 'string' },
|
|
95
|
+
name: { type: 'string' },
|
|
96
|
+
email: { type: 'string' },
|
|
97
|
+
age: { type: 'number' },
|
|
98
|
+
active: { type: 'boolean' },
|
|
99
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
100
|
+
},
|
|
101
|
+
required: ['id', 'name', 'email'],
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const result = generateTypeDefinition(schema, 'MixedFields')
|
|
105
|
+
|
|
106
|
+
expect(result).toStrictEqual(
|
|
107
|
+
'export type MixedFields = {\n' +
|
|
108
|
+
' id: string;\n' +
|
|
109
|
+
' name: string;\n' +
|
|
110
|
+
' email: string;\n' +
|
|
111
|
+
' age?: number;\n' +
|
|
112
|
+
' active?: boolean;\n' +
|
|
113
|
+
' tags?: string[];\n' +
|
|
114
|
+
'};',
|
|
115
|
+
)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('generates type for object with all primitive types', () => {
|
|
119
|
+
const schema = {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
stringField: { type: 'string' },
|
|
123
|
+
numberField: { type: 'number' },
|
|
124
|
+
integerField: { type: 'integer' },
|
|
125
|
+
booleanField: { type: 'boolean' },
|
|
126
|
+
},
|
|
127
|
+
required: ['stringField', 'numberField', 'integerField', 'booleanField'],
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const result = generateTypeDefinition(schema, 'AllPrimitives')
|
|
131
|
+
|
|
132
|
+
expect(result).toStrictEqual(
|
|
133
|
+
'export type AllPrimitives = {\n' +
|
|
134
|
+
' stringField: string;\n' +
|
|
135
|
+
' numberField: number;\n' +
|
|
136
|
+
' integerField: number;\n' +
|
|
137
|
+
' booleanField: boolean;\n' +
|
|
138
|
+
'};',
|
|
139
|
+
)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('generates type for array without items definition', () => {
|
|
143
|
+
const schema = {
|
|
144
|
+
type: 'object',
|
|
145
|
+
properties: {
|
|
146
|
+
data: {
|
|
147
|
+
type: 'array',
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const result = generateTypeDefinition(schema, 'UnknownArray')
|
|
153
|
+
|
|
154
|
+
expect(result).toStrictEqual('export type UnknownArray = {\n' + ' data?: unknown[];\n' + '};')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('generates type for object without properties', () => {
|
|
158
|
+
const schema = {
|
|
159
|
+
type: 'object',
|
|
160
|
+
properties: {
|
|
161
|
+
metadata: {
|
|
162
|
+
type: 'object',
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const result = generateTypeDefinition(schema, 'GenericObject')
|
|
168
|
+
|
|
169
|
+
expect(result).toStrictEqual('export type GenericObject = {\n' + ' metadata?: object;\n' + '};')
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('generates type for complex nested structure with arrays and objects', () => {
|
|
173
|
+
const schema = {
|
|
174
|
+
type: 'object',
|
|
175
|
+
properties: {
|
|
176
|
+
company: {
|
|
177
|
+
type: 'object',
|
|
178
|
+
properties: {
|
|
179
|
+
name: { type: 'string' },
|
|
180
|
+
departments: {
|
|
181
|
+
type: 'array',
|
|
182
|
+
items: {
|
|
183
|
+
type: 'object',
|
|
184
|
+
properties: {
|
|
185
|
+
name: { type: 'string' },
|
|
186
|
+
employees: {
|
|
187
|
+
type: 'array',
|
|
188
|
+
items: {
|
|
189
|
+
type: 'object',
|
|
190
|
+
properties: {
|
|
191
|
+
id: { type: 'number' },
|
|
192
|
+
name: { type: 'string' },
|
|
193
|
+
},
|
|
194
|
+
required: ['id'],
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
required: ['name'],
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
required: ['name'],
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
required: ['company'],
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const result = generateTypeDefinition(schema, 'Company')
|
|
209
|
+
|
|
210
|
+
expect(result).toStrictEqual(
|
|
211
|
+
'export type Company = {\n' +
|
|
212
|
+
' company: { name: string; departments?: { name: string; employees?: { id: number; name?: string }[] }[] };\n' +
|
|
213
|
+
'};',
|
|
214
|
+
)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('generates type for empty object schema', () => {
|
|
218
|
+
const schema = {
|
|
219
|
+
type: 'object',
|
|
220
|
+
properties: {},
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const result = generateTypeDefinition(schema, 'EmptyObject')
|
|
224
|
+
|
|
225
|
+
expect(result).toStrictEqual('export type EmptyObject = {\n' + '\n' + '};')
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('generates type for object with no type specified', () => {
|
|
229
|
+
const schema = {
|
|
230
|
+
properties: {
|
|
231
|
+
field: { type: 'string' },
|
|
232
|
+
},
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const result = generateTypeDefinition(schema, 'NoType')
|
|
236
|
+
|
|
237
|
+
expect(result).toStrictEqual('export type NoType = {\n' + ' field?: string;\n' + '};')
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
it('generates type for boolean schema true (any value valid)', () => {
|
|
241
|
+
const schema = true
|
|
242
|
+
|
|
243
|
+
const result = generateTypeDefinition(schema, 'BooleanSchema')
|
|
244
|
+
|
|
245
|
+
expect(result).toStrictEqual('export type BooleanSchema = unknown;')
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('generates type for boolean schema false (no value valid)', () => {
|
|
249
|
+
const schema = false
|
|
250
|
+
|
|
251
|
+
const result = generateTypeDefinition(schema, 'NeverSchema')
|
|
252
|
+
|
|
253
|
+
expect(result).toStrictEqual('export type NeverSchema = never;')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('generates type for array of arrays of objects', () => {
|
|
257
|
+
const schema = {
|
|
258
|
+
type: 'object',
|
|
259
|
+
properties: {
|
|
260
|
+
grid: {
|
|
261
|
+
type: 'array',
|
|
262
|
+
items: {
|
|
263
|
+
type: 'array',
|
|
264
|
+
items: {
|
|
265
|
+
type: 'object',
|
|
266
|
+
properties: {
|
|
267
|
+
x: { type: 'number' },
|
|
268
|
+
y: { type: 'number' },
|
|
269
|
+
value: { type: 'string' },
|
|
270
|
+
},
|
|
271
|
+
required: ['x', 'y'],
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const result = generateTypeDefinition(schema, 'Grid')
|
|
279
|
+
|
|
280
|
+
expect(result).toStrictEqual(
|
|
281
|
+
'export type Grid = {\n' + ' grid?: { x: number; y: number; value?: string }[][];\n' + '};',
|
|
282
|
+
)
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
it('generates type for object with all fields required', () => {
|
|
286
|
+
const schema = {
|
|
287
|
+
type: 'object',
|
|
288
|
+
properties: {
|
|
289
|
+
id: { type: 'string' },
|
|
290
|
+
name: { type: 'string' },
|
|
291
|
+
email: { type: 'string' },
|
|
292
|
+
age: { type: 'number' },
|
|
293
|
+
},
|
|
294
|
+
required: ['id', 'name', 'email', 'age'],
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const result = generateTypeDefinition(schema, 'AllRequired')
|
|
298
|
+
|
|
299
|
+
expect(result).toStrictEqual(
|
|
300
|
+
'export type AllRequired = {\n' +
|
|
301
|
+
' id: string;\n' +
|
|
302
|
+
' name: string;\n' +
|
|
303
|
+
' email: string;\n' +
|
|
304
|
+
' age: number;\n' +
|
|
305
|
+
'};',
|
|
306
|
+
)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
it('generates type for object with no fields required', () => {
|
|
310
|
+
const schema = {
|
|
311
|
+
type: 'object',
|
|
312
|
+
properties: {
|
|
313
|
+
id: { type: 'string' },
|
|
314
|
+
name: { type: 'string' },
|
|
315
|
+
email: { type: 'string' },
|
|
316
|
+
},
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const result = generateTypeDefinition(schema, 'AllOptional')
|
|
320
|
+
|
|
321
|
+
expect(result).toStrictEqual(
|
|
322
|
+
'export type AllOptional = {\n' + ' id?: string;\n' + ' name?: string;\n' + ' email?: string;\n' + '};',
|
|
323
|
+
)
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
it('generates type for complex API response structure', () => {
|
|
327
|
+
const schema = {
|
|
328
|
+
type: 'object',
|
|
329
|
+
properties: {
|
|
330
|
+
status: { type: 'string' },
|
|
331
|
+
data: {
|
|
332
|
+
type: 'object',
|
|
333
|
+
properties: {
|
|
334
|
+
users: {
|
|
335
|
+
type: 'array',
|
|
336
|
+
items: {
|
|
337
|
+
type: 'object',
|
|
338
|
+
properties: {
|
|
339
|
+
id: { type: 'number' },
|
|
340
|
+
username: { type: 'string' },
|
|
341
|
+
profile: {
|
|
342
|
+
type: 'object',
|
|
343
|
+
properties: {
|
|
344
|
+
avatar: { type: 'string' },
|
|
345
|
+
bio: { type: 'string' },
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
required: ['id', 'username'],
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
pagination: {
|
|
353
|
+
type: 'object',
|
|
354
|
+
properties: {
|
|
355
|
+
page: { type: 'number' },
|
|
356
|
+
perPage: { type: 'number' },
|
|
357
|
+
total: { type: 'number' },
|
|
358
|
+
},
|
|
359
|
+
required: ['page', 'perPage', 'total'],
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
required: ['users'],
|
|
363
|
+
},
|
|
364
|
+
error: {
|
|
365
|
+
type: 'object',
|
|
366
|
+
properties: {
|
|
367
|
+
code: { type: 'string' },
|
|
368
|
+
message: { type: 'string' },
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
required: ['status'],
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const result = generateTypeDefinition(schema, 'APIResponse')
|
|
376
|
+
|
|
377
|
+
expect(result).toStrictEqual(
|
|
378
|
+
'export type APIResponse = {\n' +
|
|
379
|
+
' status: string;\n' +
|
|
380
|
+
' data?: { users: { id: number; username: string; profile?: { avatar?: string; bio?: string } }[]; pagination?: { page: number; perPage: number; total: number } };\n' +
|
|
381
|
+
' error?: { code?: string; message?: string };\n' +
|
|
382
|
+
'};',
|
|
383
|
+
)
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
it('generates type for array of different primitive types', () => {
|
|
387
|
+
const schema = {
|
|
388
|
+
type: 'object',
|
|
389
|
+
properties: {
|
|
390
|
+
strings: {
|
|
391
|
+
type: 'array',
|
|
392
|
+
items: { type: 'string' },
|
|
393
|
+
},
|
|
394
|
+
numbers: {
|
|
395
|
+
type: 'array',
|
|
396
|
+
items: { type: 'number' },
|
|
397
|
+
},
|
|
398
|
+
booleans: {
|
|
399
|
+
type: 'array',
|
|
400
|
+
items: { type: 'boolean' },
|
|
401
|
+
},
|
|
402
|
+
},
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const result = generateTypeDefinition(schema, 'ArrayTypes')
|
|
406
|
+
|
|
407
|
+
expect(result).toStrictEqual(
|
|
408
|
+
'export type ArrayTypes = {\n' +
|
|
409
|
+
' strings?: string[];\n' +
|
|
410
|
+
' numbers?: number[];\n' +
|
|
411
|
+
' booleans?: boolean[];\n' +
|
|
412
|
+
'};',
|
|
413
|
+
)
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
it('generates type for recursive-like structure', () => {
|
|
417
|
+
const schema = {
|
|
418
|
+
type: 'object',
|
|
419
|
+
properties: {
|
|
420
|
+
id: { type: 'string' },
|
|
421
|
+
children: {
|
|
422
|
+
type: 'array',
|
|
423
|
+
items: {
|
|
424
|
+
type: 'object',
|
|
425
|
+
properties: {
|
|
426
|
+
id: { type: 'string' },
|
|
427
|
+
children: {
|
|
428
|
+
type: 'array',
|
|
429
|
+
items: {
|
|
430
|
+
type: 'object',
|
|
431
|
+
properties: {
|
|
432
|
+
id: { type: 'string' },
|
|
433
|
+
},
|
|
434
|
+
required: ['id'],
|
|
435
|
+
},
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
required: ['id'],
|
|
439
|
+
},
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
required: ['id'],
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const result = generateTypeDefinition(schema, 'TreeNode')
|
|
446
|
+
|
|
447
|
+
expect(result).toStrictEqual(
|
|
448
|
+
'export type TreeNode = {\n' +
|
|
449
|
+
' id: string;\n' +
|
|
450
|
+
' children?: { id: string; children?: { id: string }[] }[];\n' +
|
|
451
|
+
'};',
|
|
452
|
+
)
|
|
453
|
+
})
|
|
454
|
+
|
|
455
|
+
it('generates type for schema with mixed nested structures', () => {
|
|
456
|
+
const schema = {
|
|
457
|
+
type: 'object',
|
|
458
|
+
properties: {
|
|
459
|
+
config: {
|
|
460
|
+
type: 'object',
|
|
461
|
+
properties: {
|
|
462
|
+
settings: {
|
|
463
|
+
type: 'object',
|
|
464
|
+
properties: {
|
|
465
|
+
enabled: { type: 'boolean' },
|
|
466
|
+
options: {
|
|
467
|
+
type: 'array',
|
|
468
|
+
items: { type: 'string' },
|
|
469
|
+
},
|
|
470
|
+
},
|
|
471
|
+
required: ['enabled'],
|
|
472
|
+
},
|
|
473
|
+
metadata: {
|
|
474
|
+
type: 'array',
|
|
475
|
+
items: {
|
|
476
|
+
type: 'object',
|
|
477
|
+
properties: {
|
|
478
|
+
key: { type: 'string' },
|
|
479
|
+
value: { type: 'string' },
|
|
480
|
+
},
|
|
481
|
+
required: ['key', 'value'],
|
|
482
|
+
},
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
},
|
|
486
|
+
},
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const result = generateTypeDefinition(schema, 'Configuration')
|
|
490
|
+
|
|
491
|
+
expect(result).toStrictEqual(
|
|
492
|
+
'export type Configuration = {\n' +
|
|
493
|
+
' config?: { settings?: { enabled: boolean; options?: string[] }; metadata?: { key: string; value: string }[] };\n' +
|
|
494
|
+
'};',
|
|
495
|
+
)
|
|
496
|
+
})
|
|
497
|
+
|
|
498
|
+
it('generates type for schema with property without type', () => {
|
|
499
|
+
const schema = {
|
|
500
|
+
type: 'object',
|
|
501
|
+
properties: {
|
|
502
|
+
name: { type: 'string' },
|
|
503
|
+
unknownField: {},
|
|
504
|
+
age: { type: 'number' },
|
|
505
|
+
},
|
|
506
|
+
required: ['name'],
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const result = generateTypeDefinition(schema, 'UnknownField')
|
|
510
|
+
|
|
511
|
+
expect(result).toStrictEqual(
|
|
512
|
+
'export type UnknownField = {\n' +
|
|
513
|
+
' name: string;\n' +
|
|
514
|
+
' unknownField?: unknown;\n' +
|
|
515
|
+
' age?: number;\n' +
|
|
516
|
+
'};',
|
|
517
|
+
)
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
it('generates type for deeply nested array structures with mixed types', () => {
|
|
521
|
+
const schema: JSONSchema.Object = {
|
|
522
|
+
type: 'object',
|
|
523
|
+
properties: {
|
|
524
|
+
data: {
|
|
525
|
+
type: 'array',
|
|
526
|
+
items: {
|
|
527
|
+
type: 'object',
|
|
528
|
+
properties: {
|
|
529
|
+
id: { type: 'string' },
|
|
530
|
+
tags: {
|
|
531
|
+
type: 'array',
|
|
532
|
+
items: { type: 'string' },
|
|
533
|
+
},
|
|
534
|
+
metadata: {
|
|
535
|
+
type: 'array',
|
|
536
|
+
items: {
|
|
537
|
+
type: 'object',
|
|
538
|
+
properties: {
|
|
539
|
+
key: { type: 'string' },
|
|
540
|
+
values: {
|
|
541
|
+
type: 'array',
|
|
542
|
+
items: { type: 'number' },
|
|
543
|
+
},
|
|
544
|
+
},
|
|
545
|
+
required: ['key'],
|
|
546
|
+
},
|
|
547
|
+
},
|
|
548
|
+
},
|
|
549
|
+
required: ['id'],
|
|
550
|
+
},
|
|
551
|
+
},
|
|
552
|
+
},
|
|
553
|
+
required: ['data'],
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const result = generateTypeDefinition(schema, 'ComplexNestedArrays')
|
|
557
|
+
|
|
558
|
+
expect(result).toStrictEqual(
|
|
559
|
+
'export type ComplexNestedArrays = {\n' +
|
|
560
|
+
' data: { id: string; tags?: string[]; metadata?: { key: string; values?: number[] }[] }[];\n' +
|
|
561
|
+
'};',
|
|
562
|
+
)
|
|
563
|
+
})
|
|
564
|
+
|
|
565
|
+
it('generates type for info-like object schema with URL $comment as JSDoc description', () => {
|
|
566
|
+
const info: JSONSchema.Object = {
|
|
567
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#info-object',
|
|
568
|
+
type: 'object',
|
|
569
|
+
properties: {
|
|
570
|
+
title: { type: 'string' },
|
|
571
|
+
summary: { type: 'string' },
|
|
572
|
+
contact: { $ref: '#/$defs/contact' },
|
|
573
|
+
version: { type: 'string' },
|
|
574
|
+
},
|
|
575
|
+
required: ['title', 'version'],
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const result = generateTypeDefinition(info, 'InfoObject')
|
|
579
|
+
|
|
580
|
+
expect(result).toStrictEqual(
|
|
581
|
+
'/**\n' +
|
|
582
|
+
'* InfoObject\n' +
|
|
583
|
+
'*\n' +
|
|
584
|
+
'* https://spec.openapis.org/oas/v3.1#info-object\n' +
|
|
585
|
+
'*/\n' +
|
|
586
|
+
'export type InfoObject = {\n' +
|
|
587
|
+
' title: string;\n' +
|
|
588
|
+
' summary?: string;\n' +
|
|
589
|
+
' contact?: ContactObject;\n' +
|
|
590
|
+
' version: string;\n' +
|
|
591
|
+
'};',
|
|
592
|
+
)
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
it('generates type for object with additionalProperties refs as Record type', () => {
|
|
596
|
+
const components: JSONSchema.Object = {
|
|
597
|
+
type: 'object',
|
|
598
|
+
properties: {
|
|
599
|
+
responses: {
|
|
600
|
+
type: 'object',
|
|
601
|
+
additionalProperties: { $ref: '#/$defs/response' },
|
|
602
|
+
},
|
|
603
|
+
parameters: {
|
|
604
|
+
type: 'object',
|
|
605
|
+
additionalProperties: { $ref: '#/$defs/parameter' },
|
|
606
|
+
},
|
|
607
|
+
pathItems: {
|
|
608
|
+
type: 'object',
|
|
609
|
+
additionalProperties: { $ref: '#/$defs/path-item' },
|
|
610
|
+
},
|
|
611
|
+
},
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const result = generateTypeDefinition(components, 'ComponentsObject')
|
|
615
|
+
|
|
616
|
+
expect(result).toStrictEqual(
|
|
617
|
+
'export type ComponentsObject = {\n' +
|
|
618
|
+
' responses?: Record<string, ResponseObject>;\n' +
|
|
619
|
+
' parameters?: Record<string, ParameterObject>;\n' +
|
|
620
|
+
' pathItems?: Record<string, PathItemObject>;\n' +
|
|
621
|
+
'};',
|
|
622
|
+
)
|
|
623
|
+
})
|
|
624
|
+
|
|
625
|
+
it('generates type for object with paths property as Record<string, PathItemObject>', () => {
|
|
626
|
+
const document: JSONSchema.Object = {
|
|
627
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#openapi-object',
|
|
628
|
+
type: 'object',
|
|
629
|
+
properties: {
|
|
630
|
+
openapi: { type: 'string' },
|
|
631
|
+
info: { $ref: '#/$defs/info' },
|
|
632
|
+
paths: {
|
|
633
|
+
type: 'object',
|
|
634
|
+
additionalProperties: { $ref: '#/$defs/path-item' },
|
|
635
|
+
},
|
|
636
|
+
},
|
|
637
|
+
required: ['openapi', 'info'],
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const result = generateTypeDefinition(document, 'Document')
|
|
641
|
+
|
|
642
|
+
expect(result).toStrictEqual(
|
|
643
|
+
'/**\n' +
|
|
644
|
+
'* Document\n' +
|
|
645
|
+
'*\n' +
|
|
646
|
+
'* https://spec.openapis.org/oas/v3.1#openapi-object\n' +
|
|
647
|
+
'*/\n' +
|
|
648
|
+
'export type Document = {\n' +
|
|
649
|
+
' openapi: string;\n' +
|
|
650
|
+
' info: InfoObject;\n' +
|
|
651
|
+
' paths?: Record<string, PathItemObject>;\n' +
|
|
652
|
+
'};',
|
|
653
|
+
)
|
|
654
|
+
})
|
|
655
|
+
|
|
656
|
+
it('generates type for Document with paths and webhooks as Record types', () => {
|
|
657
|
+
const document: JSONSchema.Object = {
|
|
658
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#openapi-object',
|
|
659
|
+
type: 'object',
|
|
660
|
+
properties: {
|
|
661
|
+
openapi: {
|
|
662
|
+
type: 'string',
|
|
663
|
+
},
|
|
664
|
+
info: {
|
|
665
|
+
$ref: '#/$defs/info',
|
|
666
|
+
},
|
|
667
|
+
jsonSchemaDialect: {
|
|
668
|
+
type: 'string',
|
|
669
|
+
},
|
|
670
|
+
servers: {
|
|
671
|
+
type: 'array',
|
|
672
|
+
items: {
|
|
673
|
+
$ref: '#/$defs/server',
|
|
674
|
+
},
|
|
675
|
+
},
|
|
676
|
+
paths: {
|
|
677
|
+
type: 'object',
|
|
678
|
+
additionalProperties: {
|
|
679
|
+
$ref: '#/$defs/path-item',
|
|
680
|
+
},
|
|
681
|
+
},
|
|
682
|
+
webhooks: {
|
|
683
|
+
type: 'object',
|
|
684
|
+
additionalProperties: {
|
|
685
|
+
$ref: '#/$defs/path-item',
|
|
686
|
+
},
|
|
687
|
+
},
|
|
688
|
+
components: {
|
|
689
|
+
$ref: '#/$defs/components',
|
|
690
|
+
},
|
|
691
|
+
},
|
|
692
|
+
required: ['openapi', 'info'],
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
const result = generateTypeDefinition(document, 'Document')
|
|
696
|
+
|
|
697
|
+
expect(result).toStrictEqual(
|
|
698
|
+
'/**\n' +
|
|
699
|
+
'* Document\n' +
|
|
700
|
+
'*\n' +
|
|
701
|
+
'* https://spec.openapis.org/oas/v3.1#openapi-object\n' +
|
|
702
|
+
'*/\n' +
|
|
703
|
+
'export type Document = {\n' +
|
|
704
|
+
' openapi: string;\n' +
|
|
705
|
+
' info: InfoObject;\n' +
|
|
706
|
+
' jsonSchemaDialect?: string;\n' +
|
|
707
|
+
' servers?: ServerObject[];\n' +
|
|
708
|
+
' paths?: Record<string, PathItemObject>;\n' +
|
|
709
|
+
' webhooks?: Record<string, PathItemObject>;\n' +
|
|
710
|
+
' components?: ComponentsObject;\n' +
|
|
711
|
+
'};',
|
|
712
|
+
)
|
|
713
|
+
})
|
|
714
|
+
|
|
715
|
+
it('generates type for object with patternProperties as Record type', () => {
|
|
716
|
+
const paths: JSONSchema.Object = {
|
|
717
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#paths-object',
|
|
718
|
+
type: 'object',
|
|
719
|
+
patternProperties: {
|
|
720
|
+
'^/': {
|
|
721
|
+
$ref: '#/$defs/path-item',
|
|
722
|
+
},
|
|
723
|
+
},
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const result = generateTypeDefinition(paths, 'PathsObject')
|
|
727
|
+
|
|
728
|
+
expect(result).toStrictEqual(
|
|
729
|
+
'/**\n' +
|
|
730
|
+
'* PathsObject\n' +
|
|
731
|
+
'*\n' +
|
|
732
|
+
'* https://spec.openapis.org/oas/v3.1#paths-object\n' +
|
|
733
|
+
'*/\n' +
|
|
734
|
+
'export type PathsObject = Record<string, PathItemObject>;',
|
|
735
|
+
)
|
|
736
|
+
})
|
|
737
|
+
|
|
738
|
+
it('quotes hyphenated property names in type definitions', () => {
|
|
739
|
+
const schema = {
|
|
740
|
+
type: 'object' as const,
|
|
741
|
+
properties: {
|
|
742
|
+
'x-linkedin': { type: 'string' as const },
|
|
743
|
+
name: { type: 'string' as const },
|
|
744
|
+
},
|
|
745
|
+
}
|
|
746
|
+
const result = generateTypeDefinition(schema, 'InfoExtensionsObject')
|
|
747
|
+
expect(result).toContain("'x-linkedin'?: string;")
|
|
748
|
+
expect(result).toContain('name?: string;')
|
|
749
|
+
})
|
|
750
|
+
|
|
751
|
+
it('generates type from conditional if/then object fragments', () => {
|
|
752
|
+
const schema: JSONSchema = {
|
|
753
|
+
if: {
|
|
754
|
+
properties: {
|
|
755
|
+
type: {
|
|
756
|
+
const: 'http',
|
|
757
|
+
},
|
|
758
|
+
},
|
|
759
|
+
},
|
|
760
|
+
then: {
|
|
761
|
+
properties: {
|
|
762
|
+
scheme: {
|
|
763
|
+
type: 'string',
|
|
764
|
+
},
|
|
765
|
+
},
|
|
766
|
+
required: ['scheme'],
|
|
767
|
+
},
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
const result = generateTypeDefinition(schema, 'TypeHttpObject')
|
|
771
|
+
|
|
772
|
+
expect(result).toStrictEqual(
|
|
773
|
+
'export type TypeHttpObject = {\n' + ' type: "http";\n' + ' scheme: string;\n' + '};',
|
|
774
|
+
)
|
|
775
|
+
})
|
|
776
|
+
|
|
777
|
+
it('generates required property from then properties without explicit required', () => {
|
|
778
|
+
const schema: JSONSchema = {
|
|
779
|
+
if: {
|
|
780
|
+
properties: {
|
|
781
|
+
type: {
|
|
782
|
+
const: 'http',
|
|
783
|
+
},
|
|
784
|
+
},
|
|
785
|
+
},
|
|
786
|
+
then: {
|
|
787
|
+
properties: {
|
|
788
|
+
bearerFormat: {
|
|
789
|
+
type: 'string',
|
|
790
|
+
},
|
|
791
|
+
},
|
|
792
|
+
},
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
const result = generateTypeDefinition(schema, 'TypeHttpObject')
|
|
796
|
+
|
|
797
|
+
expect(result).toStrictEqual(
|
|
798
|
+
'export type TypeHttpObject = {\n' + ' type: "http";\n' + ' bearerFormat: string;\n' + '};',
|
|
799
|
+
)
|
|
800
|
+
})
|
|
801
|
+
|
|
802
|
+
it('generates required discriminator for conditional type-http schema with $comment JSDoc', () => {
|
|
803
|
+
const schema: JSONSchema = {
|
|
804
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#security-scheme-object',
|
|
805
|
+
if: {
|
|
806
|
+
properties: {
|
|
807
|
+
type: {
|
|
808
|
+
const: 'http',
|
|
809
|
+
},
|
|
810
|
+
scheme: {
|
|
811
|
+
type: 'string',
|
|
812
|
+
pattern: '^[Bb][Ee][Aa][Rr][Ee][Rr]$',
|
|
813
|
+
},
|
|
814
|
+
},
|
|
815
|
+
required: ['type', 'scheme'],
|
|
816
|
+
},
|
|
817
|
+
then: {
|
|
818
|
+
properties: {
|
|
819
|
+
bearerFormat: {
|
|
820
|
+
type: 'string',
|
|
821
|
+
},
|
|
822
|
+
},
|
|
823
|
+
},
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const result = generateTypeDefinition(schema, 'TypeHttpObject')
|
|
827
|
+
|
|
828
|
+
expect(result).toContain('* TypeHttpObject')
|
|
829
|
+
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#security-scheme-object')
|
|
830
|
+
expect(result).toContain('type: "http";')
|
|
831
|
+
expect(result).toContain('scheme: string;')
|
|
832
|
+
expect(result).toContain('bearerFormat: string;')
|
|
833
|
+
})
|
|
834
|
+
|
|
835
|
+
it('generates intersection type for schema with allOf $ref entries', () => {
|
|
836
|
+
const securityScheme: JSONSchema.Object = {
|
|
837
|
+
type: 'object',
|
|
838
|
+
properties: {
|
|
839
|
+
type: {
|
|
840
|
+
enum: ['apiKey', 'http', 'oauth2'],
|
|
841
|
+
},
|
|
842
|
+
description: {
|
|
843
|
+
type: 'string',
|
|
844
|
+
},
|
|
845
|
+
},
|
|
846
|
+
required: ['type'],
|
|
847
|
+
allOf: [{ $ref: '#/$defs/type-apikey' }, { $ref: '#/$defs/type-http' }, { $ref: '#/$defs/type-oauth2' }],
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
const result = generateTypeDefinition(securityScheme, 'SecuritySchemeObject')
|
|
851
|
+
|
|
852
|
+
expect(result).toStrictEqual(
|
|
853
|
+
'export type SecuritySchemeObject = {\n' +
|
|
854
|
+
' type: "apiKey" | "http" | "oauth2";\n' +
|
|
855
|
+
' description?: string;\n' +
|
|
856
|
+
'} & TypeApikeyObject & TypeHttpObject & TypeOauth2Object;',
|
|
857
|
+
)
|
|
858
|
+
})
|
|
859
|
+
|
|
860
|
+
it('generates JSDoc from $comment URL for schema with allOf intersections', () => {
|
|
861
|
+
const securityScheme: JSONSchema.Object = {
|
|
862
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#security-scheme-object',
|
|
863
|
+
type: 'object',
|
|
864
|
+
properties: {
|
|
865
|
+
type: {
|
|
866
|
+
enum: ['apiKey', 'http', 'oauth2'],
|
|
867
|
+
},
|
|
868
|
+
},
|
|
869
|
+
required: ['type'],
|
|
870
|
+
allOf: [{ $ref: '#/$defs/type-apikey' }, { $ref: '#/$defs/type-http' }],
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const result = generateTypeDefinition(securityScheme, 'SecuritySchemeObject')
|
|
874
|
+
|
|
875
|
+
expect(result).toContain('* SecuritySchemeObject')
|
|
876
|
+
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#security-scheme-object')
|
|
877
|
+
expect(result).toContain('type: "apiKey" | "http" | "oauth2";')
|
|
878
|
+
expect(result).toContain('} & TypeApikeyObject & TypeHttpObject;')
|
|
879
|
+
})
|
|
880
|
+
|
|
881
|
+
it('generates record type for patternProperties-only schema without explicit type', () => {
|
|
882
|
+
const schema: JSONSchema = {
|
|
883
|
+
patternProperties: {
|
|
884
|
+
'^x-': true,
|
|
885
|
+
},
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
const result = generateTypeDefinition(schema, 'SpecificationExtensionsObject')
|
|
889
|
+
|
|
890
|
+
expect(result).toStrictEqual('export type SpecificationExtensionsObject = Record<`x-${string}`, unknown>;')
|
|
891
|
+
})
|
|
892
|
+
|
|
893
|
+
it('generates Record<string, never> for patternProperties-only schema with false boolean value', () => {
|
|
894
|
+
// The false boolean schema means no values are allowed for matching keys,
|
|
895
|
+
// which maps to the never type in TypeScript.
|
|
896
|
+
const schema: JSONSchema = {
|
|
897
|
+
patternProperties: {
|
|
898
|
+
'^x-': false,
|
|
899
|
+
},
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const result = generateTypeDefinition(schema, 'RestrictedObject')
|
|
903
|
+
|
|
904
|
+
expect(result).toStrictEqual('export type RestrictedObject = Record<`x-${string}`, never>;')
|
|
905
|
+
})
|
|
906
|
+
|
|
907
|
+
it('generates Schema type for property with $dynamicRef pointing to #meta', () => {
|
|
908
|
+
// $dynamicRef: '#meta' is a JSON Schema 2020-12 pattern used for recursive
|
|
909
|
+
// schema definitions that refer to the root Schema type itself.
|
|
910
|
+
const schema: JSONSchema.Object = {
|
|
911
|
+
type: 'object',
|
|
912
|
+
properties: {
|
|
913
|
+
schema: { $dynamicRef: '#meta' },
|
|
914
|
+
},
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
const result = generateTypeDefinition(schema, 'SchemaContainer')
|
|
918
|
+
|
|
919
|
+
expect(result).toContain('schema?: Schema')
|
|
920
|
+
})
|
|
921
|
+
|
|
922
|
+
it('generates type name from non-meta $dynamicRef', () => {
|
|
923
|
+
// A $dynamicRef other than '#meta' is converted via refToName like a $ref.
|
|
924
|
+
const schema: JSONSchema.Object = {
|
|
925
|
+
type: 'object',
|
|
926
|
+
properties: {
|
|
927
|
+
content: { $dynamicRef: '#/$defs/schema' },
|
|
928
|
+
},
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
const result = generateTypeDefinition(schema, 'ContentContainer')
|
|
932
|
+
|
|
933
|
+
expect(result).toContain('content?: SchemaObject')
|
|
934
|
+
})
|
|
935
|
+
|
|
936
|
+
it('generates union type for schema with array of types', () => {
|
|
937
|
+
// JSON Schema allows `type` to be an array of strings to express a union type.
|
|
938
|
+
const schema: JSONSchema.Object = {
|
|
939
|
+
type: 'object',
|
|
940
|
+
properties: {
|
|
941
|
+
value: { type: ['string', 'null'] },
|
|
942
|
+
},
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
const result = generateTypeDefinition(schema, 'NullableStringContainer')
|
|
946
|
+
|
|
947
|
+
expect(result).toContain('value?: string | null')
|
|
948
|
+
})
|
|
949
|
+
|
|
950
|
+
it('generates correct union for all supported types in type array', () => {
|
|
951
|
+
const schema: JSONSchema.Object = {
|
|
952
|
+
type: 'object',
|
|
953
|
+
properties: {
|
|
954
|
+
anything: { type: ['string', 'number', 'boolean', 'null', 'array', 'object'] },
|
|
955
|
+
},
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
const result = generateTypeDefinition(schema, 'AnyTypeContainer')
|
|
959
|
+
|
|
960
|
+
expect(result).toContain('string | number | boolean | null | unknown[] | Record<string, unknown>')
|
|
961
|
+
})
|
|
962
|
+
|
|
963
|
+
it('infers Record<string, unknown> type for no-type property with boolean true additionalProperties', () => {
|
|
964
|
+
// A schema with additionalProperties: true and no explicit type is treated
|
|
965
|
+
// as an open record allowing any values.
|
|
966
|
+
const schema: JSONSchema.Object = {
|
|
967
|
+
type: 'object',
|
|
968
|
+
properties: {
|
|
969
|
+
extensions: { additionalProperties: true },
|
|
970
|
+
},
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
const result = generateTypeDefinition(schema, 'Container')
|
|
974
|
+
|
|
975
|
+
expect(result).toContain('extensions?: Record<string, unknown>')
|
|
976
|
+
})
|
|
977
|
+
|
|
978
|
+
it('infers Record<string, never> type for no-type property with boolean false additionalProperties', () => {
|
|
979
|
+
// A schema with additionalProperties: false and no explicit type means
|
|
980
|
+
// no values are allowed, which maps to the never type.
|
|
981
|
+
const schema: JSONSchema.Object = {
|
|
982
|
+
type: 'object',
|
|
983
|
+
properties: {
|
|
984
|
+
locked: { additionalProperties: false },
|
|
985
|
+
},
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
const result = generateTypeDefinition(schema, 'Container')
|
|
989
|
+
|
|
990
|
+
expect(result).toContain('locked?: Record<string, never>')
|
|
991
|
+
})
|
|
992
|
+
|
|
993
|
+
it('infers Record<`x-${string}`, unknown> for no-type property with ^x- patternProperties', () => {
|
|
994
|
+
// The ^x- pattern is a common JSON Schema convention for vendor extensions that
|
|
995
|
+
// maps naturally to the TypeScript template literal `x-${string}`.
|
|
996
|
+
const schema: JSONSchema.Object = {
|
|
997
|
+
type: 'object',
|
|
998
|
+
properties: {
|
|
999
|
+
extensions: { patternProperties: { '^x-': true } },
|
|
1000
|
+
},
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
const result = generateTypeDefinition(schema, 'Container')
|
|
1004
|
+
|
|
1005
|
+
expect(result).toContain('extensions?: Record<`x-${string}`, unknown>')
|
|
1006
|
+
})
|
|
1007
|
+
|
|
1008
|
+
it('infers string type for no-type property whose default is a string', () => {
|
|
1009
|
+
// When a property has no explicit type but has a string default, we infer
|
|
1010
|
+
// the type as string so the generated TypeScript stays as specific as possible.
|
|
1011
|
+
const schema: JSONSchema.Object = {
|
|
1012
|
+
type: 'object',
|
|
1013
|
+
properties: {
|
|
1014
|
+
format: { default: 'json' },
|
|
1015
|
+
},
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
const result = generateTypeDefinition(schema, 'Config')
|
|
1019
|
+
|
|
1020
|
+
expect(result).toContain('format?: string')
|
|
1021
|
+
})
|
|
1022
|
+
|
|
1023
|
+
it('infers number type for no-type property whose default is a number', () => {
|
|
1024
|
+
const schema: JSONSchema.Object = {
|
|
1025
|
+
type: 'object',
|
|
1026
|
+
properties: {
|
|
1027
|
+
timeout: { default: 30 },
|
|
1028
|
+
},
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
const result = generateTypeDefinition(schema, 'Config')
|
|
1032
|
+
|
|
1033
|
+
expect(result).toContain('timeout?: number')
|
|
1034
|
+
})
|
|
1035
|
+
|
|
1036
|
+
it('infers boolean type for no-type property whose default is a boolean', () => {
|
|
1037
|
+
const schema: JSONSchema.Object = {
|
|
1038
|
+
type: 'object',
|
|
1039
|
+
properties: {
|
|
1040
|
+
enabled: { default: true },
|
|
1041
|
+
},
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
const result = generateTypeDefinition(schema, 'Config')
|
|
1045
|
+
|
|
1046
|
+
expect(result).toContain('enabled?: boolean')
|
|
1047
|
+
})
|
|
1048
|
+
|
|
1049
|
+
it('generates type with JSDoc for additionalProperties-only schema when documentation is found', () => {
|
|
1050
|
+
// This tests the documentation block (lines 431–438) inside the additionalProperties-only
|
|
1051
|
+
// path — a code path that is only reached when the schema has no fixed properties but
|
|
1052
|
+
// does have additionalProperties, and a matching documentation section exists.
|
|
1053
|
+
const schema: JSONSchema.Object = {
|
|
1054
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#callback-object',
|
|
1055
|
+
type: 'object',
|
|
1056
|
+
additionalProperties: {
|
|
1057
|
+
$ref: '#/$defs/path-item',
|
|
1058
|
+
},
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
const result = generateTypeDefinition(schema, 'CallbackObject')
|
|
1062
|
+
|
|
1063
|
+
expect(result).toContain('/**')
|
|
1064
|
+
expect(result).toContain('* CallbackObject')
|
|
1065
|
+
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#callback-object')
|
|
1066
|
+
expect(result).toContain('[key: string]: PathItemObject')
|
|
1067
|
+
})
|
|
1068
|
+
|
|
1069
|
+
it('generates type for product schema with required, optional, and array fields', () => {
|
|
1070
|
+
const schema: JSONSchema = {
|
|
1071
|
+
description: 'A product available for purchase in the catalog.',
|
|
1072
|
+
type: 'object',
|
|
1073
|
+
properties: {
|
|
1074
|
+
id: { description: 'Unique product identifier (UUID).', type: 'string' },
|
|
1075
|
+
name: { description: 'Display name shown to customers.', type: 'string' },
|
|
1076
|
+
price: { description: 'Unit price in USD cents (must be non-negative).', type: 'number', minimum: 0 },
|
|
1077
|
+
inStock: { description: 'Whether the product is currently available for purchase.', type: 'boolean' },
|
|
1078
|
+
tags: {
|
|
1079
|
+
description: 'Searchable labels associated with the product.',
|
|
1080
|
+
type: 'array',
|
|
1081
|
+
items: { type: 'string' },
|
|
1082
|
+
},
|
|
1083
|
+
},
|
|
1084
|
+
required: ['id', 'name', 'price'],
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
const result = generateTypeDefinition(schema, 'Product')
|
|
1088
|
+
|
|
1089
|
+
expect(result).toBe(
|
|
1090
|
+
'export type Product = {\n' +
|
|
1091
|
+
' /** Unique product identifier (UUID). */\n' +
|
|
1092
|
+
' id: string;\n' +
|
|
1093
|
+
' /** Display name shown to customers. */\n' +
|
|
1094
|
+
' name: string;\n' +
|
|
1095
|
+
' /** Unit price in USD cents (must be non-negative). */\n' +
|
|
1096
|
+
' price: number;\n' +
|
|
1097
|
+
' /** Whether the product is currently available for purchase. */\n' +
|
|
1098
|
+
' inStock?: boolean;\n' +
|
|
1099
|
+
' /** Searchable labels associated with the product. */\n' +
|
|
1100
|
+
' tags?: string[];\n' +
|
|
1101
|
+
'};',
|
|
1102
|
+
)
|
|
1103
|
+
})
|
|
1104
|
+
|
|
1105
|
+
it('generates type for string enum schema', () => {
|
|
1106
|
+
const schema: JSONSchema = {
|
|
1107
|
+
description: 'One of the supported theme colors.',
|
|
1108
|
+
type: 'string',
|
|
1109
|
+
enum: ['red', 'green', 'blue', 'yellow', 'purple'],
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
const result = generateTypeDefinition(schema, 'ThemeColor')
|
|
1113
|
+
|
|
1114
|
+
expect(result).toBe('export type ThemeColor = "red" | "green" | "blue" | "yellow" | "purple";')
|
|
1115
|
+
})
|
|
1116
|
+
|
|
1117
|
+
it('generates type for geo coordinate with min/max constraints on required number fields', () => {
|
|
1118
|
+
const schema: JSONSchema = {
|
|
1119
|
+
description: 'A geographic coordinate pair.',
|
|
1120
|
+
type: 'object',
|
|
1121
|
+
properties: {
|
|
1122
|
+
latitude: { description: 'Degrees latitude, from -90 to 90.', type: 'number', minimum: -90, maximum: 90 },
|
|
1123
|
+
longitude: { description: 'Degrees longitude, from -180 to 180.', type: 'number', minimum: -180, maximum: 180 },
|
|
1124
|
+
altitude: { description: 'Elevation in metres above sea level.', type: 'number' },
|
|
1125
|
+
label: { description: 'Human-readable name for this location.', type: 'string' },
|
|
1126
|
+
},
|
|
1127
|
+
required: ['latitude', 'longitude'],
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
const result = generateTypeDefinition(schema, 'GeoCoordinate')
|
|
1131
|
+
|
|
1132
|
+
expect(result).toBe(
|
|
1133
|
+
'export type GeoCoordinate = {\n' +
|
|
1134
|
+
' /** Degrees latitude, from -90 to 90. */\n' +
|
|
1135
|
+
' latitude: number;\n' +
|
|
1136
|
+
' /** Degrees longitude, from -180 to 180. */\n' +
|
|
1137
|
+
' longitude: number;\n' +
|
|
1138
|
+
' /** Elevation in metres above sea level. */\n' +
|
|
1139
|
+
' altitude?: number;\n' +
|
|
1140
|
+
' /** Human-readable name for this location. */\n' +
|
|
1141
|
+
' label?: string;\n' +
|
|
1142
|
+
'};',
|
|
1143
|
+
)
|
|
1144
|
+
})
|
|
1145
|
+
|
|
1146
|
+
it('wraps union item types in parentheses for root-level array schema', () => {
|
|
1147
|
+
const schema: JSONSchema = {
|
|
1148
|
+
type: 'array',
|
|
1149
|
+
items: { anyOf: [{ $ref: '#/$defs/parameter' }, { $ref: '#/$defs/reference' }] },
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
const result = generateTypeDefinition(schema, 'Parameters')
|
|
1153
|
+
|
|
1154
|
+
expect(result).toBe('export type Parameters = (ParameterObject | ReferenceObject)[];')
|
|
1155
|
+
})
|
|
1156
|
+
|
|
1157
|
+
it('emits JSDoc for non-object schemas with a $comment URL', () => {
|
|
1158
|
+
const schema: JSONSchema = {
|
|
1159
|
+
$comment: 'https://spec.openapis.org/oas/v3.1#contact-object',
|
|
1160
|
+
type: 'array',
|
|
1161
|
+
items: { $ref: '#/$defs/server' },
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
const result = generateTypeDefinition(schema, 'Contacts')
|
|
1165
|
+
|
|
1166
|
+
expect(result).toContain('/**')
|
|
1167
|
+
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#contact-object')
|
|
1168
|
+
expect(result).toContain('export type Contacts = ServerObject[];')
|
|
1169
|
+
})
|
|
1170
|
+
|
|
1171
|
+
it('emits JSDoc for non-object schemas with a plain-text $comment', () => {
|
|
1172
|
+
const schema: JSONSchema = {
|
|
1173
|
+
$comment: 'A list of parameters applicable to the operation.',
|
|
1174
|
+
type: 'array',
|
|
1175
|
+
items: { $ref: '#/$defs/parameter' },
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
const result = generateTypeDefinition(schema, 'Parameters')
|
|
1179
|
+
|
|
1180
|
+
expect(result).toContain('/**')
|
|
1181
|
+
expect(result).toContain('A list of parameters applicable to the operation.')
|
|
1182
|
+
expect(result).toContain('export type Parameters = ParameterObject[];')
|
|
1183
|
+
})
|
|
1184
|
+
|
|
1185
|
+
it('generates unknown for external $ref', () => {
|
|
1186
|
+
// External refs (e.g. from draft-04 schemas) cannot be resolved locally — treated as unknown.
|
|
1187
|
+
const schema: JSONSchema = {
|
|
1188
|
+
$ref: 'http://json-schema.org/draft-04/schema#/properties/maximum',
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
const result = generateTypeDefinition(schema, 'MaximumObject')
|
|
1192
|
+
|
|
1193
|
+
expect(result).toBe('export type MaximumObject = unknown;')
|
|
1194
|
+
})
|
|
1195
|
+
|
|
1196
|
+
it('does not emit a trailing blank line in JSDoc when there is no @see link', () => {
|
|
1197
|
+
const schema: JSONSchema = {
|
|
1198
|
+
$comment: 'A plain-text description with no URL.',
|
|
1199
|
+
type: 'object',
|
|
1200
|
+
properties: {
|
|
1201
|
+
value: { type: 'string' },
|
|
1202
|
+
},
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
const result = generateTypeDefinition(schema, 'PlainCommentObject')
|
|
1206
|
+
|
|
1207
|
+
expect(result).toMatch(/\* A plain-text description with no URL\.\n\*\//)
|
|
1208
|
+
expect(result).not.toContain('* \n*/')
|
|
1209
|
+
})
|
|
1210
|
+
})
|