@kubb/adapter-oas 5.0.0-beta.60 → 5.0.0-beta.61
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 +33 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +34 -25
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/parser.ts +28 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/adapter-oas",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.61",
|
|
4
4
|
"description": "OpenAPI and Swagger adapter for Kubb. Parses and validates OAS 2.0/3.x specifications into a @kubb/ast RootNode for downstream code generation plugins.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"adapter",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"api-ref-bundler": "0.5.1",
|
|
47
47
|
"swagger2openapi": "^7.0.8",
|
|
48
48
|
"yaml": "^2.9.0",
|
|
49
|
-
"@kubb/ast": "5.0.0-beta.
|
|
50
|
-
"@kubb/core": "5.0.0-beta.
|
|
49
|
+
"@kubb/ast": "5.0.0-beta.61",
|
|
50
|
+
"@kubb/core": "5.0.0-beta.61"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/json-schema": "^7.0.15",
|
package/src/parser.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { pascalCase } from '@internals/utils'
|
|
2
|
-
import {
|
|
2
|
+
import { macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion } from '@kubb/ast/macros'
|
|
3
|
+
import { childName, enumPropName, extractRefName, mergeAdjacentObjectsLazy } from '@kubb/ast/utils'
|
|
3
4
|
import { ast } from '@kubb/core'
|
|
4
5
|
import { DEFAULT_PARSER_OPTIONS, enumExtensionKeys, SCHEMA_REF_PREFIX, typeOptionMap } from './constants.ts'
|
|
5
6
|
import { oasDialect, type OasDialect } from './dialect.ts'
|
|
@@ -100,6 +101,23 @@ function normalizeArrayEnum(schema: SchemaObject): SchemaObject {
|
|
|
100
101
|
return { ...schemaWithoutEnum, items: normalizedItems } as SchemaObject
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Names the inline enums on a property's schema, and on each item when the property is a tuple, from
|
|
106
|
+
* the parent and property name. Wraps `macroEnumName` at the property construction site.
|
|
107
|
+
*/
|
|
108
|
+
function nameEnums(node: ast.SchemaNode, options: { parentName: string | null | undefined; propName: string; enumSuffix: string }): ast.SchemaNode {
|
|
109
|
+
const macro = macroEnumName(options)
|
|
110
|
+
const named = ast.applyMacros(node, [macro], { depth: 'shallow' })
|
|
111
|
+
const tupleNode = ast.narrowSchema(named, 'tuple')
|
|
112
|
+
if (tupleNode?.items) {
|
|
113
|
+
const namedItems = tupleNode.items.map((item) => ast.applyMacros(item, [macro], { depth: 'shallow' }))
|
|
114
|
+
if (namedItems.some((item, i) => item !== tupleNode.items![i])) {
|
|
115
|
+
return { ...tupleNode, items: namedItems }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return named
|
|
119
|
+
}
|
|
120
|
+
|
|
103
121
|
/**
|
|
104
122
|
* Factory function that creates schema and operation converters for a given OpenAPI context.
|
|
105
123
|
*
|
|
@@ -276,7 +294,7 @@ export function createSchemaParser(ctx: OasParserContext, dialect: OasDialect =
|
|
|
276
294
|
|
|
277
295
|
return ast.factory.createSchema({
|
|
278
296
|
type: 'intersection',
|
|
279
|
-
members: [...
|
|
297
|
+
members: [...mergeAdjacentObjectsLazy(allOfMembers.slice(0, syntheticStart)), ...mergeAdjacentObjectsLazy(allOfMembers.slice(syntheticStart))],
|
|
280
298
|
...buildSchemaNode(schema, name, nullable, defaultValue),
|
|
281
299
|
})
|
|
282
300
|
}
|
|
@@ -330,10 +348,8 @@ export function createSchemaParser(ctx: OasParserContext, dialect: OasDialect =
|
|
|
330
348
|
|
|
331
349
|
const narrowedDiscriminatorNode = sharedPropertiesNode
|
|
332
350
|
? pickDiscriminatorPropertyNode(
|
|
333
|
-
ast.
|
|
334
|
-
|
|
335
|
-
propertyName: discriminator.propertyName,
|
|
336
|
-
values: [discriminatorValue],
|
|
351
|
+
ast.applyMacros(sharedPropertiesNode, [macroDiscriminatorEnum({ propertyName: discriminator.propertyName, values: [discriminatorValue] })], {
|
|
352
|
+
depth: 'shallow',
|
|
337
353
|
}),
|
|
338
354
|
discriminator.propertyName,
|
|
339
355
|
)
|
|
@@ -369,11 +385,13 @@ export function createSchemaParser(ctx: OasParserContext, dialect: OasDialect =
|
|
|
369
385
|
})
|
|
370
386
|
}
|
|
371
387
|
|
|
372
|
-
|
|
388
|
+
const unionNode = ast.factory.createSchema({
|
|
373
389
|
type: 'union',
|
|
374
390
|
...unionBase,
|
|
375
|
-
members:
|
|
391
|
+
members: unionMembers.map((s) => parseSchema({ schema: s as SchemaObject, name }, rawOptions)),
|
|
376
392
|
})
|
|
393
|
+
|
|
394
|
+
return ast.applyMacros(unionNode, [macroSimplifyUnion], { depth: 'shallow' })
|
|
377
395
|
}
|
|
378
396
|
|
|
379
397
|
/**
|
|
@@ -585,17 +603,7 @@ export function createSchemaParser(ctx: OasParserContext, dialect: OasDialect =
|
|
|
585
603
|
|
|
586
604
|
const resolvedChildName = childName(name, propName)
|
|
587
605
|
const propNode = parseSchema({ schema: resolvedPropSchema, name: resolvedChildName }, rawOptions)
|
|
588
|
-
const schemaNode = (
|
|
589
|
-
const node = ast.setEnumName(propNode, name, propName, options.enumSuffix)
|
|
590
|
-
const tupleNode = ast.narrowSchema(node, 'tuple')
|
|
591
|
-
if (tupleNode?.items) {
|
|
592
|
-
const namedItems = tupleNode.items.map((item) => ast.setEnumName(item, name, propName, options.enumSuffix))
|
|
593
|
-
if (namedItems.some((item, i) => item !== tupleNode.items![i])) {
|
|
594
|
-
return { ...tupleNode, items: namedItems }
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
return node
|
|
598
|
-
})()
|
|
606
|
+
const schemaNode = nameEnums(propNode, { parentName: name, propName, enumSuffix: options.enumSuffix })
|
|
599
607
|
|
|
600
608
|
return ast.factory.createProperty({
|
|
601
609
|
name: propName,
|
|
@@ -649,12 +657,7 @@ export function createSchemaParser(ctx: OasParserContext, dialect: OasDialect =
|
|
|
649
657
|
const discPropName = schema.discriminator.propertyName
|
|
650
658
|
const values = Object.keys(schema.discriminator.mapping)
|
|
651
659
|
const enumName = name ? enumPropName(name, discPropName, options.enumSuffix) : undefined
|
|
652
|
-
return ast.
|
|
653
|
-
node: objectNode,
|
|
654
|
-
propertyName: discPropName,
|
|
655
|
-
values,
|
|
656
|
-
enumName,
|
|
657
|
-
})
|
|
660
|
+
return ast.applyMacros(objectNode, [macroDiscriminatorEnum({ propertyName: discPropName, values, enumName })], { depth: 'shallow' })
|
|
658
661
|
}
|
|
659
662
|
|
|
660
663
|
return objectNode
|