@kubb/adapter-oas 5.0.0-beta.26 → 5.0.0-beta.28
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 +43 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +43 -16
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/parser.ts +26 -13
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.28",
|
|
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",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"oas": "^32.1.18",
|
|
48
48
|
"oas-normalize": "^16.0.4",
|
|
49
49
|
"swagger2openapi": "^7.0.8",
|
|
50
|
-
"@kubb/core": "5.0.0-beta.
|
|
50
|
+
"@kubb/core": "5.0.0-beta.28"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/swagger2openapi": "^7.0.4",
|
package/src/parser.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { URLPath } from '@internals/utils'
|
|
1
|
+
import { pascalCase, URLPath } from '@internals/utils'
|
|
2
2
|
import { ast } from '@kubb/core'
|
|
3
3
|
import BaseOas from 'oas'
|
|
4
4
|
import { DEFAULT_PARSER_OPTIONS, enumExtensionKeys, SCHEMA_REF_PREFIX, typeOptionMap } from './constants.ts'
|
|
@@ -161,7 +161,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
161
161
|
schema.additionalProperties === undefined
|
|
162
162
|
) {
|
|
163
163
|
const [memberSchema] = schema.allOf as Array<SchemaObject | ReferenceObject>
|
|
164
|
-
const memberNode = parseSchema({ schema: memberSchema! as SchemaObject, name
|
|
164
|
+
const memberNode = parseSchema({ schema: memberSchema! as SchemaObject, name }, rawOptions)
|
|
165
165
|
const { kind: _kind, ...memberNodeProps } = memberNode
|
|
166
166
|
const mergedNullable = nullable || memberNode.nullable || undefined
|
|
167
167
|
const mergedDefault = schema.default === null && mergedNullable ? undefined : (schema.default ?? memberNode.default)
|
|
@@ -208,7 +208,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
208
208
|
}
|
|
209
209
|
return true
|
|
210
210
|
})
|
|
211
|
-
.map((s) => parseSchema({ schema: s as SchemaObject }, rawOptions))
|
|
211
|
+
.map((s) => parseSchema({ schema: s as SchemaObject, name }, rawOptions))
|
|
212
212
|
|
|
213
213
|
const syntheticStart = allOfMembers.length
|
|
214
214
|
|
|
@@ -233,6 +233,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
233
233
|
properties: { [key]: resolved.properties[key] },
|
|
234
234
|
required: [key],
|
|
235
235
|
} as SchemaObject,
|
|
236
|
+
name,
|
|
236
237
|
},
|
|
237
238
|
rawOptions,
|
|
238
239
|
),
|
|
@@ -246,6 +247,9 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
246
247
|
|
|
247
248
|
if (schema.properties) {
|
|
248
249
|
const { allOf: _allOf, ...schemaWithoutAllOf } = schema
|
|
250
|
+
// Don't pass `name` here — the result must stay anonymous so it can be merged with the
|
|
251
|
+
// adjacent synthetic object in `mergeAdjacentObjectsLazy`. Nested enum qualification
|
|
252
|
+
// happens upstream via `convertObject`'s `setEnumName` propagation.
|
|
249
253
|
allOfMembers.push(parseSchema({ schema: schemaWithoutAllOf }, rawOptions))
|
|
250
254
|
}
|
|
251
255
|
|
|
@@ -301,7 +305,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
301
305
|
const members = unionMembers.map((s) => {
|
|
302
306
|
const ref = isReference(s) ? s.$ref : undefined
|
|
303
307
|
const discriminatorValue = ast.findDiscriminator(discriminator?.mapping, ref)
|
|
304
|
-
const memberNode = parseSchema({ schema: s as SchemaObject }, rawOptions)
|
|
308
|
+
const memberNode = parseSchema({ schema: s as SchemaObject, name }, rawOptions)
|
|
305
309
|
|
|
306
310
|
if (!discriminatorValue || !discriminator) {
|
|
307
311
|
return memberNode
|
|
@@ -351,7 +355,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
351
355
|
return ast.createSchema({
|
|
352
356
|
type: 'union',
|
|
353
357
|
...unionBase,
|
|
354
|
-
members: ast.simplifyUnion(unionMembers.map((s) => parseSchema({ schema: s as SchemaObject }, rawOptions))),
|
|
358
|
+
members: ast.simplifyUnion(unionMembers.map((s) => parseSchema({ schema: s as SchemaObject, name }, rawOptions))),
|
|
355
359
|
})
|
|
356
360
|
}
|
|
357
361
|
|
|
@@ -646,7 +650,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
646
650
|
*/
|
|
647
651
|
function convertArray({ schema, name, nullable, defaultValue, rawOptions, options }: SchemaContext): ast.SchemaNode {
|
|
648
652
|
const rawItems = schema.items as SchemaObject | undefined
|
|
649
|
-
const itemName = rawItems?.enum?.length && name ? ast.enumPropName(null, name, options.enumSuffix) :
|
|
653
|
+
const itemName = rawItems?.enum?.length && name ? ast.enumPropName(null, name, options.enumSuffix) : name
|
|
650
654
|
const items = rawItems ? [parseSchema({ schema: rawItems, name: itemName }, rawOptions)] : []
|
|
651
655
|
|
|
652
656
|
return ast.createSchema({
|
|
@@ -814,15 +818,17 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
814
818
|
/**
|
|
815
819
|
* Converts a dereferenced OAS parameter object into a `ParameterNode`.
|
|
816
820
|
*/
|
|
817
|
-
function parseParameter(options: ast.ParserOptions, param: Record<string, unknown
|
|
821
|
+
function parseParameter(options: ast.ParserOptions, param: Record<string, unknown>, parentName?: string): ast.ParameterNode {
|
|
818
822
|
const required = (param['required'] as boolean | undefined) ?? false
|
|
823
|
+
const paramName = param['name'] as string
|
|
824
|
+
const schemaName = parentName && paramName ? pascalCase(`${parentName} ${paramName}`) : undefined
|
|
819
825
|
|
|
820
826
|
const schema: ast.SchemaNode = param['schema']
|
|
821
|
-
? parseSchema({ schema: param['schema'] as SchemaObject }, options)
|
|
827
|
+
? parseSchema({ schema: param['schema'] as SchemaObject, name: schemaName }, options)
|
|
822
828
|
: ast.createSchema({ type: typeOptionMap.get(options.unknownType)! })
|
|
823
829
|
|
|
824
830
|
return ast.createParameter({
|
|
825
|
-
name:
|
|
831
|
+
name: paramName,
|
|
826
832
|
in: param['in'] as ast.ParameterLocation,
|
|
827
833
|
schema: {
|
|
828
834
|
...schema,
|
|
@@ -888,8 +894,10 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
888
894
|
* Converts an OAS `Operation` into an `OperationNode`.
|
|
889
895
|
*/
|
|
890
896
|
function parseOperation(options: ast.ParserOptions, operation: Operation): ast.OperationNode {
|
|
897
|
+
const operationId = operation.getOperationId()
|
|
898
|
+
const operationName = operationId ? pascalCase(operationId) : undefined
|
|
891
899
|
const parameters: Array<ast.ParameterNode> = getParameters(document, operation).map((param) =>
|
|
892
|
-
parseParameter(options, param as unknown as Record<string, unknown
|
|
900
|
+
parseParameter(options, param as unknown as Record<string, unknown>, operationName),
|
|
893
901
|
)
|
|
894
902
|
|
|
895
903
|
// Determine which content types to include in requestBody.content.
|
|
@@ -898,6 +906,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
898
906
|
const allContentTypes = ctx.contentType ? [ctx.contentType] : getRequestBodyContentTypes(document, operation)
|
|
899
907
|
|
|
900
908
|
const requestBodyMeta = getRequestBodyMeta(operation)
|
|
909
|
+
const requestBodyName = operationName ? `${operationName}Request` : undefined
|
|
901
910
|
|
|
902
911
|
const content = allContentTypes.flatMap((ct) => {
|
|
903
912
|
const schema = getRequestSchema(document, operation, { contentType: ct })
|
|
@@ -905,7 +914,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
905
914
|
return [
|
|
906
915
|
{
|
|
907
916
|
contentType: ct,
|
|
908
|
-
schema: ast.syncOptionality(parseSchema({ schema }, options), requestBodyMeta.required),
|
|
917
|
+
schema: ast.syncOptionality(parseSchema({ schema, name: requestBodyName }, options), requestBodyMeta.required),
|
|
909
918
|
keysToOmit: collectPropertyKeysByFlag(schema, 'readOnly'),
|
|
910
919
|
},
|
|
911
920
|
]
|
|
@@ -924,9 +933,13 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
924
933
|
const responseObj = operation.getResponseByStatusCode(statusCode)
|
|
925
934
|
const responseSchema = getResponseSchema(document, operation, statusCode, { contentType: ctx.contentType })
|
|
926
935
|
|
|
936
|
+
// Use `Status<code>` (matching plugin-ts's resolveResponseStatusName convention) so the
|
|
937
|
+
// qualified names for nested enums don't collide with top-level component schemas that
|
|
938
|
+
// happen to be named `<operation><statusCode>` (e.g. `GetMaintenance200`).
|
|
939
|
+
const responseName = operationName ? `${operationName}Status${statusCode}` : undefined
|
|
927
940
|
const schema =
|
|
928
941
|
responseSchema && Object.keys(responseSchema).length > 0
|
|
929
|
-
? parseSchema({ schema: responseSchema }, options)
|
|
942
|
+
? parseSchema({ schema: responseSchema, name: responseName }, options)
|
|
930
943
|
: ast.createSchema({
|
|
931
944
|
type: typeOptionMap.get(options.emptySchemaType)!,
|
|
932
945
|
})
|
|
@@ -946,7 +959,7 @@ export function createSchemaParser(ctx: OasParserContext) {
|
|
|
946
959
|
const urlPath = new URLPath(operation.path)
|
|
947
960
|
|
|
948
961
|
return ast.createOperation({
|
|
949
|
-
operationId
|
|
962
|
+
operationId,
|
|
950
963
|
method: operation.method.toUpperCase() as ast.HttpMethod,
|
|
951
964
|
path: urlPath.path,
|
|
952
965
|
tags: operation.getTags().map((tag) => tag.name),
|