@kubb/adapter-oas 5.29.4 → 5.30.0

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
@@ -15,7 +15,7 @@ var compatibility = {
15
15
  "node": ">=22.22"
16
16
  };
17
17
  // package.json
18
- var version = "5.29.4";
18
+ var version = "5.30.0";
19
19
  // src/constants.ts
20
20
  /**
21
21
  * Default parser options applied when no explicit options are provided.
@@ -171,9 +171,6 @@ const enumExtensionKeys = ["x-enumNames", "x-enum-varnames"];
171
171
  */
172
172
  const enumDescriptionKeys = ["x-enumDescriptions", "x-enum-descriptions"];
173
173
  // src/emit/discriminator/propagate.ts
174
- function findUnion(schema) {
175
- return _kubb_kit.ast.schema.narrow(schema, "union") ?? _kubb_kit.ast.schema.narrow(schema, "intersection")?.members?.map((member) => _kubb_kit.ast.schema.narrow(member, "union")).find((union) => union);
176
- }
177
174
  function pinnedValues({ objNode, propertyName }) {
178
175
  const prop = objNode.properties.find((p) => p.name === propertyName);
179
176
  return (prop ? _kubb_kit.ast.schema.narrow(prop.schema, "enum") : null)?.enumValues?.filter((v) => v !== null) ?? [];
@@ -193,17 +190,19 @@ function memberDiscriminant({ member, propertyName }) {
193
190
  } : null;
194
191
  }
195
192
  /**
196
- * Maps each child schema's emitted name to its discriminator patch data by scanning the given
197
- * top-level AST schema nodes for union schemas that carry a `discriminatorPropertyName`.
193
+ * Maps each child schema's emitted name to its discriminator patch data, one entry per discriminator property, by scanning the given
194
+ * AST schema nodes, and the schemas nested in them, for union schemas that carry a `discriminatorPropertyName`.
195
+ * A union can sit at the root, inside an intersection (a union with shared properties), on a property, or on array `items`.
196
+ * Ref targets are not followed.
198
197
  *
199
198
  * Called on a small pre-parsed subset of schemas (only the discriminator parents)
200
199
  * rather than on all schemas at once.
201
200
  */
