@kubb/adapter-oas 5.18.0 → 5.18.2

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.js CHANGED
@@ -20,7 +20,7 @@ var compatibility = {
20
20
  "node": ">=22.22"
21
21
  };
22
22
  // package.json
23
- var version = "5.18.0";
23
+ var version = "5.18.2";
24
24
  // src/constants.ts
25
25
  /**
26
26
  * Default parser options applied when no explicit options are provided.
@@ -248,7 +248,7 @@ function patchDiscriminatorNode(node, entry) {
248
248
  * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
249
249
  */
250
250
  function convert(text, pascal) {
251
- return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
251
+ return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)(?!xx(?![a-z]))([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
252
252
  if (word.length > 1 && word === word.toUpperCase()) return word;
253
253
  return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
254
254
  }).join("").replace(/[^a-zA-Z0-9]/g, "");
@@ -1209,8 +1209,29 @@ const semanticSuffixes = {
1209
1209
  */
1210
1210
  function getSchemas(document, { contentType }, refs) {
1211
1211
  const components = document.components;
1212
+ /**
1213
+ * Follows a `$ref` to the schema it ends at. Keywords next to a `$ref` apply together with its target,
1214
+ * so they stay on a one-member `allOf` around it.
1215
+ */
1216
+ function inlineRef(schema, seen = /* @__PURE__ */ new Set()) {
1217
+ if (!isReference(schema) || seen.has(schema.$ref)) return schema;
1218
+ const { $ref, ...siblings } = schema;
1219
+ const resolved = refs.resolve($ref);
1220
+ if (!resolved) return schema;
1221
+ const target = inlineRef(resolved, seen.add($ref));
1222
+ if (!Object.keys(siblings).length) return target;
1223
+ const own = "allOf" in siblings && Array.isArray(siblings.allOf) ? siblings.allOf : [];
1224
+ return {
1225
+ ...siblings,
1226
+ allOf: [...own, target]
1227
+ };
1228
+ }
1229
+ /**
1230
+ * Replaces a `$ref` schema with its target. A plain `$ref` to another `$ref` stays as it is, so the parser reads it as an alias.
1231
+ */
1212
1232
  function resolveSchemaRef(schema) {
1213
1233
  if (!isReference(schema)) return schema;
1234
+ if (Object.keys(schema).length > 1) return inlineRef(schema);
1214
1235
  const resolved = refs.resolve(schema.$ref);
1215
1236
  return resolved && !isReference(resolved) ? resolved : schema;
1216
1237
  }
@@ -1976,6 +1997,7 @@ function parseParameter({ param, pointer, parentName, options, parseSchema, refs
1976
1997
  required: param.required ?? false,
1977
1998
  ...pickStyleFields(param),
1978
1999
  ...contentType !== void 0 ? { contentType } : {},
2000
+ ...mediaType?.encoding && { encoding: getEncodingFields({ encoding: mediaType.encoding }).encoding },
1979
2001
  ...getExamples(mediaType ?? param, refs),
1980
2002
  ...param.deprecated && { deprecated: true },
1981
2003
  ...param.allowEmptyValue && { allowEmptyValue: true }
@@ -2044,12 +2066,13 @@ function parseOperation({ operation, ...ctx }) {
2044
2066
  required: bodyRequired || void 0,
2045
2067
  content: content.length > 0 ? content : void 0
2046
2068
  } : void 0;
2047
- const responses = getResponseStatusCodes(operation).map((statusCode) => {
2069
+ const responses = getResponseStatusCodes(operation).map((key) => {
2048
2070
  const response = getResponseByStatusCode({
2049
2071
  operation,
2050
2072
  refs,
2051
- statusCode
2073
+ statusCode: key
2052
2074
  });
2075
+ const statusCode = key.toLowerCase();
2053
2076
  const responseName = operationName ? `${operationName}Status${statusCode}` : void 0;
2054
2077
  const parseEntrySchema = (ct) => {
2055
2078
  const raw = getBodySchema({
@@ -2067,7 +2090,7 @@ function parseOperation({ operation, ...ctx }) {
2067
2090
  schema: raw,
2068
2091
  name: responseName
2069
2092
  }) : ast.factory.createSchema({ type: options.emptySchemaType });
2070
- if (raw && ct) scanSchema(node, `${mediaTypePointer(response?.content, ct, `${pointer}/responses/${statusCode}/content/${escapePointerToken(ct)}`)}/${schemaKeyword(mediaType)}`);
2093
+ if (raw && ct) scanSchema(node, `${mediaTypePointer(response?.content, ct, `${pointer}/responses/${key}/content/${escapePointerToken(ct)}`)}/${schemaKeyword(mediaType)}`);
2071
2094
  return {
2072
2095
  schema: node,
2073
2096
  keysToOmit: collectPropertyKeysByFlag(raw, "writeOnly"),
@@ -2091,7 +2114,7 @@ function parseOperation({ operation, ...ctx }) {
2091
2114
  contentType: "",
2092
2115
  ...parseEntrySchema(contentType)
2093
2116
  }));
2094
- const responsePointer = `${pointer}/responses/${statusCode}`;
2117
+ const responsePointer = `${pointer}/responses/${key}`;
2095
2118
  const headers = Object.entries(response?.headers ?? {}).flatMap(([name, value]) => {
2096
2119
  const header = name.toLowerCase() === "content-type" ? null : refs.deref(value);
2097
2120
  if (!header) return [];
@@ -2125,7 +2148,7 @@ function parseOperation({ operation, ...ctx }) {
2125
2148
  const fallback = operation.pathItem[key];
2126
2149
  return typeof fallback === "string" ? fallback : void 0;
2127
2150
  };
2128
- const servers = operation.schema.servers ?? operation.pathItem.servers;
2151
+ const servers = operation.schema.servers?.length ? operation.schema.servers : operation.pathItem.servers;
2129
2152
  const auth = toAuthRequirements(operation.schema.security ?? document.security, document);
2130
2153
  return ast.factory.createOperation({
2131
2154
  pointer,
@@ -2326,10 +2349,13 @@ function narrowUnionMembers({ unionMembers, discriminator, sharedPropertiesNode,
2326
2349
  if (!variant) return null;
2327
2350
  const propertyName = discriminator.propertyName;
2328
2351
  const seen = /* @__PURE__ */ new Set([member.$ref]);
2352
+ /** Whether the variant, or a schema it composes, pins the discriminator property to literal values. */
2329
2353
  function constrains(v) {
2330
2354
  const prop = v.properties?.[propertyName];
2331
2355
  const resolved = prop && isReference(prop) ? refs.resolve(prop.$ref, { report: false }) : prop;
2332
- if (resolved && (Array.isArray(resolved.enum) || resolved.const !== void 0)) return true;
2356
+ const pins = (candidate) => Array.isArray(candidate.enum) || candidate.const !== void 0;
2357
+ const members = (resolved?.anyOf ?? resolved?.oneOf ?? []).filter((member) => member.type !== "null");
2358
+ if (resolved && (pins(resolved) || members.length > 0 && members.every(pins))) return true;
2333
2359
  const composition = v.allOf ?? v.oneOf ?? v.anyOf;
2334
2360
  if (!composition) return false;
2335
2361
  return composition.some((m) => {
@@ -2498,13 +2524,13 @@ function convertAnnotatedEnum(context) {
2498
2524
  function collectSiblingProperties(allOfMembers, refs, outerProperties) {
2499
2525
  const properties = { ...outerProperties };
2500
2526
  const visited = /* @__PURE__ */ new Set();
2527
+ /** Adds the properties of one member, following its `$ref` and its own `allOf`. */
2501
2528
  function collectFrom(item) {
2502
2529
  if (!item) return;
2503
2530
  if (isReference(item)) {
2504
2531
  if (visited.has(item.$ref)) return;
2505
2532
  visited.add(item.$ref);
2506
- const resolved = refs.resolve(item.$ref, { report: false });
2507
- if (resolved && !isReference(resolved)) collectFrom(resolved);
2533
+ collectFrom(refs.resolve(item.$ref, { report: false }) ?? void 0);
2508
2534
  return;
2509
2535
  }
2510
2536
  if (item.properties) Object.assign(properties, item.properties);
@@ -2694,7 +2720,12 @@ function convertAllOf({ schema: original, name, nullable, defaultValue, parse, r
2694
2720
  if (missingRequired.length) {
2695
2721
  const resolvedMembers = schema.allOf.flatMap((item) => {
2696
2722
  if (!isReference(item)) return [item];
2697
- const deref = refs.resolve(item.$ref);
2723
+ const seen = /* @__PURE__ */ new Set();
2724
+ let deref = item;
2725
+ while (deref && isReference(deref) && !seen.has(deref.$ref)) {
2726
+ seen.add(deref.$ref);
2727
+ deref = refs.resolve(deref.$ref);
2728
+ }
2698
2729
  return deref && !isReference(deref) ? [deref] : [];
2699
2730
  });
2700
2731
  for (const key of missingRequired) for (const resolved of resolvedMembers) {
@@ -3389,12 +3420,20 @@ function createSchemaParser(ctx) {
3389
3420
  ...DEFAULT_PARSER_OPTIONS,
3390
3421
  ...ctx.options
3391
3422
  };
3423
+ /** Converts one OpenAPI schema into a `SchemaNode`, through the first matching rule of `schemaRules`. */
3392
3424
  function parseSchema({ schema, name }) {
3393
- const flattenedSchema = flattenSchema(schema);
3394
- if (flattenedSchema !== schema) return parseSchema({
3395
- schema: flattenedSchema,
3396
- name
3425
+ if (typeof schema === "boolean") return ast.factory.createSchema({
3426
+ type: schema ? options.emptySchemaType : "never",
3427
+ name: name ?? void 0
3397
3428
  });
3429
+ const flattenedSchema = flattenSchema(schema);
3430
+ if (flattenedSchema !== schema) {
3431
+ const { if: _if, then: _then, else: _else, dependentRequired: _required, dependentSchemas: _schemas, ...shape } = flattenedSchema;
3432
+ return withConditionals(parseSchema({
3433
+ schema: shape,
3434
+ name
3435
+ }), [schema, ...schema.allOf]);
3436
+ }
3398
3437
  if (hasUnevaluatedSiblings(schema)) {
3399
3438
  const { unevaluatedProperties: _properties, unevaluatedItems: _items, ...open } = schema;
3400
3439
  return parseSchema({
@@ -3416,11 +3455,12 @@ function createSchemaParser(ctx) {
3416
3455
  renames
3417
3456
  };
3418
3457
  const rule = schemaRules.find((entry) => entry.match(context));
3419
- return withConditionals(rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType }), schema);
3458
+ return withConditionals(rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType }), [schema, ...Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : []]);
3420
3459
  }
3460
+ const presenceKeys = /* @__PURE__ */ new WeakMap();
3421
3461
  function requireKeys(keys) {
3422
3462
  const unknown = ast.factory.createSchema({ type: ast.schemaTypes.unknown });
3423
- return ast.factory.createSchema({
3463
+ const node = ast.factory.createSchema({
3424
3464
  type: ast.schemaTypes.object,
3425
3465
  primitive: "object",
3426
3466
  properties: keys.map((name) => ast.factory.createProperty({
@@ -3429,17 +3469,35 @@ function createSchemaParser(ctx) {
3429
3469
  schema: unknown
3430
3470
  }))
3431
3471
  });
3472
+ presenceKeys.set(node, keys.join("\0"));
3473
+ return node;
3432
3474
  }
3433
3475
  /**
3434
3476
  * Adds `if`/`then`/`else`, `dependentRequired` and `dependentSchemas` to the node as `conditionals`.
3435
3477
  */
3436
- function withConditionals(node, schema) {
3437
- const conditionals = [schema, ...Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : []].flatMap(toConditionals);
3438
- return conditionals.length ? {
3478
+ function withConditionals(node, sources) {
3479
+ const conditionals = sources.flatMap(toConditionals);
3480
+ const existing = new Set((node.conditionals ?? []).map(presenceKey));
3481
+ const added = conditionals.filter((entry) => {
3482
+ const key = presenceKey(entry);
3483
+ return key === void 0 || !existing.has(key);
3484
+ });
3485
+ return added.length ? {
3439
3486
  ...node,
3440
- conditionals
3487
+ conditionals: [...node.conditionals ?? [], ...added]
3441
3488
  } : node;
3442
3489
  }
3490
+ /**
3491
+ * The keys an entry checks for presence, or `undefined` when a branch checks more than presence and the entry is always kept.
3492
+ */
3493
+ function presenceKey({ if: condition, then: thenNode, else: elseNode }) {
3494
+ const keys = [
3495
+ condition,
3496
+ thenNode,
3497
+ elseNode
3498
+ ].map((branch) => branch ? presenceKeys.get(branch) : "");
3499
+ return keys.includes(void 0) ? void 0 : keys.join("\n");
3500
+ }
3443
3501
  function toConditionals(schema) {
3444
3502
  const branch = (value) => {
3445
3503
  if (!value || typeof value !== "object") return void 0;