@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.cjs CHANGED
@@ -36,7 +36,7 @@ var compatibility = {
36
36
  "node": ">=22.22"
37
37
  };
38
38
  // package.json
39
- var version = "5.18.0";
39
+ var version = "5.18.2";
40
40
  // src/constants.ts
41
41
  /**
42
42
  * Default parser options applied when no explicit options are provided.
@@ -264,7 +264,7 @@ function patchDiscriminatorNode(node, entry) {
264
264
  * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
265
265
  */
266
266
  function convert(text, pascal) {
267
- 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) => {
267
+ 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) => {
268
268
  if (word.length > 1 && word === word.toUpperCase()) return word;
269
269
  return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
270
270
  }).join("").replace(/[^a-zA-Z0-9]/g, "");
@@ -1225,8 +1225,29 @@ const semanticSuffixes = {
1225
1225
  */
1226
1226
  function getSchemas(document, { contentType }, refs) {
1227
1227
  const components = document.components;
1228
+ /**
1229
+ * Follows a `$ref` to the schema it ends at. Keywords next to a `$ref` apply together with its target,
1230
+ * so they stay on a one-member `allOf` around it.
1231
+ */
1232
+ function inlineRef(schema, seen = /* @__PURE__ */ new Set()) {
1233
+ if (!isReference(schema) || seen.has(schema.$ref)) return schema;
1234
+ const { $ref, ...siblings } = schema;
1235
+ const resolved = refs.resolve($ref);
1236
+ if (!resolved) return schema;
1237
+ const target = inlineRef(resolved, seen.add($ref));
1238
+ if (!Object.keys(siblings).length) return target;
1239
+ const own = "allOf" in siblings && Array.isArray(siblings.allOf) ? siblings.allOf : [];
1240
+ return {
1241
+ ...siblings,
1242
+ allOf: [...own, target]
1243
+ };
1244
+ }
1245
+ /**
1246
+ * 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.
1247
+ */
1228
1248
  function resolveSchemaRef(schema) {
1229
1249
  if (!isReference(schema)) return schema;
1250
+ if (Object.keys(schema).length > 1) return inlineRef(schema);
1230
1251
  const resolved = refs.resolve(schema.$ref);
1231
1252
  return resolved && !isReference(resolved) ? resolved : schema;
1232
1253
  }
@@ -1992,6 +2013,7 @@ function parseParameter({ param, pointer, parentName, options, parseSchema, refs
1992
2013
  required: param.required ?? false,
1993
2014
  ...pickStyleFields(param),
1994
2015
  ...contentType !== void 0 ? { contentType } : {},
2016
+ ...mediaType?.encoding && { encoding: getEncodingFields({ encoding: mediaType.encoding }).encoding },
1995
2017
  ...getExamples(mediaType ?? param, refs),
1996
2018
  ...param.deprecated && { deprecated: true },
1997
2019
  ...param.allowEmptyValue && { allowEmptyValue: true }
@@ -2060,12 +2082,13 @@ function parseOperation({ operation, ...ctx }) {
2060
2082
  required: bodyRequired || void 0,
2061
2083
  content: content.length > 0 ? content : void 0
2062
2084
  } : void 0;
2063
- const responses = getResponseStatusCodes(operation).map((statusCode) => {
2085
+ const responses = getResponseStatusCodes(operation).map((key) => {
2064
2086
  const response = getResponseByStatusCode({
2065
2087
  operation,
2066
2088
  refs,
2067
- statusCode
2089
+ statusCode: key
2068
2090
  });
2091
+ const statusCode = key.toLowerCase();
2069
2092
  const responseName = operationName ? `${operationName}Status${statusCode}` : void 0;
2070
2093
  const parseEntrySchema = (ct) => {
2071
2094
  const raw = getBodySchema({
@@ -2083,7 +2106,7 @@ function parseOperation({ operation, ...ctx }) {
2083
2106
  schema: raw,
2084
2107
  name: responseName
2085
2108
  }) : _kubb_kit.ast.factory.createSchema({ type: options.emptySchemaType });
2086
- if (raw && ct) scanSchema(node, `${mediaTypePointer(response?.content, ct, `${pointer}/responses/${statusCode}/content/${escapePointerToken(ct)}`)}/${schemaKeyword(mediaType)}`);
2109
+ if (raw && ct) scanSchema(node, `${mediaTypePointer(response?.content, ct, `${pointer}/responses/${key}/content/${escapePointerToken(ct)}`)}/${schemaKeyword(mediaType)}`);
2087
2110
  return {
2088
2111
  schema: node,
2089
2112
  keysToOmit: collectPropertyKeysByFlag(raw, "writeOnly"),
@@ -2107,7 +2130,7 @@ function parseOperation({ operation, ...ctx }) {
2107
2130
  contentType: "",
2108
2131
  ...parseEntrySchema(contentType)
2109
2132
  }));
2110
- const responsePointer = `${pointer}/responses/${statusCode}`;
2133
+ const responsePointer = `${pointer}/responses/${key}`;
2111
2134
  const headers = Object.entries(response?.headers ?? {}).flatMap(([name, value]) => {
2112
2135
  const header = name.toLowerCase() === "content-type" ? null : refs.deref(value);
2113
2136
  if (!header) return [];
@@ -2141,7 +2164,7 @@ function parseOperation({ operation, ...ctx }) {
2141
2164
  const fallback = operation.pathItem[key];
2142
2165
  return typeof fallback === "string" ? fallback : void 0;
2143
2166
  };
2144
- const servers = operation.schema.servers ?? operation.pathItem.servers;
2167
+ const servers = operation.schema.servers?.length ? operation.schema.servers : operation.pathItem.servers;
2145
2168
  const auth = toAuthRequirements(operation.schema.security ?? document.security, document);
2146
2169
  return _kubb_kit.ast.factory.createOperation({
2147
2170
  pointer,
@@ -2342,10 +2365,13 @@ function narrowUnionMembers({ unionMembers, discriminator, sharedPropertiesNode,
2342
2365
  if (!variant) return null;
2343
2366
  const propertyName = discriminator.propertyName;
2344
2367
  const seen = /* @__PURE__ */ new Set([member.$ref]);
2368
+ /** Whether the variant, or a schema it composes, pins the discriminator property to literal values. */
2345
2369
  function constrains(v) {
2346
2370
  const prop = v.properties?.[propertyName];
2347
2371
  const resolved = prop && isReference(prop) ? refs.resolve(prop.$ref, { report: false }) : prop;
2348
- if (resolved && (Array.isArray(resolved.enum) || resolved.const !== void 0)) return true;
2372
+ const pins = (candidate) => Array.isArray(candidate.enum) || candidate.const !== void 0;
2373
+ const members = (resolved?.anyOf ?? resolved?.oneOf ?? []).filter((member) => member.type !== "null");
2374
+ if (resolved && (pins(resolved) || members.length > 0 && members.every(pins))) return true;
2349
2375
  const composition = v.allOf ?? v.oneOf ?? v.anyOf;
2350
2376
  if (!composition) return false;
2351
2377
  return composition.some((m) => {
@@ -2514,13 +2540,13 @@ function convertAnnotatedEnum(context) {
2514
2540
  function collectSiblingProperties(allOfMembers, refs, outerProperties) {
2515
2541
  const properties = { ...outerProperties };
2516
2542
  const visited = /* @__PURE__ */ new Set();
2543
+ /** Adds the properties of one member, following its `$ref` and its own `allOf`. */
2517
2544
  function collectFrom(item) {
2518
2545
  if (!item) return;
2519
2546
  if (isReference(item)) {
2520
2547
  if (visited.has(item.$ref)) return;
2521
2548
  visited.add(item.$ref);
2522
- const resolved = refs.resolve(item.$ref, { report: false });
2523
- if (resolved && !isReference(resolved)) collectFrom(resolved);
2549
+ collectFrom(refs.resolve(item.$ref, { report: false }) ?? void 0);
2524
2550
  return;
2525
2551
  }
2526
2552
  if (item.properties) Object.assign(properties, item.properties);
@@ -2710,7 +2736,12 @@ function convertAllOf({ schema: original, name, nullable, defaultValue, parse, r
2710
2736
  if (missingRequired.length) {
2711
2737
  const resolvedMembers = schema.allOf.flatMap((item) => {
2712
2738
  if (!isReference(item)) return [item];
2713
- const deref = refs.resolve(item.$ref);
2739
+ const seen = /* @__PURE__ */ new Set();
2740
+ let deref = item;
2741
+ while (deref && isReference(deref) && !seen.has(deref.$ref)) {
2742
+ seen.add(deref.$ref);
2743
+ deref = refs.resolve(deref.$ref);
2744
+ }
2714
2745
  return deref && !isReference(deref) ? [deref] : [];
2715
2746
  });
2716
2747
  for (const key of missingRequired) for (const resolved of resolvedMembers) {
@@ -3405,12 +3436,20 @@ function createSchemaParser(ctx) {
3405
3436
  ...DEFAULT_PARSER_OPTIONS,
3406
3437
  ...ctx.options
3407
3438
  };
3439
+ /** Converts one OpenAPI schema into a `SchemaNode`, through the first matching rule of `schemaRules`. */
3408
3440
  function parseSchema({ schema, name }) {
3409
- const flattenedSchema = flattenSchema(schema);
3410
- if (flattenedSchema !== schema) return parseSchema({
3411
- schema: flattenedSchema,
3412
- name
3441
+ if (typeof schema === "boolean") return _kubb_kit.ast.factory.createSchema({
3442
+ type: schema ? options.emptySchemaType : "never",
3443
+ name: name ?? void 0
3413
3444
  });
3445
+ const flattenedSchema = flattenSchema(schema);
3446
+ if (flattenedSchema !== schema) {
3447
+ const { if: _if, then: _then, else: _else, dependentRequired: _required, dependentSchemas: _schemas, ...shape } = flattenedSchema;
3448
+ return withConditionals(parseSchema({
3449
+ schema: shape,
3450
+ name
3451
+ }), [schema, ...schema.allOf]);
3452
+ }
3414
3453
  if (hasUnevaluatedSiblings(schema)) {
3415
3454
  const { unevaluatedProperties: _properties, unevaluatedItems: _items, ...open } = schema;
3416
3455
  return parseSchema({
@@ -3432,11 +3471,12 @@ function createSchemaParser(ctx) {
3432
3471
  renames
3433
3472
  };
3434
3473
  const rule = schemaRules.find((entry) => entry.match(context));
3435
- return withConditionals(rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType }), schema);
3474
+ return withConditionals(rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType }), [schema, ...Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : []]);
3436
3475
  }
3476
+ const presenceKeys = /* @__PURE__ */ new WeakMap();
3437
3477
  function requireKeys(keys) {
3438
3478
  const unknown = _kubb_kit.ast.factory.createSchema({ type: _kubb_kit.ast.schemaTypes.unknown });
3439
- return _kubb_kit.ast.factory.createSchema({
3479
+ const node = _kubb_kit.ast.factory.createSchema({
3440
3480
  type: _kubb_kit.ast.schemaTypes.object,
3441
3481
  primitive: "object",
3442
3482
  properties: keys.map((name) => _kubb_kit.ast.factory.createProperty({
@@ -3445,17 +3485,35 @@ function createSchemaParser(ctx) {
3445
3485
  schema: unknown
3446
3486
  }))
3447
3487
  });
3488
+ presenceKeys.set(node, keys.join("\0"));
3489
+ return node;
3448
3490
  }
3449
3491
  /**
3450
3492
  * Adds `if`/`then`/`else`, `dependentRequired` and `dependentSchemas` to the node as `conditionals`.
3451
3493
  */
3452
- function withConditionals(node, schema) {
3453
- const conditionals = [schema, ...Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : []].flatMap(toConditionals);
3454
- return conditionals.length ? {
3494
+ function withConditionals(node, sources) {
3495
+ const conditionals = sources.flatMap(toConditionals);
3496
+ const existing = new Set((node.conditionals ?? []).map(presenceKey));
3497
+ const added = conditionals.filter((entry) => {
3498
+ const key = presenceKey(entry);
3499
+ return key === void 0 || !existing.has(key);
3500
+ });
3501
+ return added.length ? {
3455
3502
  ...node,
3456
- conditionals
3503
+ conditionals: [...node.conditionals ?? [], ...added]
3457
3504
  } : node;
3458
3505
  }
3506
+ /**
3507
+ * The keys an entry checks for presence, or `undefined` when a branch checks more than presence and the entry is always kept.
3508
+ */
3509
+ function presenceKey({ if: condition, then: thenNode, else: elseNode }) {
3510
+ const keys = [
3511
+ condition,
3512
+ thenNode,
3513
+ elseNode
3514
+ ].map((branch) => branch ? presenceKeys.get(branch) : "");
3515
+ return keys.includes(void 0) ? void 0 : keys.join("\n");
3516
+ }
3459
3517
  function toConditionals(schema) {
3460
3518
  const branch = (value) => {
3461
3519
  if (!value || typeof value !== "object") return void 0;