202
201
  function buildDiscriminatorChildMap(schemas) {
203
202
  const childMap = /* @__PURE__ */ new Map();
204
- for (const schema of schemas) {
205
- const unionNode = findUnion(schema);
206
- if (!unionNode?.discriminatorPropertyName || !unionNode.members) continue;
203
+ const unions = schemas.flatMap((schema) => _kubb_kit.ast.node.collect(schema, { schema: (node) => _kubb_kit.ast.schema.narrow(node, "union") }));
204
+ for (const unionNode of unions) {
205
+ if (!unionNode.discriminatorPropertyName || !unionNode.members) continue;
207
206
  const propertyName = unionNode.discriminatorPropertyName;
208
207
  for (const member of unionNode.members) {
209
208
  const child = memberDiscriminant({
@@ -211,12 +210,14 @@ function buildDiscriminatorChildMap(schemas) {
211
210
  propertyName
212
211
  });
213
212
  if (!child) continue;
214
- const existing = childMap.get(child.name);
215
- const enumValues = [.../* @__PURE__ */ new Set([...existing?.enumValues ?? [], ...child.enumValues])];
216
- childMap.set(child.name, {
217
- propertyName: existing?.propertyName ?? propertyName,
218
- enumValues
213
+ const targets = childMap.get(child.name) ?? [];
214
+ const existing = targets.find((target) => target.propertyName === propertyName);
215
+ if (existing) existing.enumValues = [.../* @__PURE__ */ new Set([...existing.enumValues, ...child.enumValues])];
216
+ else targets.push({
217
+ propertyName,
218
+ enumValues: child.enumValues
219
219
  });
220
+ childMap.set(child.name, targets);
220
221
  }
221
222
  }
222
223
  return childMap;
@@ -2473,14 +2474,38 @@ function convertRef({ schema, name, nullable, defaultValue, document, parse, ref
2473
2474
  schema: resolvedSchema
2474
2475
  });
2475
2476
  }
2477
+ /**
2478
+ * `not`, `contains` and the `if`/`then`/`else` branches check a value and are not its type, so a subschema without a
2479
+ * type accepts every value there. A `void` from `emptySchemaType` or `unknownType` would reject them all, so it becomes
2480
+ * `unknown`, also behind a `$ref`.
2481
+ */
2482
+ function acceptAnyValue(node) {
2483
+ return _kubb_kit.ast.node.transform(node, { schema: (child) => {
2484
+ if (child.type === _kubb_kit.ast.schema.types.void) return {
2485
+ ...child,
2486
+ type: _kubb_kit.ast.schema.types.unknown,
2487
+ primitive: "unknown"
2488
+ };
2489
+ if (_kubb_kit.ast.ref.getSchema(child).type === _kubb_kit.ast.schema.types.void) return _kubb_kit.ast.factory.createSchema({ type: _kubb_kit.ast.schema.types.unknown });
2490
+ } });
2491
+ }
2476
2492
  /** Converts `{ ...rest, not: X }` into `intersection[rest, not[X]]`; a bare `required` inside `not` borrows the outer `properties`. */
2477
2493
  function convertNot({ schema, name, nullable, defaultValue, parse }) {
2478
- const { not, ...rest } = schema;
2494
+ const { not, if: _if, then: _then, else: _else, dependentRequired: _required, dependentSchemas: _schemas, ...rest } = schema;
2479
2495
  const [subschema] = resolveUnionMembers([not], rest.properties ?? {});
2480
2496
  const notNode = _kubb_kit.ast.factory.createSchema({
2481
2497
  type: _kubb_kit.ast.schema.types.not,
2482
- members: [parse({ schema: subschema })]
2498
+ members: [acceptAnyValue(parse({ schema: subschema }))]
2483
2499
  });
2500
+ const parsedRest = parse({
2501
+ schema: rest,
2502
+ name
2503
+ });
2504
+ const restNode = parsedRest.type === _kubb_kit.ast.schema.types.void ? {
2505
+ ...parsedRest,
2506
+ type: _kubb_kit.ast.schema.types.unknown,
2507
+ primitive: "unknown"
2508
+ } : parsedRest;
2484
2509
  return createNode({
2485
2510
  schema,
2486
2511
  name,
@@ -2488,10 +2513,7 @@ function convertNot({ schema, name, nullable, defaultValue, parse }) {
2488
2513
  defaultValue
2489
2514
  }, {
2490
2515
  type: _kubb_kit.ast.schema.types.intersection,
2491
- members: [parse({
2492
- schema: rest,
2493
- name
2494
- }), notNode]
2516
+ members: [restNode, notNode]
2495
2517
  });
2496
2518
  }
2497
2519
  /**
@@ -2621,7 +2643,7 @@ function convertAllOf({ schema: original, name, nullable, defaultValue, parse, r
2621
2643
  }
2622
2644
  }
2623
2645
  if (keepsOwnKeywords(schema)) {
2624
- const { allOf: _allOf, ...schemaWithoutAllOf } = schema;
2646
+ const { allOf: _allOf, if: _if, then: _then, else: _else, dependentRequired: _required, dependentSchemas: _schemas, ...schemaWithoutAllOf } = schema;
2625
2647
  allOfMembers.push(parse({ schema: schemaWithoutAllOf }));
2626
2648
  }
2627
2649
  for (const { propertyName, values } of discriminantValues) allOfMembers.push(createDiscriminantNode({
@@ -3890,7 +3912,7 @@ function getContains(schema, parse) {
3890
3912
  const { contains, minContains, maxContains } = schema;
3891
3913
  if (!contains || typeof contains !== "object") return {};
3892
3914
  return { contains: {
3893
- schema: parse({ schema: contains }),
3915
+ schema: acceptAnyValue(parse({ schema: contains })),
3894
3916
  ...typeof minContains === "number" && { min: minContains },
3895
3917
  ...typeof maxContains === "number" && { max: maxContains }
3896
3918
  } };
@@ -4086,7 +4108,7 @@ function formatPrintsPlainType({ schema, options, type }) {
4086
4108
  */
4087
4109
  const schemaRules = [
4088
4110
  {
4089
- match: ({ schema }) => typeof schema.not === "object" && schema.not !== null,
4111
+ match: ({ schema }) => schema.not === true || typeof schema.not === "object" && schema.not !== null,
4090
4112
  convert: convertNot
4091
4113
  },
4092
4114
  {
@@ -4188,6 +4210,12 @@ function hasUnevaluatedSiblings(schema) {
4188
4210
  schema.$ref
4189
4211
  ].some((value) => value !== void 0);
4190
4212
  }
4213
+ const arrayKeywords = [
4214
+ "minItems",
4215
+ "maxItems",
4216
+ "uniqueItems",
4217
+ "contains"
4218
+ ];
4191
4219
  function isKeyList(value) {
4192
4220
  return Array.isArray(value) && value.length > 0 && value.every((name) => typeof name === "string");
4193
4221
  }
@@ -4296,10 +4324,15 @@ var SchemaParser = class {
4296
4324
  return keys.includes(void 0) ? void 0 : keys.join("\n");
4297
4325
  }
4298
4326
  #toBranch(value) {
4327
+ if (typeof value === "boolean") return acceptAnyValue(this.parse({ schema: value }));
4299
4328
  if (!value || typeof value !== "object") return void 0;
4300
4329
  const { required, type, ...rest } = value;
4301
4330
  if (Array.isArray(required) && required.length && (type === void 0 || type === "object") && !Object.keys(rest).length) return this.#requireKeys(required);
4302
- return this.parse({ schema: value });
4331
+ const isArrayCheck = type === void 0 && arrayKeywords.some((key) => rest[key] !== void 0);
4332
+ return acceptAnyValue(this.parse({ schema: isArrayCheck ? {
4333
+ ...value,
4334
+ type: "array"
4335
+ } : value }));
4303
4336
  }
4304
4337
  #toConditionals(schema) {
4305
4338
  const entries = (value) => value && typeof value === "object" ? Object.entries(value) : [];
@@ -4561,8 +4594,16 @@ var Adapter = class {
4561
4594
  };
4562
4595
  }
4563
4596
  };
4564
- function isDiscriminatorParent(schema) {
4565
- return Boolean((schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName);
4597
+ /**
4598
+ * Returns `true` when the schema, or any schema nested in it, is a `oneOf`/`anyOf` with a discriminator,
4599
+ * such as a union declared on a property or on array `items`.
4600
+ */
4601
+ function isDiscriminatorParent(schema, seen = /* @__PURE__ */ new WeakSet()) {
4602
+ if (!schema || typeof schema !== "object" || seen.has(schema)) return false;
4603
+ seen.add(schema);
4604
+ const { oneOf, anyOf, discriminator } = schema;
4605
+ if ((oneOf ?? anyOf) && discriminator?.propertyName) return true;
4606
+ return Object.values(schema).some((value) => isDiscriminatorParent(value, seen));
4566
4607
  }
4567
4608
  function resolveSchemaNodes({ names, parsed: { parsedByName, refAliasMap, discriminatorChildMap }, promotedEnums }) {
4568
4609
  const aliasTargetOf = (name) => {
@@ -4583,8 +4624,7 @@ function resolveSchemaNodes({ names, parsed: { parsedByName, refAliasMap, discri
4583
4624
  const aliasTarget = refAliasMap.get(name)?.name;
4584
4625
  const aliased = aliasTarget ? parsedByName.get(aliasTarget) : void 0;
4585
4626
  const parsed = parsedByName.get(name);
4586
- const child = discriminatorChildMap?.get(name);
4587
- const base = child ? patchDiscriminatorNode(parsed, child) : parsed;
4627
+ const base = (discriminatorChildMap?.get(name) ?? []).reduce(patchDiscriminatorNode, parsed);
4588
4628
  const resolved = aliased ? {
4589
4629
  ...aliased,
4590
4630
  name,