@kubb/adapter-oas 5.0.0-beta.94 → 5.0.0-beta.96

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,7 +1,6 @@
1
1
  import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
2
2
  import { ast } from "@kubb/ast";
3
3
  import { AdapterFactoryOptions } from "@kubb/core";
4
-
5
4
  //#region ../../node_modules/.pnpm/@types+json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts
6
5
  // ==================================================================================================
7
6
  // JSON Schema Draft 04
@@ -10,12 +9,12 @@ import { AdapterFactoryOptions } from "@kubb/core";
10
9
  * @see https://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1
11
10
  */
12
11
  type JSONSchema4TypeName = "string" //
13
- | "number" | "integer" | "boolean" | "object" | "array" | "null" | "any";
12
+ | "number" | "integer" | "boolean" | "object" | "array" | "null" | "any";
14
13
  /**
15
14
  * @see https://tools.ietf.org/html/draft-zyp-json-schema-04#section-3.5
16
15
  */
17
16
  type JSONSchema4Type = string //
18
- | number | boolean | JSONSchema4Object | JSONSchema4Array | null;
17
+ | number | boolean | JSONSchema4Object | JSONSchema4Array | null;
19
18
  // Workaround for infinite type recursion
20
19
  interface JSONSchema4Object {
21
20
  [key: string]: JSONSchema4Type;
@@ -204,9 +203,9 @@ interface JSONSchema4 {
204
203
  // JSON Schema Draft 06
205
204
  // ==================================================================================================
206
205
  type JSONSchema6TypeName = "string" //
207
- | "number" | "integer" | "boolean" | "object" | "array" | "null" | "any";
206
+ | "number" | "integer" | "boolean" | "object" | "array" | "null" | "any";
208
207
  type JSONSchema6Type = string //
209
- | number | boolean | JSONSchema6Object | JSONSchema6Array | null;
208
+ | number | boolean | JSONSchema6Object | JSONSchema6Array | null;
210
209
  // Workaround for infinite type recursion
211
210
  interface JSONSchema6Object {
212
211
  [key: string]: JSONSchema6Type;
@@ -472,13 +471,13 @@ interface JSONSchema6 {
472
471
  * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
473
472
  */
474
473
  type JSONSchema7TypeName = "string" //
475
- | "number" | "integer" | "boolean" | "object" | "array" | "null";
474
+ | "number" | "integer" | "boolean" | "object" | "array" | "null";
476
475
  /**
477
476
  * Primitive type
478
477
  * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
479
478
  */
480
479
  type JSONSchema7Type = string //
481
- | number | boolean | JSONSchema7Object | JSONSchema7Array | null;
480
+ | number | boolean | JSONSchema7Object | JSONSchema7Array | null;
482
481
  // Workaround for infinite type recursion
483
482
  interface JSONSchema7Object {
484
483
  [key: string]: JSONSchema7Type;
@@ -634,7 +633,7 @@ declare namespace OpenAPIV3_1 {
634
633
  export type PathItemObject<T extends {} = {}> = Modify<OpenAPIV3.PathItemObject<T>, {
635
634
  servers?: ServerObject[];
636
635
  parameters?: (ReferenceObject | ParameterObject)[];
637
- }> & { [method in HttpMethods]?: OperationObject<T> };
636
+ }> & { [method in HttpMethods]?: OperationObject<T>; };
638
637
  export type OperationObject<T extends {} = {}> = Modify<OpenAPIV3.OperationObject<T>, {
639
638
  parameters?: (ReferenceObject | ParameterObject)[];
640
639
  requestBody?: ReferenceObject | RequestBodyObject;
@@ -799,7 +798,7 @@ declare namespace OpenAPIV3 {
799
798
  description?: string;
800
799
  servers?: ServerObject[];
801
800
  parameters?: (ReferenceObject | ParameterObject)[];
802
- } & { [method in HttpMethods]?: OperationObject<T> };
801
+ } & { [method in HttpMethods]?: OperationObject<T>; };
803
802
  type OperationObject<T extends {} = {}> = {
804
803
  tags?: string[];
805
804
  summary?: string;
@@ -1240,7 +1239,7 @@ type AdapterOasOptions = {
1240
1239
  enums?: 'inline' | 'root';
1241
1240
  } & Partial<ast.ParserOptions>;
1242
1241
  /**
1243
- * Adapter options after defaults have been applied and schema name collisions resolved.
1242
+ * Adapter options after defaults have been applied.
1244
1243
  */
1245
1244
  type AdapterOasResolvedOptions = {
1246
1245
  validate: boolean;
@@ -1253,17 +1252,6 @@ type AdapterOasResolvedOptions = {
1253
1252
  unknownType: NonNullable<AdapterOasOptions['unknownType']>;
1254
1253
  emptySchemaType: NonNullable<AdapterOasOptions['emptySchemaType']>;
1255
1254
  enumSuffix: AdapterOasOptions['enumSuffix'];
1256
- /**
1257
- * Map from original `$ref` paths to their collision-resolved schema names.
1258
- * Populated once the adapter resolves a spec's schemas, on the first `parse()`.
1259
- *
1260
- * @example
1261
- * ```ts
1262
- * nameMapping.get('#/components/schemas/Order') // 'Order'
1263
- * nameMapping.get('#/components/responses/Order') // 'OrderResponse'
1264
- * ```
1265
- */
1266
- nameMapping: Map<string, string>;
1267
1255
  };
1268
1256
  /**
1269
1257
  * `@kubb/core` adapter factory type for the OpenAPI adapter.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./rolldown-runtime-C0LytTxp.js";
2
- import { ast, childName, collect, enumPropName, extractRefName, findCircularSchemas, macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion, mergeAdjacentObjectsLazy, narrowSchema } from "@kubb/ast";
2
+ import { ast, childName, enumPropName, extractRefName, findCircularSchemas, macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion, mergeAdjacentObjectsLazy, narrowSchema } from "@kubb/ast";
3
3
  import { Diagnostics, createAdapter } from "@kubb/core";
4
4
  import path from "node:path";
5
5
  import { access, readFile } from "node:fs/promises";
@@ -1014,7 +1014,7 @@ function resolveSchemaRef(document, schema) {
1014
1014
  *
1015
1015
  * @example
1016
1016
  * ```ts
1017
- * const { schemas, nameMapping } = getSchemas(document, { contentType: 'application/json' })
1017
+ * const { schemas, renames } = getSchemas(document, { contentType: 'application/json' })
1018
1018
  * ```
1019
1019
  */
1020
1020
  function getSchemas(document, { contentType }) {
@@ -1039,7 +1039,7 @@ function getSchemas(document, { contentType }) {
1039
1039
  normalizedNames.set(key, bucket);
1040
1040
  }
1041
1041
  const schemas = {};
1042
- const nameMapping = /* @__PURE__ */ new Map();
1042
+ const renames = /* @__PURE__ */ new Map();
1043
1043
  for (const [, items] of normalizedNames) {
1044
1044
  const isSingle = items.length === 1;
1045
1045
  let hasMultipleSources = false;
@@ -1054,12 +1054,12 @@ function getSchemas(document, { contentType }) {
1054
1054
  const suffix = isSingle ? "" : hasMultipleSources ? semanticSuffixes[item.source] : index === 0 ? "" : String(index + 1);
1055
1055
  const uniqueName = item.originalName + suffix;
1056
1056
  schemas[uniqueName] = item.schema;
1057
- nameMapping.set(`#/components/${item.source}/${item.originalName}`, uniqueName);
1057
+ if (suffix) renames.set(`#/components/${item.source}/${item.originalName}`, uniqueName);
1058
1058
  });
1059
1059
  }
1060
1060
  return {
1061
1061
  schemas: sortSchemas(schemas),
1062
- nameMapping
1062
+ renames
1063
1063
  };
1064
1064
  }
1065
1065
  /**
@@ -1225,18 +1225,20 @@ function nameEnums(node, options) {
1225
1225
  * Use `syncSchemaRef(node)` in printers to get a merged view of both.
1226
1226
  * Circular refs are detected in `resolveRefNode` and leave `schema` as `null`.
1227
1227
  */
1228
- function convertRef({ schema, name, nullable, defaultValue, rawOptions, document, resolveRefNode, refExists }) {
1228
+ function convertRef({ schema, name, nullable, defaultValue, rawOptions, document, resolveRefNode, refExists, renames }) {
1229
1229
  const refPath = schema.$ref;
1230
1230
  const resolvedSchema = refPath ? resolveRefNode(refPath, rawOptions) : null;
1231
1231
  if (refPath && document.components && !refExists(refPath)) return ast.factory.createSchema({
1232
1232
  ...buildSchemaNode(schema, name, nullable, defaultValue),
1233
1233
  type: "unknown"
1234
1234
  });
1235
+ const targetName = renames?.get(schema.$ref);
1235
1236
  return ast.factory.createSchema({
1236
1237
  ...buildSchemaNode(schema, name, nullable, defaultValue),
1237
1238
  type: "ref",
1238
1239
  name: extractRefName(schema.$ref),
1239
1240
  ref: schema.$ref,
1241
+ ...targetName ? { targetName } : {},
1240
1242
  schema: resolvedSchema
1241
1243
  });
1242
1244
  }
@@ -1877,7 +1879,8 @@ function createSchemaParser(ctx) {
1877
1879
  parse: parseSchema,
1878
1880
  document,
1879
1881
  resolveRefNode,
1880
- refExists
1882
+ refExists,
1883
+ renames: ctx.renames
1881
1884
  };
1882
1885
  for (const rule of schemaRules) {
1883
1886
  if (!rule.match(context)) continue;
@@ -2178,7 +2181,6 @@ const adapterOas = createAdapter((options) => {
2178
2181
  emptySchemaType,
2179
2182
  enumSuffix
2180
2183
  };
2181
- let nameMapping = /* @__PURE__ */ new Map();
2182
2184
  let parsedDocument = null;
2183
2185
  const documentCache = /* @__PURE__ */ new WeakMap();
2184
2186
  const schemasCache = /* @__PURE__ */ new WeakMap();
@@ -2199,16 +2201,16 @@ const adapterOas = createAdapter((options) => {
2199
2201
  const cached = schemasCache.get(document);
2200
2202
  if (cached) return cached;
2201
2203
  const result = getSchemas(document, { contentType });
2202
- nameMapping = result.nameMapping;
2203
- schemasCache.set(document, result.schemas);
2204
- return result.schemas;
2204
+ schemasCache.set(document, result);
2205
+ return result;
2205
2206
  }
2206
- function ensureSchemaParser(document) {
2207
+ function ensureSchemaParser({ document, renames }) {
2207
2208
  const cached = schemaParserCache.get(document);
2208
2209
  if (cached) return cached;
2209
2210
  const parser = createSchemaParser({
2210
2211
  document,
2211
- contentType
2212
+ contentType,
2213
+ renames
2212
2214
  });
2213
2215
  schemaParserCache.set(document, parser);
2214
2216
  return parser;
@@ -2290,8 +2292,7 @@ const adapterOas = createAdapter((options) => {
2290
2292
  integerType,
2291
2293
  unknownType,
2292
2294
  emptySchemaType,
2293
- enumSuffix,
2294
- nameMapping
2295
+ enumSuffix
2295
2296
  };
2296
2297
  },
2297
2298
  get document() {
@@ -2301,24 +2302,16 @@ const adapterOas = createAdapter((options) => {
2301
2302
  await assertInputExists(input);
2302
2303
  await validateDocument(await parseDocument(input), options);
2303
2304
  },
2304
- getImports(node, resolve) {
2305
- return collect(node, { schema(schemaNode) {
2306
- const schemaRef = narrowSchema(schemaNode, "ref");
2307
- if (!schemaRef?.ref) return null;
2308
- const result = resolve(nameMapping.get(schemaRef.ref) ?? extractRefName(schemaRef.ref));
2309
- if (!result) return null;
2310
- return ast.factory.createImport({
2311
- name: [result.name],
2312
- path: result.path
2313
- });
2314
- } });
2315
- },
2316
2305
  async parse(source) {
2317
2306
  const document = await ensureDocument(source);
2307
+ const { schemas, renames } = ensureSchemas(document);
2318
2308
  return parseInput({
2319
2309
  document,
2320
- schemas: ensureSchemas(document),
2321
- parser: ensureSchemaParser(document)
2310
+ schemas,
2311
+ parser: ensureSchemaParser({
2312
+ document,
2313
+ renames
2314
+ })
2322
2315
  });
2323
2316
  }
2324
2317
  };