@kubb/adapter-oas 5.0.0-alpha.73 → 5.0.0-alpha.74
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 +34 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +16 -18
- package/dist/index.js +34 -30
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/parser.ts +37 -18
- package/src/resolvers.ts +13 -21
- package/src/types.ts +16 -18
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.74",
|
|
4
4
|
"description": "OpenAPI / Swagger adapter for Kubb converts OAS input into a @kubb/ast RootNode.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"adapter",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"registry": "https://registry.npmjs.org/"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@redocly/openapi-core": "^2.30.
|
|
45
|
+
"@redocly/openapi-core": "^2.30.3",
|
|
46
46
|
"oas": "^32.1.18",
|
|
47
47
|
"oas-normalize": "^16.0.4",
|
|
48
48
|
"swagger2openapi": "^7.0.8",
|
|
49
|
-
"@kubb/core": "5.0.0-alpha.
|
|
49
|
+
"@kubb/core": "5.0.0-alpha.74"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/swagger2openapi": "^7.0.4",
|
package/src/parser.ts
CHANGED
|
@@ -20,10 +20,10 @@ import {
|
|
|
20
20
|
import type { ContentType, Document, Operation, ReferenceObject, SchemaObject } from './types.ts'
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* Parser context holding the raw OpenAPI document and optional content-type override.
|
|
24
24
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
25
|
+
* Passed to schema and operation converters to access the full specification
|
|
26
|
+
* and handle content negotiation when multiple media types are available.
|
|
27
27
|
*/
|
|
28
28
|
export type OasParserContext = {
|
|
29
29
|
document: Document
|
|
@@ -31,8 +31,11 @@ export type OasParserContext = {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* Pre-computed per-schema context passed to every
|
|
35
|
-
*
|
|
34
|
+
* Pre-computed per-schema context passed to every schema converter.
|
|
35
|
+
*
|
|
36
|
+
* Centralizes schema derivations (type resolution, defaults, options) to avoid repeated
|
|
37
|
+
* computation across all conversion branches. The `type` field is normalized from OAS 3.1
|
|
38
|
+
* multi-type arrays to a single string.
|
|
36
39
|
*/
|
|
37
40
|
type SchemaContext = {
|
|
38
41
|
schema: SchemaObject
|
|
@@ -48,9 +51,12 @@ type SchemaContext = {
|
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* but appears in
|
|
54
|
+
* Normalizes malformed `{ type: 'array', enum: [...] }` schemas by moving enum values into items.
|
|
55
|
+
*
|
|
56
|
+
* This pattern violates the OpenAPI spec but appears in real specs. The fix moves enum values
|
|
57
|
+
* from the array to its items sub-schema, making them valid for downstream processing.
|
|
58
|
+
*
|
|
59
|
+
* @note This is a defensive measure for robustness with non-compliant specs.
|
|
54
60
|
*/
|
|
55
61
|
function normalizeArrayEnum(schema: SchemaObject): SchemaObject {
|
|
56
62
|
const isItemsObject = typeof schema.items === 'object' && !Array.isArray(schema.items)
|
|
@@ -64,10 +70,13 @@ function normalizeArrayEnum(schema: SchemaObject): SchemaObject {
|
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
/**
|
|
67
|
-
*
|
|
73
|
+
* Factory function that creates schema and operation converters for a given OpenAPI context.
|
|
74
|
+
*
|
|
75
|
+
* Returns closures that share mutable state (`resolvingRefs` set for cycle detection).
|
|
76
|
+
* Each converter branch (`convertRef`, `convertAllOf`, etc.) mutually recursively calls `parseSchema`,
|
|
77
|
+
* made possible by hoisting of function declarations.
|
|
68
78
|
*
|
|
69
|
-
*
|
|
70
|
-
* reference each other and `parseSchema` via JS hoisting (mutual recursion).
|
|
79
|
+
* @note Not exported; called internally by `parseOas()` and `parseSchema()`.
|
|
71
80
|
*/
|
|
72
81
|
function createSchemaParser(ctx: OasParserContext) {
|
|
73
82
|
const document = ctx.document
|
|
@@ -922,12 +931,18 @@ function createSchemaParser(ctx: OasParserContext) {
|
|
|
922
931
|
}
|
|
923
932
|
|
|
924
933
|
/**
|
|
925
|
-
*
|
|
934
|
+
* Parses a single OpenAPI `SchemaObject` into a `SchemaNode`.
|
|
935
|
+
*
|
|
936
|
+
* Use this for targeted schema parsing when you don't need the full spec.
|
|
937
|
+
* For complete spec parsing, use `parseOas()` instead which handles operations and all schemas together.
|
|
938
|
+
*
|
|
939
|
+
* @note Circular schema references are tracked via internal state and resolve appropriately.
|
|
926
940
|
*
|
|
927
941
|
* @example
|
|
928
942
|
* ```ts
|
|
943
|
+
* const document = yaml.parse(fs.readFileSync('openapi.yaml', 'utf8'))
|
|
929
944
|
* const ctx = { document }
|
|
930
|
-
* parseSchema(ctx, { schema: { type: 'string', format: 'uuid' } })
|
|
945
|
+
* const schema = parseSchema(ctx, { schema: { type: 'string', format: 'uuid' } })
|
|
931
946
|
* ```
|
|
932
947
|
*/
|
|
933
948
|
export function parseSchema(
|
|
@@ -939,16 +954,20 @@ export function parseSchema(
|
|
|
939
954
|
}
|
|
940
955
|
|
|
941
956
|
/**
|
|
942
|
-
*
|
|
957
|
+
* Parses an OpenAPI specification into Kubb's universal `InputNode` AST.
|
|
943
958
|
*
|
|
944
|
-
* This is the main entry point
|
|
945
|
-
*
|
|
946
|
-
*
|
|
959
|
+
* This is the main entry point for `@kubb/adapter-oas`. It converts OpenAPI/Swagger specs into a spec-agnostic tree
|
|
960
|
+
* that downstream plugins (`plugin-ts`, `plugin-zod`, etc.) consume for code generation. No code is generated here —
|
|
961
|
+
* the tree is a pure data structure representing all schemas and operations.
|
|
962
|
+
*
|
|
963
|
+
* Returns the AST root and a `nameMapping` for resolving schema references.
|
|
947
964
|
*
|
|
948
965
|
* @example
|
|
949
966
|
* ```ts
|
|
967
|
+
* import { parseOas } from '@kubb/adapter-oas'
|
|
968
|
+
*
|
|
950
969
|
* const document = await parseFromConfig(config)
|
|
951
|
-
* const root = parseOas(document, { dateType: 'date', contentType: 'application/json' })
|
|
970
|
+
* const { root, nameMapping } = parseOas(document, { dateType: 'date', contentType: 'application/json' })
|
|
952
971
|
* ```
|
|
953
972
|
*/
|
|
954
973
|
export function parseOas(
|
package/src/resolvers.ts
CHANGED
|
@@ -9,10 +9,9 @@ import { dereferenceWithRef, resolveRef } from './refs.ts'
|
|
|
9
9
|
import type { ContentType, Document, MediaTypeObject, Operation, ResponseObject, SchemaObject } from './types.ts'
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* Throws when an override value is not in the variable's allowed `enum` list.
|
|
12
|
+
* Replaces `{variable}` placeholders in an OpenAPI server URL with provided values.
|
|
13
|
+
* Resolution order: `overrides[key]` → `variable.default` → left unreplaced.
|
|
14
|
+
* Throws if an override value is not in the variable's `enum` list.
|
|
16
15
|
*
|
|
17
16
|
* @example
|
|
18
17
|
* ```ts
|
|
@@ -46,18 +45,16 @@ export function resolveServerUrl(server: ServerObject, overrides?: Record<string
|
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* which are handled separately because their output depends on parser options.
|
|
48
|
+
* Returns the Kubb `SchemaType` for a given OAS `format` string, or `null` if not found.
|
|
49
|
+
* Formats not in `formatMap` (e.g., `int64`, `date-time`) are handled separately by parser options.
|
|
52
50
|
*/
|
|
53
51
|
export function getSchemaType(format: string): ast.SchemaType | null {
|
|
54
52
|
return formatMap[format as keyof typeof formatMap] ?? null
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
/**
|
|
58
|
-
*
|
|
59
|
-
* Numeric types (`number`, `integer`, `bigint`)
|
|
60
|
-
* `boolean` maps to `'boolean'`; everything else defaults to `'string'`.
|
|
56
|
+
* Converts an OAS primitive type string to its `PrimitiveSchemaType` equivalent.
|
|
57
|
+
* Numeric types (`number`, `integer`, `bigint`) pass through unchanged. `boolean` maps to `'boolean'`. Everything else becomes `'string'`.
|
|
61
58
|
*/
|
|
62
59
|
export function getPrimitiveType(type: string | undefined): ast.PrimitiveSchemaType {
|
|
63
60
|
if (type === 'number' || type === 'integer' || type === 'bigint') return type
|
|
@@ -67,8 +64,7 @@ export function getPrimitiveType(type: string | undefined): ast.PrimitiveSchemaT
|
|
|
67
64
|
}
|
|
68
65
|
|
|
69
66
|
/**
|
|
70
|
-
* Narrows a
|
|
71
|
-
* Returns `undefined` for content types not present in `KNOWN_MEDIA_TYPES`.
|
|
67
|
+
* Narrows a content-type string to the `MediaType` union Kubb recognizes, or returns `null`.
|
|
72
68
|
*/
|
|
73
69
|
export function getMediaType(contentType: string): ast.MediaType | null {
|
|
74
70
|
return Object.values(ast.mediaTypes).includes(contentType as ast.MediaType) ? (contentType as ast.MediaType) : null
|
|
@@ -79,11 +75,9 @@ export type OperationsOptions = {
|
|
|
79
75
|
}
|
|
80
76
|
|
|
81
77
|
/**
|
|
82
|
-
* Returns all
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* `$ref` parameters are resolved via `dereferenceWithRef` to restore backward compatibility
|
|
86
|
-
* with `oas` v31+ which otherwise filters them out.
|
|
78
|
+
* Returns all parameters for an operation, merging path-level and operation-level entries.
|
|
79
|
+
* Operation-level parameters override path-level ones with the same `in:name` key.
|
|
80
|
+
* `$ref` parameters resolve via `dereferenceWithRef` for backward compatibility.
|
|
87
81
|
*
|
|
88
82
|
* @example
|
|
89
83
|
* ```ts
|
|
@@ -184,7 +178,7 @@ export function getResponseSchema(document: Document, operation: Operation, stat
|
|
|
184
178
|
}
|
|
185
179
|
|
|
186
180
|
/**
|
|
187
|
-
* Returns the request body schema for an operation, or `
|
|
181
|
+
* Returns the request body schema for an operation, or `null` when absent.
|
|
188
182
|
*
|
|
189
183
|
* @example
|
|
190
184
|
* ```ts
|
|
@@ -217,9 +211,7 @@ export function getRequestSchema(document: Document, operation: Operation, optio
|
|
|
217
211
|
type SchemaSourceMode = 'schemas' | 'responses' | 'requestBodies'
|
|
218
212
|
|
|
219
213
|
/**
|
|
220
|
-
* A schema annotated with
|
|
221
|
-
*
|
|
222
|
-
* Used during cross-source name-collision resolution in `resolveNameCollisions`.
|
|
214
|
+
* A schema annotated with its component section source and original name. Used by `resolveNameCollisions` for cross-source collision resolution.
|
|
223
215
|
*/
|
|
224
216
|
export type SchemaWithMetadata = {
|
|
225
217
|
schema: SchemaObject
|
package/src/types.ts
CHANGED
|
@@ -18,19 +18,19 @@ import type { OpenAPIV3 } from 'openapi-types'
|
|
|
18
18
|
export type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Content-type string
|
|
22
|
-
*
|
|
23
|
-
* Accepts `'application/json'` or any arbitrary media type string.
|
|
21
|
+
* Content-type string for selecting request/response schemas from an OpenAPI spec.
|
|
22
|
+
* Supports `'application/json'` or any other media type.
|
|
24
23
|
*
|
|
25
24
|
* @example
|
|
26
25
|
* ```ts
|
|
27
|
-
* const ct:
|
|
26
|
+
* const ct: ContentType = 'application/vnd.api+json'
|
|
28
27
|
* ```
|
|
29
28
|
*/
|
|
30
29
|
export type ContentType = 'application/json' | (string & {})
|
|
31
30
|
|
|
32
31
|
/**
|
|
33
|
-
*
|
|
32
|
+
* Extended OpenAPI 3.0 schema object that includes OpenAPI 3.1 and JSON Schema fields.
|
|
33
|
+
* The parser uses these additional fields to handle newer spec versions.
|
|
34
34
|
*
|
|
35
35
|
* @example
|
|
36
36
|
* ```ts
|
|
@@ -74,7 +74,7 @@ export type SchemaObject = OASSchemaObject & {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
77
|
+
* Maps uppercase HTTP method names to lowercase for backwards compatibility.
|
|
78
78
|
*
|
|
79
79
|
* @example
|
|
80
80
|
* ```ts
|
|
@@ -88,45 +88,43 @@ export const HttpMethods = Object.fromEntries(Object.entries(ast.httpMethods).ma
|
|
|
88
88
|
>
|
|
89
89
|
|
|
90
90
|
/**
|
|
91
|
-
*
|
|
91
|
+
* HTTP method as a lowercase string (`'get' | 'post' | ...`).
|
|
92
92
|
*/
|
|
93
93
|
export type HttpMethod = Lowercase<ast.HttpMethod>
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
|
-
* Normalized OpenAPI document
|
|
96
|
+
* Normalized OpenAPI document after parsing.
|
|
97
97
|
*/
|
|
98
98
|
export type Document = OASDocument
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
|
-
*
|
|
101
|
+
* API operation extracted from an OpenAPI document.
|
|
102
102
|
*/
|
|
103
103
|
export type Operation = OASOperation
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
|
-
*
|
|
106
|
+
* Discriminator object for `oneOf`/`anyOf` schemas in OpenAPI.
|
|
107
107
|
*/
|
|
108
108
|
export type DiscriminatorObject = OASDiscriminatorObject
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
|
-
* OpenAPI
|
|
111
|
+
* OpenAPI reference object pointing to a schema definition via `$ref`.
|
|
112
112
|
*/
|
|
113
113
|
export type ReferenceObject = OpenAPIV3.ReferenceObject
|
|
114
114
|
|
|
115
115
|
/**
|
|
116
|
-
* OpenAPI response object
|
|
116
|
+
* OpenAPI response object from a spec that contains schema, status code, and headers.
|
|
117
117
|
*/
|
|
118
118
|
export type ResponseObject = OASResponseObject
|
|
119
119
|
|
|
120
120
|
/**
|
|
121
|
-
* OpenAPI media type object that maps a content-type to
|
|
121
|
+
* OpenAPI media type object that maps a content-type string to its schema.
|
|
122
122
|
*/
|
|
123
123
|
export type MediaTypeObject = OASMediaTypeObject
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* Extends `ParserOptions` from `@kubb/ast` with adapter-specific controls
|
|
129
|
-
* like spec validation and server URL selection.
|
|
126
|
+
* Configuration options for the OpenAPI adapter.
|
|
127
|
+
* Controls spec validation, content-type selection, and server URL resolution.
|
|
130
128
|
*
|
|
131
129
|
* @example
|
|
132
130
|
* ```ts
|
|
@@ -176,7 +174,7 @@ export type AdapterOasOptions = {
|
|
|
176
174
|
} & Partial<ast.ParserOptions>
|
|
177
175
|
|
|
178
176
|
/**
|
|
179
|
-
*
|
|
177
|
+
* Adapter options after defaults have been applied and schema name collisions resolved.
|
|
180
178
|
*/
|
|
181
179
|
export type AdapterOasResolvedOptions = {
|
|
182
180
|
validate: boolean
|