@kubb/adapter-oas 5.0.0-alpha.34 → 5.0.0-alpha.36
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/index.cjs +109 -99
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +104 -94
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/adapter.ts +5 -7
- package/src/constants.ts +7 -8
- package/src/discriminator.ts +15 -16
- package/src/parser.ts +105 -131
- package/src/resolvers.ts +6 -7
- package/src/types.ts +6 -7
package/src/parser.ts
CHANGED
|
@@ -1,38 +1,5 @@
|
|
|
1
1
|
import { URLPath } from '@internals/utils'
|
|
2
|
-
import {
|
|
3
|
-
childName,
|
|
4
|
-
createDiscriminantNode,
|
|
5
|
-
createInput,
|
|
6
|
-
createOperation,
|
|
7
|
-
createParameter,
|
|
8
|
-
createProperty,
|
|
9
|
-
createResponse,
|
|
10
|
-
createSchema,
|
|
11
|
-
enumPropName,
|
|
12
|
-
extractRefName,
|
|
13
|
-
findDiscriminator,
|
|
14
|
-
mergeAdjacentObjects,
|
|
15
|
-
narrowSchema,
|
|
16
|
-
type ParserOptions,
|
|
17
|
-
setDiscriminatorEnum,
|
|
18
|
-
setEnumName,
|
|
19
|
-
simplifyUnion,
|
|
20
|
-
syncOptionality,
|
|
21
|
-
} from '@kubb/ast'
|
|
22
|
-
import type {
|
|
23
|
-
DistributiveOmit,
|
|
24
|
-
HttpMethod,
|
|
25
|
-
InputNode,
|
|
26
|
-
OperationNode,
|
|
27
|
-
ParameterLocation,
|
|
28
|
-
ParameterNode,
|
|
29
|
-
PrimitiveSchemaType,
|
|
30
|
-
PropertyNode,
|
|
31
|
-
ResponseNode,
|
|
32
|
-
ScalarSchemaType,
|
|
33
|
-
SchemaNode,
|
|
34
|
-
StatusCode,
|
|
35
|
-
} from '@kubb/ast/types'
|
|
2
|
+
import { ast } from '@kubb/core'
|
|
36
3
|
import BaseOas from 'oas'
|
|
37
4
|
import { DEFAULT_PARSER_OPTIONS, enumExtensionKeys, typeOptionMap } from './constants.ts'
|
|
38
5
|
import { isDiscriminator, isNullable, isReference } from './guards.ts'
|
|
@@ -75,8 +42,8 @@ type SchemaContext = {
|
|
|
75
42
|
* Normalized single type string (first element when OAS 3.1 multi-type array).
|
|
76
43
|
*/
|
|
77
44
|
type: string | undefined
|
|
78
|
-
rawOptions: Partial<ParserOptions> | undefined
|
|
79
|
-
options: ParserOptions
|
|
45
|
+
rawOptions: Partial<ast.ParserOptions> | undefined
|
|
46
|
+
options: ast.ParserOptions
|
|
80
47
|
}
|
|
81
48
|
|
|
82
49
|
/**
|
|
@@ -117,8 +84,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
117
84
|
* Use `syncSchemaRef(node)` in printers to get a merged view of both.
|
|
118
85
|
* Circular refs are detected via `resolvingRefs` and leave `schema` as `undefined`.
|
|
119
86
|
*/
|
|
120
|
-
function convertRef({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): SchemaNode {
|
|
121
|
-
let resolvedSchema: SchemaNode | undefined
|
|
87
|
+
function convertRef({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): ast.SchemaNode {
|
|
88
|
+
let resolvedSchema: ast.SchemaNode | undefined
|
|
122
89
|
const refPath = schema.$ref
|
|
123
90
|
if (refPath && !resolvingRefs.has(refPath)) {
|
|
124
91
|
try {
|
|
@@ -133,10 +100,10 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
133
100
|
}
|
|
134
101
|
}
|
|
135
102
|
|
|
136
|
-
return createSchema({
|
|
103
|
+
return ast.createSchema({
|
|
137
104
|
...buildSchemaNode(schema, name, nullable, defaultValue),
|
|
138
105
|
type: 'ref',
|
|
139
|
-
name: extractRefName(schema.$ref!),
|
|
106
|
+
name: ast.extractRefName(schema.$ref!),
|
|
140
107
|
ref: schema.$ref,
|
|
141
108
|
schema: resolvedSchema,
|
|
142
109
|
})
|
|
@@ -145,7 +112,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
145
112
|
/**
|
|
146
113
|
* Converts an `allOf` schema into a flattened node or an `IntersectionSchemaNode`.
|
|
147
114
|
*/
|
|
148
|
-
function convertAllOf({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): SchemaNode {
|
|
115
|
+
function convertAllOf({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): ast.SchemaNode {
|
|
149
116
|
if (
|
|
150
117
|
schema.allOf!.length === 1 &&
|
|
151
118
|
!schema.properties &&
|
|
@@ -158,7 +125,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
158
125
|
const mergedNullable = nullable || memberNode.nullable || undefined
|
|
159
126
|
const mergedDefault = schema.default === null && mergedNullable ? undefined : (schema.default ?? memberNode.default)
|
|
160
127
|
|
|
161
|
-
return createSchema({
|
|
128
|
+
return ast.createSchema({
|
|
162
129
|
...memberNodeProps,
|
|
163
130
|
name,
|
|
164
131
|
title: schema.title ?? memberNode.title,
|
|
@@ -170,11 +137,11 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
170
137
|
default: mergedDefault,
|
|
171
138
|
example: schema.example ?? memberNode.example,
|
|
172
139
|
pattern: schema.pattern ?? ('pattern' in memberNode ? memberNode.pattern : undefined),
|
|
173
|
-
} as DistributiveOmit<SchemaNode, 'kind'>)
|
|
140
|
+
} as ast.DistributiveOmit<ast.SchemaNode, 'kind'>)
|
|
174
141
|
}
|
|
175
142
|
|
|
176
143
|
const filteredDiscriminantValues: Array<{ propertyName: string; value: string }> = []
|
|
177
|
-
const allOfMembers: Array<SchemaNode> = (schema.allOf as Array<SchemaObject | ReferenceObject>)
|
|
144
|
+
const allOfMembers: Array<ast.SchemaNode> = (schema.allOf as Array<SchemaObject | ReferenceObject>)
|
|
178
145
|
.filter((item) => {
|
|
179
146
|
if (!isReference(item) || !name) return true
|
|
180
147
|
const deref = resolveRef<SchemaObject>(document, item.$ref)
|
|
@@ -185,7 +152,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
185
152
|
const inOneOf = parentUnion.some((oneOfItem) => isReference(oneOfItem) && oneOfItem.$ref === childRef)
|
|
186
153
|
const inMapping = Object.values(deref.discriminator.mapping ?? {}).some((v) => v === childRef)
|
|
187
154
|
if (inOneOf || inMapping) {
|
|
188
|
-
const discriminatorValue = findDiscriminator(deref.discriminator.mapping, childRef)
|
|
155
|
+
const discriminatorValue = ast.findDiscriminator(deref.discriminator.mapping, childRef)
|
|
189
156
|
if (discriminatorValue) {
|
|
190
157
|
filteredDiscriminantValues.push({ propertyName: deref.discriminator.propertyName, value: discriminatorValue })
|
|
191
158
|
}
|
|
@@ -225,12 +192,12 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
225
192
|
}
|
|
226
193
|
|
|
227
194
|
for (const { propertyName, value } of filteredDiscriminantValues) {
|
|
228
|
-
allOfMembers.push(createDiscriminantNode({ propertyName, value }))
|
|
195
|
+
allOfMembers.push(ast.createDiscriminantNode({ propertyName, value }))
|
|
229
196
|
}
|
|
230
197
|
|
|
231
|
-
return createSchema({
|
|
198
|
+
return ast.createSchema({
|
|
232
199
|
type: 'intersection',
|
|
233
|
-
members: [...mergeAdjacentObjects(allOfMembers.slice(0, syntheticStart)), ...mergeAdjacentObjects(allOfMembers.slice(syntheticStart))],
|
|
200
|
+
members: [...ast.mergeAdjacentObjects(allOfMembers.slice(0, syntheticStart)), ...ast.mergeAdjacentObjects(allOfMembers.slice(syntheticStart))],
|
|
234
201
|
...buildSchemaNode(schema, name, nullable, defaultValue),
|
|
235
202
|
})
|
|
236
203
|
}
|
|
@@ -238,16 +205,16 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
238
205
|
/**
|
|
239
206
|
* Converts a `oneOf` / `anyOf` schema into a `UnionSchemaNode`.
|
|
240
207
|
*/
|
|
241
|
-
function convertUnion({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): SchemaNode {
|
|
242
|
-
function pickDiscriminatorPropertyNode(node: SchemaNode, propertyName: string): SchemaNode | null {
|
|
243
|
-
const objectNode = narrowSchema(node, 'object')
|
|
208
|
+
function convertUnion({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): ast.SchemaNode {
|
|
209
|
+
function pickDiscriminatorPropertyNode(node: ast.SchemaNode, propertyName: string): ast.SchemaNode | null {
|
|
210
|
+
const objectNode = ast.narrowSchema(node, 'object')
|
|
244
211
|
const discriminatorProperty = objectNode?.properties?.find((property) => property.name === propertyName)
|
|
245
212
|
|
|
246
213
|
if (!discriminatorProperty) {
|
|
247
214
|
return null
|
|
248
215
|
}
|
|
249
216
|
|
|
250
|
-
return createSchema({
|
|
217
|
+
return ast.createSchema({
|
|
251
218
|
type: 'object',
|
|
252
219
|
primitive: 'object',
|
|
253
220
|
properties: [discriminatorProperty],
|
|
@@ -273,7 +240,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
273
240
|
if (sharedPropertiesNode || discriminator?.mapping) {
|
|
274
241
|
const members = unionMembers.map((s) => {
|
|
275
242
|
const ref = isReference(s) ? s.$ref : undefined
|
|
276
|
-
const discriminatorValue = findDiscriminator(discriminator?.mapping, ref)
|
|
243
|
+
const discriminatorValue = ast.findDiscriminator(discriminator?.mapping, ref)
|
|
277
244
|
const memberNode = parseSchema({ schema: s as SchemaObject }, rawOptions)
|
|
278
245
|
|
|
279
246
|
if (!discriminatorValue || !discriminator) {
|
|
@@ -282,7 +249,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
282
249
|
|
|
283
250
|
const narrowedDiscriminatorNode = sharedPropertiesNode
|
|
284
251
|
? pickDiscriminatorPropertyNode(
|
|
285
|
-
setDiscriminatorEnum({
|
|
252
|
+
ast.setDiscriminatorEnum({
|
|
286
253
|
node: sharedPropertiesNode,
|
|
287
254
|
propertyName: discriminator.propertyName,
|
|
288
255
|
values: [discriminatorValue],
|
|
@@ -291,13 +258,16 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
291
258
|
)
|
|
292
259
|
: undefined
|
|
293
260
|
|
|
294
|
-
return createSchema({
|
|
261
|
+
return ast.createSchema({
|
|
295
262
|
type: 'intersection',
|
|
296
|
-
members: [
|
|
263
|
+
members: [
|
|
264
|
+
memberNode,
|
|
265
|
+
narrowedDiscriminatorNode ?? ast.createDiscriminantNode({ propertyName: discriminator.propertyName, value: discriminatorValue }),
|
|
266
|
+
],
|
|
297
267
|
})
|
|
298
268
|
})
|
|
299
269
|
|
|
300
|
-
const unionNode = createSchema({
|
|
270
|
+
const unionNode = ast.createSchema({
|
|
301
271
|
type: 'union',
|
|
302
272
|
...unionBase,
|
|
303
273
|
members,
|
|
@@ -307,28 +277,28 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
307
277
|
return unionNode
|
|
308
278
|
}
|
|
309
279
|
|
|
310
|
-
return createSchema({
|
|
280
|
+
return ast.createSchema({
|
|
311
281
|
type: 'intersection',
|
|
312
282
|
...buildSchemaNode(schema, name, nullable, defaultValue),
|
|
313
283
|
members: [unionNode, sharedPropertiesNode],
|
|
314
284
|
})
|
|
315
285
|
}
|
|
316
286
|
|
|
317
|
-
return createSchema({
|
|
287
|
+
return ast.createSchema({
|
|
318
288
|
type: 'union',
|
|
319
289
|
...unionBase,
|
|
320
|
-
members: simplifyUnion(unionMembers.map((s) => parseSchema({ schema: s as SchemaObject }, rawOptions))),
|
|
290
|
+
members: ast.simplifyUnion(unionMembers.map((s) => parseSchema({ schema: s as SchemaObject }, rawOptions))),
|
|
321
291
|
})
|
|
322
292
|
}
|
|
323
293
|
|
|
324
294
|
/**
|
|
325
295
|
* Converts an OAS 3.1 `const` schema into a null scalar or a single-value `EnumSchemaNode`.
|
|
326
296
|
*/
|
|
327
|
-
function convertConst({ schema, name, nullable, defaultValue }: SchemaContext): SchemaNode {
|
|
297
|
+
function convertConst({ schema, name, nullable, defaultValue }: SchemaContext): ast.SchemaNode {
|
|
328
298
|
const constValue = schema.const
|
|
329
299
|
|
|
330
300
|
if (constValue === null) {
|
|
331
|
-
return createSchema({
|
|
301
|
+
return ast.createSchema({
|
|
332
302
|
type: 'null',
|
|
333
303
|
primitive: 'null',
|
|
334
304
|
name,
|
|
@@ -339,7 +309,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
339
309
|
}
|
|
340
310
|
|
|
341
311
|
const constPrimitive = getPrimitiveType(typeof constValue === 'number' ? 'number' : typeof constValue === 'boolean' ? 'boolean' : 'string')
|
|
342
|
-
return createSchema({
|
|
312
|
+
return ast.createSchema({
|
|
343
313
|
type: 'enum',
|
|
344
314
|
primitive: constPrimitive,
|
|
345
315
|
enumValues: [constValue as string | number | boolean],
|
|
@@ -351,11 +321,11 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
351
321
|
* Converts a format-annotated schema into a special-type `SchemaNode`.
|
|
352
322
|
* Returns `null` when the format should fall through to string handling (`dateType: false`).
|
|
353
323
|
*/
|
|
354
|
-
function convertFormat({ schema, name, nullable, defaultValue, options }: SchemaContext): SchemaNode | null {
|
|
324
|
+
function convertFormat({ schema, name, nullable, defaultValue, options }: SchemaContext): ast.SchemaNode | null {
|
|
355
325
|
const base = buildSchemaNode(schema, name, nullable, defaultValue)
|
|
356
326
|
|
|
357
327
|
if (schema.format === 'int64') {
|
|
358
|
-
return createSchema({
|
|
328
|
+
return ast.createSchema({
|
|
359
329
|
type: options.integerType === 'bigint' ? 'bigint' : 'integer',
|
|
360
330
|
primitive: 'integer',
|
|
361
331
|
...base,
|
|
@@ -371,39 +341,39 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
371
341
|
if (!dateType) return null
|
|
372
342
|
|
|
373
343
|
if (dateType.type === 'datetime') {
|
|
374
|
-
return createSchema({ ...base, primitive: 'string' as const, type: 'datetime', offset: dateType.offset, local: dateType.local })
|
|
344
|
+
return ast.createSchema({ ...base, primitive: 'string' as const, type: 'datetime', offset: dateType.offset, local: dateType.local })
|
|
375
345
|
}
|
|
376
|
-
return createSchema({ ...base, primitive: 'string' as const, type: dateType.type, representation: dateType.representation })
|
|
346
|
+
return ast.createSchema({ ...base, primitive: 'string' as const, type: dateType.type, representation: dateType.representation })
|
|
377
347
|
}
|
|
378
348
|
|
|
379
349
|
const specialType = getSchemaType(schema.format!)
|
|
380
350
|
if (!specialType) return null
|
|
381
351
|
|
|
382
|
-
const specialPrimitive: PrimitiveSchemaType = specialType === 'number' || specialType === 'integer' || specialType === 'bigint' ? specialType : 'string'
|
|
352
|
+
const specialPrimitive: ast.PrimitiveSchemaType = specialType === 'number' || specialType === 'integer' || specialType === 'bigint' ? specialType : 'string'
|
|
383
353
|
|
|
384
354
|
if (specialType === 'number' || specialType === 'integer' || specialType === 'bigint') {
|
|
385
|
-
return createSchema({ ...base, primitive: specialPrimitive, type: specialType })
|
|
355
|
+
return ast.createSchema({ ...base, primitive: specialPrimitive, type: specialType })
|
|
386
356
|
}
|
|
387
357
|
if (specialType === 'url') {
|
|
388
|
-
return createSchema({ ...base, primitive: 'string' as const, type: 'url', min: schema.minLength, max: schema.maxLength })
|
|
358
|
+
return ast.createSchema({ ...base, primitive: 'string' as const, type: 'url', min: schema.minLength, max: schema.maxLength })
|
|
389
359
|
}
|
|
390
360
|
if (specialType === 'ipv4') {
|
|
391
|
-
return createSchema({ ...base, primitive: 'string' as const, type: 'ipv4' })
|
|
361
|
+
return ast.createSchema({ ...base, primitive: 'string' as const, type: 'ipv4' })
|
|
392
362
|
}
|
|
393
363
|
if (specialType === 'ipv6') {
|
|
394
|
-
return createSchema({ ...base, primitive: 'string' as const, type: 'ipv6' })
|
|
364
|
+
return ast.createSchema({ ...base, primitive: 'string' as const, type: 'ipv6' })
|
|
395
365
|
}
|
|
396
366
|
if (specialType === 'uuid' || specialType === 'email') {
|
|
397
|
-
return createSchema({ ...base, primitive: 'string' as const, type: specialType, min: schema.minLength, max: schema.maxLength })
|
|
367
|
+
return ast.createSchema({ ...base, primitive: 'string' as const, type: specialType, min: schema.minLength, max: schema.maxLength })
|
|
398
368
|
}
|
|
399
369
|
|
|
400
|
-
return createSchema({ ...base, primitive: specialPrimitive, type: specialType as ScalarSchemaType })
|
|
370
|
+
return ast.createSchema({ ...base, primitive: specialPrimitive, type: specialType as ast.ScalarSchemaType })
|
|
401
371
|
}
|
|
402
372
|
|
|
403
373
|
/**
|
|
404
374
|
* Converts an `enum` schema into an `EnumSchemaNode`.
|
|
405
375
|
*/
|
|
406
|
-
function convertEnum({ schema, name, nullable, type, rawOptions }: SchemaContext): SchemaNode {
|
|
376
|
+
function convertEnum({ schema, name, nullable, type, rawOptions }: SchemaContext): ast.SchemaNode {
|
|
407
377
|
if (type === 'array') {
|
|
408
378
|
return parseSchema({ schema: normalizeArrayEnum(schema), name }, rawOptions)
|
|
409
379
|
}
|
|
@@ -438,7 +408,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
438
408
|
? [...new Set((schema as Record<string, unknown>)[extensionKey] as Array<string | number>)]
|
|
439
409
|
: [...new Set(filteredValues)]
|
|
440
410
|
|
|
441
|
-
return createSchema({
|
|
411
|
+
return ast.createSchema({
|
|
442
412
|
...enumBase,
|
|
443
413
|
primitive: enumPrimitiveType,
|
|
444
414
|
namedEnumValues: sourceValues.map((label, index) => ({
|
|
@@ -449,7 +419,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
449
419
|
})
|
|
450
420
|
}
|
|
451
421
|
|
|
452
|
-
return createSchema({
|
|
422
|
+
return ast.createSchema({
|
|
453
423
|
...enumBase,
|
|
454
424
|
enumValues: [...new Set(filteredValues)],
|
|
455
425
|
})
|
|
@@ -458,26 +428,26 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
458
428
|
/**
|
|
459
429
|
* Converts an object-like schema into an `ObjectSchemaNode`.
|
|
460
430
|
*/
|
|
461
|
-
function convertObject({ schema, name, nullable, defaultValue, rawOptions, options }: SchemaContext): SchemaNode {
|
|
462
|
-
const properties: Array<PropertyNode> = schema.properties
|
|
431
|
+
function convertObject({ schema, name, nullable, defaultValue, rawOptions, options }: SchemaContext): ast.SchemaNode {
|
|
432
|
+
const properties: Array<ast.PropertyNode> = schema.properties
|
|
463
433
|
? Object.entries(schema.properties).map(([propName, propSchema]) => {
|
|
464
434
|
const required = Array.isArray(schema.required) ? schema.required.includes(propName) : !!schema.required
|
|
465
435
|
const resolvedPropSchema = propSchema as SchemaObject
|
|
466
436
|
const propNullable = isNullable(resolvedPropSchema)
|
|
467
437
|
|
|
468
|
-
const resolvedChildName = childName(name, propName)
|
|
438
|
+
const resolvedChildName = ast.childName(name, propName)
|
|
469
439
|
const propNode = parseSchema({ schema: resolvedPropSchema, name: resolvedChildName }, rawOptions)
|
|
470
|
-
let schemaNode = setEnumName(propNode, name, propName, options.enumSuffix)
|
|
440
|
+
let schemaNode = ast.setEnumName(propNode, name, propName, options.enumSuffix)
|
|
471
441
|
|
|
472
|
-
const tupleNode = narrowSchema(schemaNode, 'tuple')
|
|
442
|
+
const tupleNode = ast.narrowSchema(schemaNode, 'tuple')
|
|
473
443
|
if (tupleNode?.items) {
|
|
474
|
-
const namedItems = tupleNode.items.map((item) => setEnumName(item, name, propName, options.enumSuffix))
|
|
444
|
+
const namedItems = tupleNode.items.map((item) => ast.setEnumName(item, name, propName, options.enumSuffix))
|
|
475
445
|
if (namedItems.some((item, i) => item !== tupleNode.items![i])) {
|
|
476
446
|
schemaNode = { ...tupleNode, items: namedItems }
|
|
477
447
|
}
|
|
478
448
|
}
|
|
479
449
|
|
|
480
|
-
return createProperty({
|
|
450
|
+
return ast.createProperty({
|
|
481
451
|
name: propName,
|
|
482
452
|
schema: {
|
|
483
453
|
...schemaNode,
|
|
@@ -489,7 +459,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
489
459
|
: []
|
|
490
460
|
|
|
491
461
|
const additionalProperties = schema.additionalProperties
|
|
492
|
-
let additionalPropertiesNode: SchemaNode | boolean | undefined
|
|
462
|
+
let additionalPropertiesNode: ast.SchemaNode | boolean | undefined
|
|
493
463
|
if (additionalProperties === true) {
|
|
494
464
|
additionalPropertiesNode = true
|
|
495
465
|
} else if (additionalProperties && Object.keys(additionalProperties).length > 0) {
|
|
@@ -497,7 +467,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
497
467
|
} else if (additionalProperties === false) {
|
|
498
468
|
additionalPropertiesNode = false
|
|
499
469
|
} else if (additionalProperties) {
|
|
500
|
-
additionalPropertiesNode = createSchema({ type: typeOptionMap.get(options.unknownType)! })
|
|
470
|
+
additionalPropertiesNode = ast.createSchema({ type: typeOptionMap.get(options.unknownType)! })
|
|
501
471
|
}
|
|
502
472
|
|
|
503
473
|
const rawPatternProperties = 'patternProperties' in schema ? schema.patternProperties : undefined
|
|
@@ -507,13 +477,13 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
507
477
|
Object.entries(rawPatternProperties).map(([pattern, patternSchema]) => [
|
|
508
478
|
pattern,
|
|
509
479
|
patternSchema === true || (typeof patternSchema === 'object' && Object.keys(patternSchema).length === 0)
|
|
510
|
-
? createSchema({ type: typeOptionMap.get(options.unknownType)! })
|
|
480
|
+
? ast.createSchema({ type: typeOptionMap.get(options.unknownType)! })
|
|
511
481
|
: parseSchema({ schema: patternSchema as SchemaObject }, rawOptions),
|
|
512
482
|
]),
|
|
513
483
|
)
|
|
514
484
|
: undefined
|
|
515
485
|
|
|
516
|
-
const objectNode: SchemaNode = createSchema({
|
|
486
|
+
const objectNode: ast.SchemaNode = ast.createSchema({
|
|
517
487
|
type: 'object',
|
|
518
488
|
primitive: 'object',
|
|
519
489
|
properties,
|
|
@@ -527,8 +497,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
527
497
|
if (isDiscriminator(schema) && schema.discriminator.mapping) {
|
|
528
498
|
const discPropName = schema.discriminator.propertyName
|
|
529
499
|
const values = Object.keys(schema.discriminator.mapping)
|
|
530
|
-
const enumName = name ? enumPropName(name, discPropName, options.enumSuffix) : undefined
|
|
531
|
-
return setDiscriminatorEnum({ node: objectNode, propertyName: discPropName, values, enumName })
|
|
500
|
+
const enumName = name ? ast.enumPropName(name, discPropName, options.enumSuffix) : undefined
|
|
501
|
+
return ast.setDiscriminatorEnum({ node: objectNode, propertyName: discPropName, values, enumName })
|
|
532
502
|
}
|
|
533
503
|
|
|
534
504
|
return objectNode
|
|
@@ -537,11 +507,11 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
537
507
|
/**
|
|
538
508
|
* Converts an OAS 3.1 `prefixItems` tuple into a `TupleSchemaNode`.
|
|
539
509
|
*/
|
|
540
|
-
function convertTuple({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): SchemaNode {
|
|
510
|
+
function convertTuple({ schema, name, nullable, defaultValue, rawOptions }: SchemaContext): ast.SchemaNode {
|
|
541
511
|
const tupleItems = (schema.prefixItems ?? []).map((item) => parseSchema({ schema: item as SchemaObject }, rawOptions))
|
|
542
|
-
const rest = schema.items ? parseSchema({ schema: schema.items as SchemaObject }, rawOptions) : createSchema({ type: 'any' })
|
|
512
|
+
const rest = schema.items ? parseSchema({ schema: schema.items as SchemaObject }, rawOptions) : ast.createSchema({ type: 'any' })
|
|
543
513
|
|
|
544
|
-
return createSchema({
|
|
514
|
+
return ast.createSchema({
|
|
545
515
|
type: 'tuple',
|
|
546
516
|
primitive: 'array',
|
|
547
517
|
items: tupleItems,
|
|
@@ -555,12 +525,12 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
555
525
|
/**
|
|
556
526
|
* Converts a `type: 'array'` schema into an `ArraySchemaNode`.
|
|
557
527
|
*/
|
|
558
|
-
function convertArray({ schema, name, nullable, defaultValue, rawOptions, options }: SchemaContext): SchemaNode {
|
|
528
|
+
function convertArray({ schema, name, nullable, defaultValue, rawOptions, options }: SchemaContext): ast.SchemaNode {
|
|
559
529
|
const rawItems = schema.items as SchemaObject | undefined
|
|
560
|
-
const itemName = rawItems?.enum?.length && name ? enumPropName(undefined, name, options.enumSuffix) : undefined
|
|
530
|
+
const itemName = rawItems?.enum?.length && name ? ast.enumPropName(undefined, name, options.enumSuffix) : undefined
|
|
561
531
|
const items = rawItems ? [parseSchema({ schema: rawItems, name: itemName }, rawOptions)] : []
|
|
562
532
|
|
|
563
|
-
return createSchema({
|
|
533
|
+
return ast.createSchema({
|
|
564
534
|
type: 'array',
|
|
565
535
|
primitive: 'array',
|
|
566
536
|
items,
|
|
@@ -574,8 +544,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
574
544
|
/**
|
|
575
545
|
* Converts a `type: 'string'` schema into a `StringSchemaNode`.
|
|
576
546
|
*/
|
|
577
|
-
function convertString({ schema, name, nullable, defaultValue }: SchemaContext): SchemaNode {
|
|
578
|
-
return createSchema({
|
|
547
|
+
function convertString({ schema, name, nullable, defaultValue }: SchemaContext): ast.SchemaNode {
|
|
548
|
+
return ast.createSchema({
|
|
579
549
|
type: 'string',
|
|
580
550
|
primitive: 'string',
|
|
581
551
|
min: schema.minLength,
|
|
@@ -588,8 +558,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
588
558
|
/**
|
|
589
559
|
* Converts a `type: 'number'` or `type: 'integer'` schema.
|
|
590
560
|
*/
|
|
591
|
-
function convertNumeric({ schema, name, nullable, defaultValue }: SchemaContext, type: 'number' | 'integer'): SchemaNode {
|
|
592
|
-
return createSchema({
|
|
561
|
+
function convertNumeric({ schema, name, nullable, defaultValue }: SchemaContext, type: 'number' | 'integer'): ast.SchemaNode {
|
|
562
|
+
return ast.createSchema({
|
|
593
563
|
type,
|
|
594
564
|
primitive: type,
|
|
595
565
|
min: schema.minimum,
|
|
@@ -604,8 +574,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
604
574
|
/**
|
|
605
575
|
* Converts a `type: 'boolean'` schema.
|
|
606
576
|
*/
|
|
607
|
-
function convertBoolean({ schema, name, nullable, defaultValue }: SchemaContext): SchemaNode {
|
|
608
|
-
return createSchema({
|
|
577
|
+
function convertBoolean({ schema, name, nullable, defaultValue }: SchemaContext): ast.SchemaNode {
|
|
578
|
+
return ast.createSchema({
|
|
609
579
|
type: 'boolean',
|
|
610
580
|
primitive: 'boolean',
|
|
611
581
|
...buildSchemaNode(schema, name, nullable, defaultValue),
|
|
@@ -615,8 +585,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
615
585
|
/**
|
|
616
586
|
* Converts an explicit `type: 'null'` schema.
|
|
617
587
|
*/
|
|
618
|
-
function convertNull({ schema, name, nullable }: SchemaContext): SchemaNode {
|
|
619
|
-
return createSchema({
|
|
588
|
+
function convertNull({ schema, name, nullable }: SchemaContext): ast.SchemaNode {
|
|
589
|
+
return ast.createSchema({
|
|
620
590
|
type: 'null',
|
|
621
591
|
primitive: 'null',
|
|
622
592
|
name,
|
|
@@ -634,8 +604,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
634
604
|
* → octet-stream blob → multi-type array → constraint-inferred type → `enum` → object/array/tuple/scalar
|
|
635
605
|
* → empty-schema fallback (`emptySchemaType` option).
|
|
636
606
|
*/
|
|
637
|
-
function parseSchema({ schema, name }: { schema: SchemaObject; name?: string | null }, rawOptions?: Partial<ParserOptions>): SchemaNode {
|
|
638
|
-
const options: ParserOptions = { ...DEFAULT_PARSER_OPTIONS, ...rawOptions }
|
|
607
|
+
function parseSchema({ schema, name }: { schema: SchemaObject; name?: string | null }, rawOptions?: Partial<ast.ParserOptions>): ast.SchemaNode {
|
|
608
|
+
const options: ast.ParserOptions = { ...DEFAULT_PARSER_OPTIONS, ...rawOptions }
|
|
639
609
|
const flattenedSchema = flattenSchema(schema)
|
|
640
610
|
if (flattenedSchema && flattenedSchema !== schema) {
|
|
641
611
|
return parseSchema({ schema: flattenedSchema, name }, rawOptions)
|
|
@@ -661,7 +631,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
661
631
|
}
|
|
662
632
|
|
|
663
633
|
if (schema.type === 'string' && schema.contentMediaType === 'application/octet-stream') {
|
|
664
|
-
return createSchema({ type: 'blob', primitive: 'string', ...buildSchemaNode(schema, name, nullable, defaultValue) })
|
|
634
|
+
return ast.createSchema({ type: 'blob', primitive: 'string', ...buildSchemaNode(schema, name, nullable, defaultValue) })
|
|
665
635
|
}
|
|
666
636
|
|
|
667
637
|
if (Array.isArray(schema.type) && schema.type.length > 1) {
|
|
@@ -669,7 +639,7 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
669
639
|
const arrayNullable = schema.type.includes('null') || nullable || undefined
|
|
670
640
|
|
|
671
641
|
if (nonNullTypes.length > 1) {
|
|
672
|
-
return createSchema({
|
|
642
|
+
return ast.createSchema({
|
|
673
643
|
type: 'union',
|
|
674
644
|
members: nonNullTypes.map((t) => parseSchema({ schema: { ...schema, type: t } as SchemaObject, name }, rawOptions)),
|
|
675
645
|
...buildSchemaNode(schema, name, arrayNullable, defaultValue),
|
|
@@ -697,22 +667,22 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
697
667
|
if (type === 'null') return convertNull(ctx)
|
|
698
668
|
|
|
699
669
|
const emptyType = typeOptionMap.get(options.emptySchemaType)!
|
|
700
|
-
return createSchema({ type: emptyType as ScalarSchemaType, name, title: schema.title, description: schema.description })
|
|
670
|
+
return ast.createSchema({ type: emptyType as ast.ScalarSchemaType, name, title: schema.title, description: schema.description })
|
|
701
671
|
}
|
|
702
672
|
|
|
703
673
|
/**
|
|
704
674
|
* Converts a dereferenced OAS parameter object into a `ParameterNode`.
|
|
705
675
|
*/
|
|
706
|
-
function parseParameter(options: ParserOptions, param: Record<string, unknown>): ParameterNode {
|
|
676
|
+
function parseParameter(options: ast.ParserOptions, param: Record<string, unknown>): ast.ParameterNode {
|
|
707
677
|
const required = (param['required'] as boolean | undefined) ?? false
|
|
708
678
|
|
|
709
|
-
const schema: SchemaNode = param['schema']
|
|
679
|
+
const schema: ast.SchemaNode = param['schema']
|
|
710
680
|
? parseSchema({ schema: param['schema'] as SchemaObject }, options)
|
|
711
|
-
: createSchema({ type: typeOptionMap.get(options.unknownType)! })
|
|
681
|
+
: ast.createSchema({ type: typeOptionMap.get(options.unknownType)! })
|
|
712
682
|
|
|
713
|
-
return createParameter({
|
|
683
|
+
return ast.createParameter({
|
|
714
684
|
name: param['name'] as string,
|
|
715
|
-
in: param['in'] as ParameterLocation,
|
|
685
|
+
in: param['in'] as ast.ParameterLocation,
|
|
716
686
|
schema: {
|
|
717
687
|
...schema,
|
|
718
688
|
description: (param['description'] as string | undefined) ?? schema.description,
|
|
@@ -724,8 +694,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
724
694
|
/**
|
|
725
695
|
* Converts an OAS `Operation` into an `OperationNode`.
|
|
726
696
|
*/
|
|
727
|
-
function parseOperation(options: ParserOptions, operation: Operation): OperationNode {
|
|
728
|
-
const parameters: Array<ParameterNode> = getParameters(document, operation).map((param) =>
|
|
697
|
+
function parseOperation(options: ast.ParserOptions, operation: Operation): ast.OperationNode {
|
|
698
|
+
const parameters: Array<ast.ParameterNode> = getParameters(document, operation).map((param) =>
|
|
729
699
|
parseParameter(options, param as unknown as Record<string, unknown>),
|
|
730
700
|
)
|
|
731
701
|
|
|
@@ -759,21 +729,21 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
759
729
|
const requestBody = requestBodySchemaNode
|
|
760
730
|
? {
|
|
761
731
|
description: requestBodyDescription,
|
|
762
|
-
schema: syncOptionality(requestBodySchemaNode, requestBodyRequired),
|
|
732
|
+
schema: ast.syncOptionality(requestBodySchemaNode, requestBodyRequired),
|
|
763
733
|
keysToOmit: requestBodyKeysToOmit?.length ? requestBodyKeysToOmit : undefined,
|
|
764
734
|
required: requestBodyRequired || undefined,
|
|
765
735
|
contentType: requestBodyContentType,
|
|
766
736
|
}
|
|
767
737
|
: undefined
|
|
768
738
|
|
|
769
|
-
const responses: Array<ResponseNode> = operation.getResponseStatusCodes().map((statusCode) => {
|
|
739
|
+
const responses: Array<ast.ResponseNode> = operation.getResponseStatusCodes().map((statusCode) => {
|
|
770
740
|
const responseObj = operation.getResponseByStatusCode(statusCode)
|
|
771
741
|
const responseSchema = getResponseSchema(document, operation, statusCode, { contentType: ctx.contentType })
|
|
772
742
|
|
|
773
743
|
const schema =
|
|
774
744
|
responseSchema && Object.keys(responseSchema).length > 0
|
|
775
745
|
? parseSchema({ schema: responseSchema }, options)
|
|
776
|
-
: createSchema({ type: typeOptionMap.get(options.emptySchemaType)! })
|
|
746
|
+
: ast.createSchema({ type: typeOptionMap.get(options.emptySchemaType)! })
|
|
777
747
|
|
|
778
748
|
const description = typeof responseObj === 'object' && responseObj !== null && !Array.isArray(responseObj) ? responseObj.description : undefined
|
|
779
749
|
|
|
@@ -790,8 +760,8 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
790
760
|
.map(([key]) => key)
|
|
791
761
|
: undefined
|
|
792
762
|
|
|
793
|
-
return createResponse({
|
|
794
|
-
statusCode: statusCode as StatusCode,
|
|
763
|
+
return ast.createResponse({
|
|
764
|
+
statusCode: statusCode as ast.StatusCode,
|
|
795
765
|
description,
|
|
796
766
|
schema,
|
|
797
767
|
mediaType,
|
|
@@ -801,9 +771,9 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
801
771
|
|
|
802
772
|
const urlPath = new URLPath(operation.path)
|
|
803
773
|
|
|
804
|
-
return createOperation({
|
|
774
|
+
return ast.createOperation({
|
|
805
775
|
operationId: operation.getOperationId(),
|
|
806
|
-
method: operation.method.toUpperCase() as HttpMethod,
|
|
776
|
+
method: operation.method.toUpperCase() as ast.HttpMethod,
|
|
807
777
|
path: urlPath.path,
|
|
808
778
|
tags: operation.getTags().map((tag) => tag.name),
|
|
809
779
|
summary: operation.getSummary() || undefined,
|
|
@@ -827,7 +797,11 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
827
797
|
* parseSchema(ctx, { schema: { type: 'string', format: 'uuid' } })
|
|
828
798
|
* ```
|
|
829
799
|
*/
|
|
830
|
-
export function parseSchema(
|
|
800
|
+
export function parseSchema(
|
|
801
|
+
ctx: OasParserContext,
|
|
802
|
+
{ schema, name }: { schema: SchemaObject; name?: string },
|
|
803
|
+
options?: Partial<ast.ParserOptions>,
|
|
804
|
+
): ast.SchemaNode {
|
|
831
805
|
return createSchemaParser(ctx).parseSchema({ schema, name }, options)
|
|
832
806
|
}
|
|
833
807
|
|
|
@@ -846,26 +820,26 @@ export function parseSchema(ctx: OasParserContext, { schema, name }: { schema: S
|
|
|
846
820
|
*/
|
|
847
821
|
export function parseOas(
|
|
848
822
|
document: Document,
|
|
849
|
-
options: Partial<ParserOptions> & { contentType?: ContentType } = {},
|
|
850
|
-
): { root: InputNode; nameMapping: Map<string, string> } {
|
|
823
|
+
options: Partial<ast.ParserOptions> & { contentType?: ContentType } = {},
|
|
824
|
+
): { root: ast.InputNode; nameMapping: Map<string, string> } {
|
|
851
825
|
const { contentType, ...parserOptions } = options
|
|
852
|
-
const mergedOptions: ParserOptions = { ...DEFAULT_PARSER_OPTIONS, ...parserOptions }
|
|
826
|
+
const mergedOptions: ast.ParserOptions = { ...DEFAULT_PARSER_OPTIONS, ...parserOptions }
|
|
853
827
|
|
|
854
828
|
const { schemas: schemaObjects, nameMapping } = getSchemas(document, { contentType })
|
|
855
829
|
const { parseSchema: _parseSchema, parseOperation: _parseOperation } = createSchemaParser({ document, contentType })
|
|
856
830
|
|
|
857
|
-
const schemas: Array<SchemaNode> = Object.entries(schemaObjects).map(([name, schema]) => _parseSchema({ schema, name }, mergedOptions))
|
|
831
|
+
const schemas: Array<ast.SchemaNode> = Object.entries(schemaObjects).map(([name, schema]) => _parseSchema({ schema, name }, mergedOptions))
|
|
858
832
|
|
|
859
833
|
const baseOas = new BaseOas(document)
|
|
860
834
|
const paths = baseOas.getPaths()
|
|
861
835
|
|
|
862
|
-
const operations: Array<OperationNode> = Object.entries(paths).flatMap(([_path, methods]) =>
|
|
836
|
+
const operations: Array<ast.OperationNode> = Object.entries(paths).flatMap(([_path, methods]) =>
|
|
863
837
|
Object.entries(methods)
|
|
864
838
|
.map(([, operation]) => (operation ? _parseOperation(mergedOptions, operation) : null))
|
|
865
|
-
.filter((op): op is OperationNode => op !== null),
|
|
839
|
+
.filter((op): op is ast.OperationNode => op !== null),
|
|
866
840
|
)
|
|
867
841
|
|
|
868
|
-
const root = createInput({ schemas, operations })
|
|
842
|
+
const root = ast.createInput({ schemas, operations })
|
|
869
843
|
|
|
870
844
|
return { root, nameMapping }
|
|
871
845
|
}
|
package/src/resolvers.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { pascalCase } from '@internals/utils'
|
|
2
|
-
import {
|
|
3
|
-
import type { MediaType, ParserOptions, PrimitiveSchemaType, SchemaType } from '@kubb/ast/types'
|
|
2
|
+
import { ast } from '@kubb/core'
|
|
4
3
|
import type { ParameterObject, ServerObject } from 'oas/types'
|
|
5
4
|
import { isRef } from 'oas/types'
|
|
6
5
|
import { matchesMimeType } from 'oas/utils'
|
|
@@ -51,7 +50,7 @@ export function resolveServerUrl(server: ServerObject, overrides?: Record<string
|
|
|
51
50
|
* Returns `undefined` for formats not in `formatMap` (e.g. `int64`, `date-time`),
|
|
52
51
|
* which are handled separately because their output depends on parser options.
|
|
53
52
|
*/
|
|
54
|
-
export function getSchemaType(format: string): SchemaType | null {
|
|
53
|
+
export function getSchemaType(format: string): ast.SchemaType | null {
|
|
55
54
|
return formatMap[format as keyof typeof formatMap] ?? null
|
|
56
55
|
}
|
|
57
56
|
|
|
@@ -60,7 +59,7 @@ export function getSchemaType(format: string): SchemaType | null {
|
|
|
60
59
|
* Numeric types (`number`, `integer`, `bigint`) are returned unchanged;
|
|
61
60
|
* `boolean` maps to `'boolean'`; everything else defaults to `'string'`.
|
|
62
61
|
*/
|
|
63
|
-
export function getPrimitiveType(type: string | undefined): PrimitiveSchemaType {
|
|
62
|
+
export function getPrimitiveType(type: string | undefined): ast.PrimitiveSchemaType {
|
|
64
63
|
if (type === 'number' || type === 'integer' || type === 'bigint') return type
|
|
65
64
|
if (type === 'boolean') return 'boolean'
|
|
66
65
|
|
|
@@ -71,8 +70,8 @@ export function getPrimitiveType(type: string | undefined): PrimitiveSchemaType
|
|
|
71
70
|
* Narrows a raw content-type string to the `MediaType` union recognized by Kubb.
|
|
72
71
|
* Returns `undefined` for content types not present in `KNOWN_MEDIA_TYPES`.
|
|
73
72
|
*/
|
|
74
|
-
export function getMediaType(contentType: string): MediaType | null {
|
|
75
|
-
return Object.values(mediaTypes).includes(contentType as MediaType) ? (contentType as MediaType) : null
|
|
73
|
+
export function getMediaType(contentType: string): ast.MediaType | null {
|
|
74
|
+
return Object.values(ast.mediaTypes).includes(contentType as ast.MediaType) ? (contentType as ast.MediaType) : null
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
export type OperationsOptions = {
|
|
@@ -444,7 +443,7 @@ export function getSchemas(document: Document, { contentType }: GetSchemasOption
|
|
|
444
443
|
* Returns `null` when `dateType: false`, signalling the format should fall through to `string`.
|
|
445
444
|
*/
|
|
446
445
|
export function getDateType(
|
|
447
|
-
options: ParserOptions,
|
|
446
|
+
options: ast.ParserOptions,
|
|
448
447
|
format: 'date-time' | 'date' | 'time',
|
|
449
448
|
): { type: 'datetime'; offset?: boolean; local?: boolean } | { type: 'date' | 'time'; representation: 'date' | 'string' } | null {
|
|
450
449
|
if (!options.dateType) {
|