@kubb/adapter-oas 5.18.0 → 5.18.1

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.1";
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
  }
@@ -2060,12 +2081,13 @@ function parseOperation({ operation, ...ctx }) {
2060
2081
  required: bodyRequired || void 0,
2061
2082
  content: content.length > 0 ? content : void 0
2062
2083
  } : void 0;
2063
- const responses = getResponseStatusCodes(operation).map((statusCode) => {
2084
+ const responses = getResponseStatusCodes(operation).map((key) => {
2064
2085
  const response = getResponseByStatusCode({
2065
2086
  operation,
2066
2087
  refs,
2067
- statusCode
2088
+ statusCode: key
2068
2089
  });
2090
+ const statusCode = key.toLowerCase();
2069
2091
  const responseName = operationName ? `${operationName}Status${statusCode}` : void 0;
2070
2092
  const parseEntrySchema = (ct) => {
2071
2093
  const raw = getBodySchema({
@@ -2083,7 +2105,7 @@ function parseOperation({ operation, ...ctx }) {
2083
2105
  schema: raw,
2084
2106
  name: responseName
2085
2107
  }) : _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)}`);
2108
+ if (raw && ct) scanSchema(node, `${mediaTypePointer(response?.content, ct, `${pointer}/responses/${key}/content/${escapePointerToken(ct)}`)}/${schemaKeyword(mediaType)}`);
2087
2109
  return {
2088
2110
  schema: node,
2089
2111
  keysToOmit: collectPropertyKeysByFlag(raw, "writeOnly"),
@@ -2107,7 +2129,7 @@ function parseOperation({ operation, ...ctx }) {
2107
2129
  contentType: "",
2108
2130
  ...parseEntrySchema(contentType)
2109
2131
  }));
2110
- const responsePointer = `${pointer}/responses/${statusCode}`;
2132
+ const responsePointer = `${pointer}/responses/${key}`;
2111
2133
  const headers = Object.entries(response?.headers ?? {}).flatMap(([name, value]) => {
2112
2134
  const header = name.toLowerCase() === "content-type" ? null : refs.deref(value);
2113
2135
  if (!header) return [];
@@ -2141,7 +2163,7 @@ function parseOperation({ operation, ...ctx }) {
2141
2163
  const fallback = operation.pathItem[key];
2142
2164
  return typeof fallback === "string" ? fallback : void 0;
2143
2165
  };
2144
- const servers = operation.schema.servers ?? operation.pathItem.servers;
2166
+ const servers = operation.schema.servers?.length ? operation.schema.servers : operation.pathItem.servers;
2145
2167
  const auth = toAuthRequirements(operation.schema.security ?? document.security, document);
2146
2168
  return _kubb_kit.ast.factory.createOperation({
2147
2169
  pointer,
@@ -3407,10 +3429,13 @@ function createSchemaParser(ctx) {
3407
3429
  };
3408
3430
  function parseSchema({ schema, name }) {
3409
3431
  const flattenedSchema = flattenSchema(schema);
3410
- if (flattenedSchema !== schema) return parseSchema({
3411
- schema: flattenedSchema,
3412
- name
3413
- });
3432
+ if (flattenedSchema !== schema) {
3433
+ const { if: _if, then: _then, else: _else, dependentRequired: _required, dependentSchemas: _schemas, ...shape } = flattenedSchema;
3434
+ return withConditionals(parseSchema({
3435
+ schema: shape,
3436
+ name
3437
+ }), [schema, ...schema.allOf]);
3438
+ }
3414
3439
  if (hasUnevaluatedSiblings(schema)) {
3415
3440
  const { unevaluatedProperties: _properties, unevaluatedItems: _items, ...open } = schema;
3416
3441
  return parseSchema({
@@ -3432,11 +3457,12 @@ function createSchemaParser(ctx) {
3432
3457
  renames
3433
3458
  };
3434
3459
  const rule = schemaRules.find((entry) => entry.match(context));
3435
- return withConditionals(rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType }), schema);
3460
+ return withConditionals(rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType }), [schema, ...Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : []]);
3436
3461
  }
3462
+ const presenceKeys = /* @__PURE__ */ new WeakMap();
3437
3463
  function requireKeys(keys) {
3438
3464
  const unknown = _kubb_kit.ast.factory.createSchema({ type: _kubb_kit.ast.schemaTypes.unknown });
3439
- return _kubb_kit.ast.factory.createSchema({
3465
+ const node = _kubb_kit.ast.factory.createSchema({
3440
3466
  type: _kubb_kit.ast.schemaTypes.object,
3441
3467
  primitive: "object",
3442
3468
  properties: keys.map((name) => _kubb_kit.ast.factory.createProperty({
@@ -3445,17 +3471,35 @@ function createSchemaParser(ctx) {
3445
3471
  schema: unknown
3446
3472
  }))
3447
3473
  });
3474
+ presenceKeys.set(node, keys.join("\0"));
3475
+ return node;
3448
3476
  }
3449
3477
  /**
3450
3478
  * Adds `if`/`then`/`else`, `dependentRequired` and `dependentSchemas` to the node as `conditionals`.
3451
3479
  */
3452
- function withConditionals(node, schema) {
3453
- const conditionals = [schema, ...Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : []].flatMap(toConditionals);
3454
- return conditionals.length ? {
3480
+ function withConditionals(node, sources) {
3481
+ const conditionals = sources.flatMap(toConditionals);
3482
+ const existing = new Set((node.conditionals ?? []).map(presenceKey));
3483
+ const added = conditionals.filter((entry) => {
3484
+ const key = presenceKey(entry);
3485
+ return key === void 0 || !existing.has(key);
3486
+ });
3487
+ return added.length ? {
3455
3488
  ...node,
3456
- conditionals
3489
+ conditionals: [...node.conditionals ?? [], ...added]
3457
3490
  } : node;
3458
3491
  }
3492
+ /**
3493
+ * The keys an entry checks for presence, or `undefined` when a branch checks more than presence and the entry is always kept.
3494
+ */
3495
+ function presenceKey({ if: condition, then: thenNode, else: elseNode }) {
3496
+ const keys = [
3497
+ condition,
3498
+ thenNode,
3499
+ elseNode
3500
+ ].map((branch) => branch ? presenceKeys.get(branch) : "");
3501
+ return keys.includes(void 0) ? void 0 : keys.join("\n");
3502
+ }
3459
3503
  function toConditionals(schema) {
3460
3504
  const branch = (value) => {
3461
3505
  if (!value || typeof value !== "object") return void 0;