@kubb/oas 4.33.2 → 4.33.3

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.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { i as __name } from "./chunk-OuPHjz6n.js";
2
2
  import BaseOas from "oas";
3
- import * as _readme_openapi_parser0 from "@readme/openapi-parser";
4
3
  import * as OasTypes from "oas/types";
5
4
  import { DiscriminatorObject as DiscriminatorObject$1, HttpMethods as HttpMethods$1, MediaTypeObject as MediaTypeObject$1, OASDocument, ParameterObject, ResponseObject as ResponseObject$1, SchemaObject as SchemaObject$1 } from "oas/types";
5
+ import * as oas_normalize_lib_types0 from "oas-normalize/lib/types";
6
6
  import { Operation as Operation$1 } from "oas/operation";
7
7
  import { OpenAPIV3, OpenAPIV3 as OpenAPIV3$1, OpenAPIV3_1, OpenAPIV3_1 as OpenAPIV3_1$1 } from "openapi-types";
8
8
  import { Config } from "@kubb/core";
@@ -61,7 +61,7 @@ declare class Oas extends BaseOas {
61
61
  getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject;
62
62
  getRequestSchema(operation: Operation): SchemaObject | undefined;
63
63
  getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null;
64
- validate(): Promise<_readme_openapi_parser0.ValidationResult>;
64
+ validate(): Promise<oas_normalize_lib_types0.ValidationResult>;
65
65
  flattenSchema(schema: SchemaObject | null): SchemaObject | null;
66
66
  /**
67
67
  * Get schemas from OpenAPI components (schemas, responses, requestBodies).
@@ -136,6 +136,7 @@ declare function isOptional(schema?: SchemaObject$1): boolean;
136
136
  declare function getDefaultValue(schema?: SchemaObject$1): string | undefined;
137
137
  declare function parse(pathOrApi: string | Document, {
138
138
  oasClass,
139
+ canBundle,
139
140
  enablePaths
140
141
  }?: {
141
142
  oasClass?: typeof Oas;
@@ -151,7 +152,7 @@ declare function parseFromConfig(config: Config, oasClass?: typeof Oas): Promise
151
152
  /**
152
153
  * Validate an OpenAPI document using oas-normalize.
153
154
  */
154
- declare function validate(document: Document): Promise<_readme_openapi_parser0.ValidationResult>;
155
+ declare function validate(document: Document): Promise<oas_normalize_lib_types0.ValidationResult>;
155
156
  //#endregion
156
157
  export { DiscriminatorObject, Document, HttpMethod, HttpMethods, KUBB_INLINE_REF_PREFIX, MediaTypeObject, Oas, type OasTypes, type OpenAPIV3, type OpenAPIV3_1, Operation, ReferenceObject, ResponseObject, SchemaObject, contentType, getDefaultValue, isAllOptional, isDiscriminator, isNullable, isOpenApiV3_1Document, isOptional, isParameterObject, isReference, isRequired, merge, parse, parseFromConfig, resolveServerUrl, validate };
157
158
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -2,9 +2,8 @@ import { a as __require, n as __esmMin, o as __toCommonJS, r as __exportAll, s a
2
2
  import jsonpointer from "jsonpointer";
3
3
  import BaseOas from "oas";
4
4
  import { matchesMimeType } from "oas/utils";
5
- import fs from "node:fs";
6
5
  import path from "node:path";
7
- import { bundle } from "@readme/openapi-parser";
6
+ import { bundle, loadConfig } from "@redocly/openapi-core";
8
7
  import { isRef } from "oas/types";
9
8
  import OASNormalize from "oas-normalize";
10
9
  import { isPlainObject, mergeDeep } from "remeda";
@@ -4503,96 +4502,16 @@ function getDefaultValue(schema) {
4503
4502
  }
4504
4503
  if (schema.type === "object" || schema.properties) return "{}";
4505
4504
  }
4506
- /**
4507
- * Recursively collect all external local-file $ref prefixes (e.g. "api-definitions.yml")
4508
- * from an object tree. URL refs (http/https) are ignored.
4509
- */
4510
- function collectExternalFilePaths(obj, files) {
4511
- if (!obj || typeof obj !== "object") return;
4512
- if (Array.isArray(obj)) {
4513
- for (const item of obj) collectExternalFilePaths(item, files);
4514
- return;
4515
- }
4516
- for (const [key, value] of Object.entries(obj)) if (key === "$ref" && typeof value === "string") {
4517
- const hashIdx = value.indexOf("#");
4518
- const filePart = hashIdx > 0 ? value.slice(0, hashIdx) : hashIdx === -1 ? value : "";
4519
- if (filePart && !filePart.startsWith("http://") && !filePart.startsWith("https://")) files.add(filePart);
4520
- } else collectExternalFilePaths(value, files);
4521
- }
4522
- /**
4523
- * Replace all $refs that start with `externalFile#` with the corresponding
4524
- * internal ref (i.e. just the fragment part, `#/...`).
4525
- */
4526
- function replaceExternalRefsInPlace(obj, externalFile) {
4527
- if (!obj || typeof obj !== "object") return;
4528
- if (Array.isArray(obj)) {
4529
- for (const item of obj) replaceExternalRefsInPlace(item, externalFile);
4530
- return;
4531
- }
4532
- const record = obj;
4533
- for (const key of Object.keys(record)) {
4534
- const value = record[key];
4535
- if (key === "$ref" && typeof value === "string" && value.startsWith(`${externalFile}#`)) record[key] = value.slice(externalFile.length);
4536
- else if (value && typeof value === "object") replaceExternalRefsInPlace(value, externalFile);
4537
- }
4538
- }
4539
- /**
4540
- * Before bundling, scan the main spec file for external local-file references and merge
4541
- * their `components` sections into the main document. This ensures that schemas defined
4542
- * in external files (e.g. `api-definitions.yml#/components/schemas/Parcel`) end up in
4543
- * `#/components/schemas/Parcel` of the bundled output, rather than being inlined as
4544
- * anonymous path-based refs.
4545
- *
4546
- * Returns the merged document, or `null` if no external file components were found.
4547
- */
4548
- function mergeExternalFileComponents(mainFilePath) {
4549
- let mainContent;
4550
- try {
4551
- mainContent = fs.readFileSync(mainFilePath, "utf-8");
4552
- } catch {
4553
- return null;
4554
- }
4555
- const mainDoc = import_yaml.parse(mainContent);
4556
- if (!mainDoc || typeof mainDoc !== "object") return null;
4557
- const mainDir = path.dirname(mainFilePath);
4558
- const externalFiles = /* @__PURE__ */ new Set();
4559
- collectExternalFilePaths(mainDoc, externalFiles);
4560
- if (externalFiles.size === 0) return null;
4561
- let hasMergedComponents = false;
4562
- for (const externalFile of externalFiles) {
4563
- const externalFilePath = path.resolve(mainDir, externalFile);
4564
- let externalContent;
4565
- try {
4566
- externalContent = fs.readFileSync(externalFilePath, "utf-8");
4567
- } catch {
4568
- continue;
4569
- }
4570
- const externalDoc = import_yaml.parse(externalContent);
4571
- if (!externalDoc?.components || typeof externalDoc.components !== "object") continue;
4572
- const mainComponents = mainDoc.components ?? {};
4573
- mainDoc.components = mainComponents;
4574
- for (const [componentType, components] of Object.entries(externalDoc.components)) {
4575
- if (!components || typeof components !== "object") continue;
4576
- mainComponents[componentType] = {
4577
- ...components,
4578
- ...mainComponents[componentType] ?? {}
4579
- };
4580
- hasMergedComponents = true;
4581
- }
4582
- }
4583
- if (!hasMergedComponents) return null;
4584
- for (const externalFile of externalFiles) replaceExternalRefsInPlace(mainDoc, externalFile);
4585
- return mainDoc;
4586
- }
4587
- async function parse(pathOrApi, { oasClass = Oas, enablePaths = true } = {}) {
4588
- if (typeof pathOrApi === "string" && !pathOrApi.match(/\n/) && !pathOrApi.match(/^\s*\{/) && enablePaths) try {
4589
- return parse(await bundle(mergeExternalFileComponents(pathOrApi) ?? pathOrApi), {
4590
- oasClass,
4591
- enablePaths
4592
- });
4593
- } catch (e) {
4594
- console.warn(`[kubb] Failed to bundle external $refs in "${pathOrApi}": ${e.message}. Falling back to plain load.`);
4595
- }
4505
+ async function parse(pathOrApi, { oasClass = Oas, canBundle = true, enablePaths = true } = {}) {
4506
+ if (typeof pathOrApi === "string" && canBundle) return parse((await bundle({
4507
+ ref: pathOrApi,
4508
+ config: await loadConfig(),
4509
+ base: pathOrApi
4510
+ })).bundle.parsed, {
4511
+ oasClass,
4512
+ canBundle,
4513
+ enablePaths
4514
+ });
4596
4515
  const document = await new OASNormalize(pathOrApi, {
4597
4516
  enablePaths,
4598
4517
  colorizeErrors: true
@@ -4606,7 +4525,8 @@ async function parse(pathOrApi, { oasClass = Oas, enablePaths = true } = {}) {
4606
4525
  async function merge(pathOrApi, { oasClass = Oas } = {}) {
4607
4526
  const instances = await Promise.all(pathOrApi.map((p) => parse(p, {
4608
4527
  oasClass,
4609
- enablePaths: false
4528
+ enablePaths: false,
4529
+ canBundle: false
4610
4530
  })));
4611
4531
  if (instances.length === 0) throw new Error("No OAS instances provided for merging.");
4612
4532
  return parse(instances.reduce((acc, current) => {
@@ -4991,9 +4911,14 @@ var Oas = class extends BaseOas {
4991
4911
  }
4992
4912
  getParametersSchema(operation, inKey) {
4993
4913
  const { contentType = operation.getContentType() } = this.#options;
4994
- const params = operation.getParameters().map((schema) => {
4995
- return this.dereferenceWithRef(schema);
4996
- }).filter((v) => v.in === inKey);
4914
+ const resolveParams = (params) => params.map((p) => this.dereferenceWithRef(p)).filter((p) => !!p && typeof p === "object" && "in" in p && "name" in p);
4915
+ const operationParams = resolveParams(operation.schema?.parameters || []);
4916
+ const pathItem = this.api?.paths?.[operation.path];
4917
+ const pathLevelParams = resolveParams(pathItem && !isReference(pathItem) && pathItem.parameters ? pathItem.parameters : []);
4918
+ const paramMap = /* @__PURE__ */ new Map();
4919
+ for (const p of pathLevelParams) if (p.name && p.in) paramMap.set(`${p.in}:${p.name}`, p);
4920
+ for (const p of operationParams) if (p.name && p.in) paramMap.set(`${p.in}:${p.name}`, p);
4921
+ const params = Array.from(paramMap.values()).filter((v) => v.in === inKey);
4997
4922
  if (!params.length) return null;
4998
4923
  return params.reduce((schema, pathParameters) => {
4999
4924
  const property = pathParameters.content?.[contentType]?.schema ?? pathParameters.schema;