@kubb/adapter-oas 5.0.0-alpha.13 → 5.0.0-alpha.15
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 +19 -75
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -17
- package/dist/index.js +19 -75
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/adapter.ts +2 -4
- package/src/oas/Oas.ts +2 -10
- package/src/oas/utils.ts +7 -8
- package/src/parser.ts +8 -27
- package/src/types.ts +0 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/adapter-oas",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.15",
|
|
4
4
|
"description": "OpenAPI / Swagger adapter for Kubb — converts OAS input into a @kubb/ast RootNode.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openapi",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"openapi-types": "^12.1.3",
|
|
47
47
|
"remeda": "^2.33.6",
|
|
48
48
|
"swagger2openapi": "^7.0.8",
|
|
49
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
50
|
-
"@kubb/core": "5.0.0-alpha.
|
|
49
|
+
"@kubb/ast": "5.0.0-alpha.15",
|
|
50
|
+
"@kubb/core": "5.0.0-alpha.15"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/swagger2openapi": "^7.0.4",
|
package/src/adapter.ts
CHANGED
|
@@ -37,7 +37,6 @@ export const adapterOas = createAdapter<OasAdapter>((options) => {
|
|
|
37
37
|
serverIndex,
|
|
38
38
|
serverVariables,
|
|
39
39
|
discriminator = 'strict',
|
|
40
|
-
collisionDetection = true,
|
|
41
40
|
dateType = DEFAULT_PARSER_OPTIONS.dateType,
|
|
42
41
|
integerType = DEFAULT_PARSER_OPTIONS.integerType,
|
|
43
42
|
unknownType = DEFAULT_PARSER_OPTIONS.unknownType,
|
|
@@ -58,7 +57,6 @@ export const adapterOas = createAdapter<OasAdapter>((options) => {
|
|
|
58
57
|
serverIndex,
|
|
59
58
|
serverVariables,
|
|
60
59
|
discriminator,
|
|
61
|
-
collisionDetection,
|
|
62
60
|
dateType,
|
|
63
61
|
integerType,
|
|
64
62
|
unknownType,
|
|
@@ -73,7 +71,7 @@ export const adapterOas = createAdapter<OasAdapter>((options) => {
|
|
|
73
71
|
const fakeConfig = sourceToFakeConfig(source)
|
|
74
72
|
const oas = await parseFromConfig(fakeConfig, oasClass)
|
|
75
73
|
|
|
76
|
-
oas.setOptions({ contentType, discriminator
|
|
74
|
+
oas.setOptions({ contentType, discriminator })
|
|
77
75
|
|
|
78
76
|
if (validate) {
|
|
79
77
|
try {
|
|
@@ -86,7 +84,7 @@ export const adapterOas = createAdapter<OasAdapter>((options) => {
|
|
|
86
84
|
const server = serverIndex !== undefined ? oas.api.servers?.at(serverIndex) : undefined
|
|
87
85
|
const baseURL = server?.url ? resolveServerUrl(server, serverVariables) : undefined
|
|
88
86
|
|
|
89
|
-
const parser = createOasParser(oas, { contentType
|
|
87
|
+
const parser = createOasParser(oas, { contentType })
|
|
90
88
|
const root = parser.parse({ dateType, integerType, unknownType, emptySchemaType, enumSuffix })
|
|
91
89
|
|
|
92
90
|
// This must happen after parse() because legacy enum remapping is finalized there.
|
package/src/oas/Oas.ts
CHANGED
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
flattenSchema,
|
|
9
9
|
isDiscriminator,
|
|
10
10
|
isReference,
|
|
11
|
-
legacyResolve,
|
|
12
11
|
resolveCollisions,
|
|
13
12
|
type SchemaWithMetadata,
|
|
14
13
|
sortSchemas,
|
|
@@ -25,11 +24,6 @@ const KUBB_INLINE_REF_PREFIX = '#kubb-inline-'
|
|
|
25
24
|
type OasOptions = {
|
|
26
25
|
contentType?: contentType
|
|
27
26
|
discriminator?: 'strict' | 'inherit'
|
|
28
|
-
/**
|
|
29
|
-
* Resolve name collisions when schemas from different components share the same name (case-insensitive).
|
|
30
|
-
* @default false
|
|
31
|
-
*/
|
|
32
|
-
collisionDetection?: boolean
|
|
33
27
|
}
|
|
34
28
|
|
|
35
29
|
export class Oas extends BaseOas {
|
|
@@ -541,13 +535,12 @@ export class Oas extends BaseOas {
|
|
|
541
535
|
* Get schemas from OpenAPI components (schemas, responses, requestBodies).
|
|
542
536
|
* Returns schemas in dependency order along with name mapping for collision resolution.
|
|
543
537
|
*/
|
|
544
|
-
getSchemas(options: { contentType?: contentType; includes?: Array<'schemas' | 'responses' | 'requestBodies'
|
|
538
|
+
getSchemas(options: { contentType?: contentType; includes?: Array<'schemas' | 'responses' | 'requestBodies'> } = {}): {
|
|
545
539
|
schemas: Record<string, SchemaObject>
|
|
546
540
|
nameMapping: Map<string, string>
|
|
547
541
|
} {
|
|
548
542
|
const contentType = options.contentType ?? this.#options.contentType
|
|
549
543
|
const includes = options.includes ?? ['schemas', 'requestBodies', 'responses']
|
|
550
|
-
const shouldResolveCollisions = options.collisionDetection ?? this.#options.collisionDetection ?? false
|
|
551
544
|
|
|
552
545
|
const components = this.getDefinition().components
|
|
553
546
|
const schemasWithMeta: SchemaWithMetadata[] = []
|
|
@@ -612,8 +605,7 @@ export class Oas extends BaseOas {
|
|
|
612
605
|
}
|
|
613
606
|
}
|
|
614
607
|
|
|
615
|
-
|
|
616
|
-
const { schemas, nameMapping } = shouldResolveCollisions ? resolveCollisions(schemasWithMeta) : legacyResolve(schemasWithMeta)
|
|
608
|
+
const { schemas, nameMapping } = resolveCollisions(schemasWithMeta)
|
|
617
609
|
|
|
618
610
|
return {
|
|
619
611
|
schemas: sortSchemas(schemas),
|
package/src/oas/utils.ts
CHANGED
|
@@ -323,15 +323,14 @@ export function extractSchemaFromContent(content: Record<string, unknown> | unde
|
|
|
323
323
|
* Returns the PascalCase suffix appended to a component name when resolving
|
|
324
324
|
* cross-source name collisions (schemas vs. responses vs. requestBodies).
|
|
325
325
|
*/
|
|
326
|
+
const semanticSuffixes: Record<SchemaSourceMode, string> = {
|
|
327
|
+
schemas: 'Schema',
|
|
328
|
+
responses: 'Response',
|
|
329
|
+
requestBodies: 'Request',
|
|
330
|
+
}
|
|
331
|
+
|
|
326
332
|
function getSemanticSuffix(source: SchemaSourceMode): string {
|
|
327
|
-
|
|
328
|
-
case 'schemas':
|
|
329
|
-
return 'Schema'
|
|
330
|
-
case 'responses':
|
|
331
|
-
return 'Response'
|
|
332
|
-
case 'requestBodies':
|
|
333
|
-
return 'Request'
|
|
334
|
-
}
|
|
333
|
+
return semanticSuffixes[source]
|
|
335
334
|
}
|
|
336
335
|
|
|
337
336
|
/**
|
package/src/parser.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { pascalCase, URLPath } from '@internals/utils'
|
|
2
2
|
import { createOperation, createParameter, createProperty, createResponse, createRoot, createSchema, narrowSchema, schemaTypes, transform } from '@kubb/ast'
|
|
3
3
|
import type {
|
|
4
4
|
ArraySchemaNode,
|
|
@@ -121,7 +121,6 @@ export type InferSchemaNode<
|
|
|
121
121
|
*/
|
|
122
122
|
export type OasParserOptions = {
|
|
123
123
|
contentType?: contentType
|
|
124
|
-
collisionDetection?: boolean
|
|
125
124
|
}
|
|
126
125
|
|
|
127
126
|
/**
|
|
@@ -221,18 +220,10 @@ export type OasParser = {
|
|
|
221
220
|
* const root = parser.parse({ emptySchemaType: 'unknown' })
|
|
222
221
|
* ```
|
|
223
222
|
*/
|
|
224
|
-
export function createOasParser(oas: Oas, { contentType
|
|
223
|
+
export function createOasParser(oas: Oas, { contentType }: OasParserOptions = {}): OasParser {
|
|
225
224
|
// Map from original component paths to resolved schema names (after collision resolution)
|
|
226
225
|
// e.g., { '#/components/schemas/Order': 'OrderSchema', '#/components/responses/Product': 'ProductResponse' }
|
|
227
|
-
const { schemas: schemaObjects, nameMapping } = oas.getSchemas({ contentType
|
|
228
|
-
|
|
229
|
-
// Legacy enum name deduplication: tracks used enum names and appends numeric suffixes
|
|
230
|
-
// (e.g. ParamsStatusEnum, ParamsStatusEnum2) when collisionDetection is disabled.
|
|
231
|
-
const usedEnumNames: Record<string, number> = {}
|
|
232
|
-
|
|
233
|
-
// Only apply legacy naming when collisionDetection is explicitly false.
|
|
234
|
-
// When undefined (e.g. direct parser usage without adapter), use the default (new) behavior.
|
|
235
|
-
const isLegacyNaming = collisionDetection === false
|
|
226
|
+
const { schemas: schemaObjects, nameMapping } = oas.getSchemas({ contentType })
|
|
236
227
|
|
|
237
228
|
/**
|
|
238
229
|
* Maps an `'any' | 'unknown' | 'void'` option string to the corresponding `SchemaType` constant.
|
|
@@ -478,7 +469,7 @@ export function createOasParser(oas: Oas, { contentType, collisionDetection }: O
|
|
|
478
469
|
|
|
479
470
|
// Convert shared properties once to avoid duplicate enum naming
|
|
480
471
|
// (e.g. StatusEnum appearing twice and getting a numeric suffix).
|
|
481
|
-
const sharedPropertiesNode = convertSchema({ schema: memberBaseSchema, name
|
|
472
|
+
const sharedPropertiesNode = convertSchema({ schema: memberBaseSchema, name }, options)
|
|
482
473
|
|
|
483
474
|
return createSchema({
|
|
484
475
|
type: 'union',
|
|
@@ -735,28 +726,21 @@ export function createOasParser(oas: Oas, { contentType, collisionDetection }: O
|
|
|
735
726
|
/**
|
|
736
727
|
* Builds the propagation name for a child property during recursive schema conversion.
|
|
737
728
|
*
|
|
738
|
-
*
|
|
739
|
-
* (e.g. `Params` for property `params`), keeping nested names short.
|
|
740
|
-
* - **Default naming**: the parent name is prepended so the full path is encoded
|
|
729
|
+
* The parent name is prepended so the full path is encoded
|
|
741
730
|
* (e.g. `OrderParams` when parent is `Order`).
|
|
742
731
|
*/
|
|
743
732
|
function resolveChildName(parentName: string | undefined, propName: string): string | undefined {
|
|
744
|
-
if (isLegacyNaming) {
|
|
745
|
-
return pascalCase(propName)
|
|
746
|
-
}
|
|
747
733
|
return parentName ? pascalCase([parentName, propName].join(' ')) : undefined
|
|
748
734
|
}
|
|
749
735
|
|
|
750
736
|
/**
|
|
751
737
|
* Derives the final name for an enum property schema node.
|
|
752
738
|
*
|
|
753
|
-
* The
|
|
754
|
-
*
|
|
755
|
-
* when the same name has already been used (e.g. `ParamsStatusEnum2`).
|
|
739
|
+
* The resulting name always includes the enum suffix and full parent path context
|
|
740
|
+
* (e.g. `OrderParamsStatusEnum`).
|
|
756
741
|
*/
|
|
757
742
|
function resolveEnumPropName(parentName: string | undefined, propName: string, enumSuffix: string): string {
|
|
758
|
-
|
|
759
|
-
return isLegacyNaming ? getUniqueName(raw, usedEnumNames) : raw
|
|
743
|
+
return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
|
|
760
744
|
}
|
|
761
745
|
|
|
762
746
|
/**
|
|
@@ -804,8 +788,6 @@ export function createOasParser(oas: Oas, { contentType, collisionDetection }: O
|
|
|
804
788
|
schema: {
|
|
805
789
|
...schemaNode,
|
|
806
790
|
nullable: schemaNode.type === 'null' ? undefined : propNullable || undefined,
|
|
807
|
-
optional: !required && !propNullable ? true : undefined,
|
|
808
|
-
nullish: !required && propNullable ? true : undefined,
|
|
809
791
|
},
|
|
810
792
|
required,
|
|
811
793
|
})
|
|
@@ -1078,7 +1060,6 @@ export function createOasParser(oas: Oas, { contentType, collisionDetection }: O
|
|
|
1078
1060
|
schema: {
|
|
1079
1061
|
...schema,
|
|
1080
1062
|
description: (param['description'] as string | undefined) ?? schema.description,
|
|
1081
|
-
optional: !required || !!schema.optional ? true : undefined,
|
|
1082
1063
|
},
|
|
1083
1064
|
required,
|
|
1084
1065
|
})
|
package/src/types.ts
CHANGED
|
@@ -66,16 +66,6 @@ export type OasAdapterOptions = {
|
|
|
66
66
|
* @default 'strict'
|
|
67
67
|
*/
|
|
68
68
|
discriminator?: 'strict' | 'inherit'
|
|
69
|
-
/**
|
|
70
|
-
* Enable collision detection for inline enum and schema type names.
|
|
71
|
-
* When `true` (default), full-path names are used and name collisions
|
|
72
|
-
* across schema components are automatically resolved (e.g. `OrderParamsStatusEnum`).
|
|
73
|
-
* When `false`, enum names use only the immediate property context
|
|
74
|
-
* (e.g. `ParamsStatusEnum`) matching the v4 naming conventions, with numeric
|
|
75
|
-
* suffixes for deduplication (e.g. `ParamsStatusEnum2`).
|
|
76
|
-
* @default true
|
|
77
|
-
*/
|
|
78
|
-
collisionDetection?: boolean
|
|
79
69
|
/**
|
|
80
70
|
* How `format: 'date-time'` schemas are represented in the AST.
|
|
81
71
|
* - `'string'` maps to a `datetime` string node.
|
|
@@ -92,7 +82,6 @@ export type OasAdapterResolvedOptions = {
|
|
|
92
82
|
serverIndex: OasAdapterOptions['serverIndex']
|
|
93
83
|
serverVariables: OasAdapterOptions['serverVariables']
|
|
94
84
|
discriminator: NonNullable<OasAdapterOptions['discriminator']>
|
|
95
|
-
collisionDetection: boolean
|
|
96
85
|
dateType: NonNullable<OasAdapterOptions['dateType']>
|
|
97
86
|
integerType: NonNullable<OasAdapterOptions['integerType']>
|
|
98
87
|
unknownType: NonNullable<OasAdapterOptions['unknownType']>
|