@skmtc/core 0.42.0 → 0.44.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.
Files changed (39) hide show
  1. package/esm/context/ParseContext.d.ts +4 -2
  2. package/esm/context/ParseContext.js +23 -8
  3. package/esm/context/generateTypes.d.ts +3 -2
  4. package/esm/helpers/refFns.d.ts +8 -3
  5. package/esm/helpers/refFns.js +26 -9
  6. package/esm/oas/operation/Operation.d.ts +19 -4
  7. package/esm/oas/operation/Operation.js +30 -10
  8. package/esm/oas/operation/toEffectiveParameters.d.ts +9 -0
  9. package/esm/oas/operation/toEffectiveParameters.js +27 -0
  10. package/esm/oas/ref/DanglingRefError.d.ts +18 -3
  11. package/esm/oas/ref/DanglingRefError.js +17 -4
  12. package/esm/oas/ref/Ref.d.ts +29 -13
  13. package/esm/oas/ref/Ref.js +63 -38
  14. package/esm/oas/webhook/Webhook.d.ts +10 -0
  15. package/esm/oas/webhook/Webhook.js +13 -1
  16. package/esm/parse/anyOfSystem.js +6 -8
  17. package/esm/parse/v3-0/schema/toSchemasV3.js +3 -3
  18. package/esm/parse/v3-1/schema/toSchemasV3.js +3 -3
  19. package/esm/parse/v3-2/schema/toSchemasV3.js +3 -3
  20. package/package.json +1 -1
  21. package/script/context/ParseContext.d.ts +4 -2
  22. package/script/context/ParseContext.js +23 -8
  23. package/script/context/generateTypes.d.ts +3 -2
  24. package/script/helpers/refFns.d.ts +8 -3
  25. package/script/helpers/refFns.js +27 -10
  26. package/script/oas/operation/Operation.d.ts +19 -4
  27. package/script/oas/operation/Operation.js +29 -9
  28. package/script/oas/operation/toEffectiveParameters.d.ts +9 -0
  29. package/script/oas/operation/toEffectiveParameters.js +29 -1
  30. package/script/oas/ref/DanglingRefError.d.ts +18 -3
  31. package/script/oas/ref/DanglingRefError.js +17 -4
  32. package/script/oas/ref/Ref.d.ts +29 -13
  33. package/script/oas/ref/Ref.js +63 -38
  34. package/script/oas/webhook/Webhook.d.ts +10 -0
  35. package/script/oas/webhook/Webhook.js +13 -1
  36. package/script/parse/anyOfSystem.js +6 -8
  37. package/script/parse/v3-0/schema/toSchemasV3.js +3 -3
  38. package/script/parse/v3-1/schema/toSchemasV3.js +3 -3
  39. package/script/parse/v3-2/schema/toSchemasV3.js +3 -3
@@ -30,6 +30,7 @@ import type { GqlParseOptions, LogAtArgs, LogIssueArgs, LogIssueAtArgs, LogIssue
30
30
  import type { ParseIssue } from './ParseIssue.js';
31
31
  import type { OasRef } from '../oas/ref/Ref.js';
32
32
  import type { OasRefData } from '../oas/ref/ref-types.js';
33
+ import { DanglingRefError } from '../oas/ref/DanglingRefError.js';
33
34
  export type { ParseIssue, GqlIssueType } from './ParseIssue.js';
34
35
  export type { GqlParseOptions, LogAtArgs, LogIssueArgs, LogIssueAtArgs, LogIssueNoKeyArgs, LogSkippedValuesArgs, ParseContextType, ParseErrorInput, ParseIssueInput, ParseWarningInput } from './parseTypes.js';
35
36
  type OasProtocolState = {
@@ -53,7 +54,8 @@ type GqlProtocolState = {
53
54
  };
54
55
  type ProtocolState = OasProtocolState | GqlProtocolState;
55
56
  type ToDanglingAllOfMemberArgs = {
56
- $ref: string;
57
+ /** Why the member names nothing: no schema has its name, or its `$ref`s loop. */
58
+ error: DanglingRefError;
57
59
  /** The schema that holds the `allOf`. */
58
60
  stackTrail: StackTrail;
59
61
  };
@@ -182,7 +184,7 @@ export declare class ParseContext {
182
184
  * removed. It is separate from `schema`: here core decides, at parse, and
183
185
  * the generator never sees the member.
184
186
  */
185
- toDanglingAllOfMember({ $ref, stackTrail }: ToDanglingAllOfMemberArgs): OpenAPIV3.SchemaObject;
187
+ toDanglingAllOfMember({ error, stackTrail }: ToDanglingAllOfMemberArgs): OpenAPIV3.SchemaObject;
186
188
  /**
187
189
  * Register an error against a ref key — typically called when the
188
190
  * target of a `$ref` (or type reference) fails to parse. `undefined`
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_oasRefs, _ParseContext_rootFrames, _ParseContext_removeComponent, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_toRawComponentParameter, _ParseContext_logDependencyIssue, _ParseContext_toDocumentTrail, _ParseContext_checkRefs, _ParseContext_removeHolder;
12
+ var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_oasRefs, _ParseContext_rootFrames, _ParseContext_removeComponent, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_toRawComponentParameter, _ParseContext_logDependencyIssue, _ParseContext_toDocumentTrail, _ParseContext_checkRefs, _ParseContext_toRefError, _ParseContext_toLoopOf, _ParseContext_removeHolder;
13
13
  /**
14
14
  * Unified parse context for both OAS and GraphQL inputs.
15
15
  *
@@ -401,15 +401,14 @@ export class ParseContext {
401
401
  * removed. It is separate from `schema`: here core decides, at parse, and
402
402
  * the generator never sees the member.
403
403
  */
404
- toDanglingAllOfMember({ $ref, stackTrail }) {
405
- const error = new DanglingRefError($ref);
404
+ toDanglingAllOfMember({ error, stackTrail }) {
406
405
  if (!this.system.danglingRefs?.includes('allOfMember')) {
407
406
  throw error;
408
407
  }
409
408
  this.logIssueNoKey({
410
409
  level: 'warning',
411
410
  message: `${error.message}. It is left out of the allOf merge (system.danglingRefs lists 'allOfMember').`,
412
- parent: { $ref },
411
+ parent: { $ref: error.ref },
413
412
  stackTrail,
414
413
  type: 'DANGLING_REF'
415
414
  });
@@ -684,10 +683,8 @@ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMa
684
683
  if (__classPrivateFieldGet(this, _ParseContext_refErrors, "f").has(toRefKey(ref.$ref))) {
685
684
  return;
686
685
  }
687
- try {
688
- ref.resolveOnce();
689
- }
690
- catch (error) {
686
+ const error = __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_toRefError).call(this, ref);
687
+ if (error !== undefined) {
691
688
  if (!DanglingRefError.is(error)) {
692
689
  const message = error instanceof Error ? error.message : String(error);
693
690
  __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_removeHolder).call(this, {
@@ -718,6 +715,24 @@ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMa
718
715
  }
719
716
  }
720
717
  });
718
+ }, _ParseContext_toRefError = function _ParseContext_toRefError(ref) {
719
+ try {
720
+ ref.resolveOnce();
721
+ }
722
+ catch (error) {
723
+ return error;
724
+ }
725
+ const loop = __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_toLoopOf).call(this, ref);
726
+ return loop === undefined ? undefined : new DanglingRefError(ref.$ref, { loop: loop.map(({ $ref }) => $ref) });
727
+ }, _ParseContext_toLoopOf = function _ParseContext_toLoopOf(ref) {
728
+ try {
729
+ const loop = ref.toLoop();
730
+ return loop?.includes(ref) ? loop : undefined;
731
+ }
732
+ catch {
733
+ // A ref further on names nothing or the wrong kind: it reports that itself.
734
+ return undefined;
735
+ }
721
736
  }, _ParseContext_removeHolder = function _ParseContext_removeHolder({ consumer, ref, error, type, message }) {
722
737
  this.logIssueNoKey({
723
738
  level: 'error',
@@ -185,8 +185,9 @@ export type AnyOasDocument = OpenAPIV2.Document | OpenAPIV3.Document | OpenAPIV3
185
185
  * of two or more members as any value. The schema keeps its other keywords.
186
186
  * One for each schema.
187
187
  *
188
- * `DANGLING_REF` — a `$ref` names nothing in the document. One for each
189
- * place the `$ref` is written. A `warning` when `system.danglingRefs` lists
188
+ * `DANGLING_REF` — a `$ref` names nothing in the document: no component has
189
+ * its name, or it is part of a loop of `$ref`s (`A` is a `$ref` to `B`, and
190
+ * `B` to `A`). One for each place the `$ref` is written. A `warning` when `system.danglingRefs` lists
190
191
  * the `$ref`'s kind: the ref stays, and resolving it throws
191
192
  * `DanglingRefError`; with `allOfMember` listed, a `$ref` that core merges
192
193
  * (in an `allOf`, or in a `oneOf` or `anyOf` member) is left out of the
@@ -2,6 +2,7 @@ import "../_dnt.polyfills.js";
2
2
  import type { RefName } from '../types/RefName.js';
3
3
  import type { OpenAPIV3 } from 'openapi-types';
4
4
  import type { ReferenceObject } from '../parse/v3-0/_merge-all-of/types.js';
5
+ import { DanglingRefError } from '../oas/ref/DanglingRefError.js';
5
6
  /**
6
7
  * Extracts a reference name from an OpenAPI $ref string.
7
8
  *
@@ -121,7 +122,8 @@ export declare const isRef: (value: unknown) => value is Ref;
121
122
  * @param oasDocument - The OpenAPI document containing the schemas to resolve against
122
123
  * @returns A function that resolves reference objects to schema objects
123
124
  *
124
- * @throws {DanglingRefError} When a reference names no schema in the document
125
+ * @throws {DanglingRefError} When a reference names no schema in the document,
126
+ * or its chain of references loops, unless `onDangling` reads something in its place
125
127
  *
126
128
  * @example Basic usage
127
129
  * ```typescript
@@ -172,6 +174,9 @@ export declare const isRef: (value: unknown) => value is Ref;
172
174
  * ```
173
175
  */
174
176
  export declare const toGetRef: (oasDocument: OpenAPIV3.Document, onDangling?: OnDangling) => ({ $ref }: ReferenceObject) => OpenAPIV3.SchemaObject;
175
- /** What a resolver reads in place of a `$ref` that names nothing; it may throw. */
176
- type OnDangling = ($ref: string) => OpenAPIV3.SchemaObject;
177
+ /**
178
+ * What a resolver reads in place of a `$ref` that names nothing: no schema
179
+ * has its name, or its chain of `$ref`s loops. It may throw `error`.
180
+ */
181
+ type OnDangling = (error: DanglingRefError) => OpenAPIV3.SchemaObject;
177
182
  export {};
@@ -138,7 +138,8 @@ export const isRef = (value) => {
138
138
  * @param oasDocument - The OpenAPI document containing the schemas to resolve against
139
139
  * @returns A function that resolves reference objects to schema objects
140
140
  *
141
- * @throws {DanglingRefError} When a reference names no schema in the document
141
+ * @throws {DanglingRefError} When a reference names no schema in the document,
142
+ * or its chain of references loops, unless `onDangling` reads something in its place
142
143
  *
143
144
  * @example Basic usage
144
145
  * ```typescript
@@ -188,17 +189,33 @@ export const isRef = (value) => {
188
189
  * }
189
190
  * ```
190
191
  */
191
- export const toGetRef = (oasDocument, onDangling) => ({ $ref }) => {
192
- const refName = toRefName($ref);
193
- const item = oasDocument.components?.schemas?.[refName];
194
- if (isRef(item)) {
195
- return toGetRef(oasDocument, onDangling)(item);
192
+ export const toGetRef = (oasDocument, onDangling) => ({ $ref }) => toTargetSchema({ oasDocument, onDangling, $ref, followed: [] });
193
+ /**
194
+ * The schema at the end of the chain of `$ref`s. A `$ref` that names nothing,
195
+ * or a chain that comes back to a component it has passed (`A` is a `$ref`
196
+ * to `B`, and `B` to `A`), gives `onDangling` a `DanglingRefError`, or throws it.
197
+ */
198
+ const toTargetSchema = ({ oasDocument, onDangling, $ref, followed }) => {
199
+ const item = oasDocument.components?.schemas?.[toRefName($ref)];
200
+ if (!item) {
201
+ return toDangling(onDangling, new DanglingRefError($ref));
196
202
  }
197
- if (item) {
203
+ if (!isRef(item)) {
198
204
  return item;
199
205
  }
206
+ const passed = [...followed, $ref];
207
+ const loopStart = passed.findIndex(each => toRefName(each) === toRefName(item.$ref));
208
+ if (loopStart === -1) {
209
+ return toTargetSchema({ oasDocument, onDangling, $ref: item.$ref, followed: passed });
210
+ }
211
+ // The `$ref`s written in the loop's components: each one after the `$ref`
212
+ // that first named one of them, as `OasRef.toLoop` gives them.
213
+ const loop = [...passed.slice(loopStart + 1), item.$ref];
214
+ return toDangling(onDangling, new DanglingRefError(passed[0] ?? $ref, { loop }));
215
+ };
216
+ const toDangling = (onDangling, error) => {
200
217
  if (onDangling) {
201
- return onDangling($ref);
218
+ return onDangling(error);
202
219
  }
203
- throw new DanglingRefError($ref);
220
+ throw error;
204
221
  };
@@ -147,14 +147,26 @@ export declare class OasOperation extends OasBase {
147
147
  */
148
148
  traverse(path: SchemaPath, mediaType?: string): OasSchema | OasRef<'schema'>;
149
149
  /**
150
- * Resolve all parameters and optionally filter by location.
150
+ * The parameters that apply to this operation, as OpenAPI defines them: the
151
+ * path item's parameters apply to each of its operations, and an operation's
152
+ * own parameter with the same name and location replaces the path item's.
153
+ * The path item's come first.
151
154
  *
152
- * As OpenAPI defines them: the path item's parameters apply to each of its
153
- * operations, and an operation's own parameter with the same name and
154
- * location replaces the path item's. The path item's come first.
155
+ * References stay references, so a caller can resolve each one on its own.
156
+ * A reference that names nothing (kept when `system.danglingRefs` lists
157
+ * `parameter`) keeps its place, and resolving it throws `DanglingRefError`.
158
+ * {@link OasOperation.toParams} resolves them all.
159
+ *
160
+ * @returns the parameters, path item's first
161
+ */
162
+ toEffectiveParameters(): (OasParameter | OasRef<'parameter'>)[];
163
+ /**
164
+ * Resolve all parameters and optionally filter by location: the resolved
165
+ * {@link OasOperation.toEffectiveParameters}.
155
166
  *
156
167
  * @param filter - only include parameters from specified locations
157
168
  * @returns
169
+ * @throws {DanglingRefError} when a parameter is a reference that names nothing
158
170
  */
159
171
  toParams(filter?: OasParameterLocation[]): OasParameter[];
160
172
  /**
@@ -169,6 +181,9 @@ export declare class OasOperation extends OasBase {
169
181
  *
170
182
  * @param options - Conversion options for nested components
171
183
  * @returns OpenAPI v3 operation object
184
+ * @throws {DanglingRefError} when a parameter is a reference that names
185
+ * nothing, as {@link OasOperation.toParams} does: there is no parameter to
186
+ * write, and the caller decides what to do
172
187
  */
173
188
  toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.OperationObject;
174
189
  /**
@@ -3,7 +3,8 @@ import { traverseSchema } from '../schemaPath/traverseSchema.js';
3
3
  import { OasObject } from '../object/Object.js';
4
4
  import { toPrimaryResponseCode } from '../response/toPrimaryResponseCode.js';
5
5
  import { OasBase } from '../../types/OasBase.js';
6
- import { toEffectiveParameters } from './toEffectiveParameters.js';
6
+ import { namesNothing, toEffectiveParameters } from './toEffectiveParameters.js';
7
+ import { DanglingRefError } from '../ref/DanglingRefError.js';
7
8
  export class OasOperation extends OasBase {
8
9
  /**
9
10
  * Creates a new OasOperation instance from operation field data.
@@ -219,17 +220,31 @@ export class OasOperation extends OasBase {
219
220
  return traverseSchema(rootSchema, rest);
220
221
  }
221
222
  /**
222
- * Resolve all parameters and optionally filter by location.
223
+ * The parameters that apply to this operation, as OpenAPI defines them: the
224
+ * path item's parameters apply to each of its operations, and an operation's
225
+ * own parameter with the same name and location replaces the path item's.
226
+ * The path item's come first.
223
227
  *
224
- * As OpenAPI defines them: the path item's parameters apply to each of its
225
- * operations, and an operation's own parameter with the same name and
226
- * location replaces the path item's. The path item's come first.
228
+ * References stay references, so a caller can resolve each one on its own.
229
+ * A reference that names nothing (kept when `system.danglingRefs` lists
230
+ * `parameter`) keeps its place, and resolving it throws `DanglingRefError`.
231
+ * {@link OasOperation.toParams} resolves them all.
232
+ *
233
+ * @returns the parameters, path item's first
234
+ */
235
+ toEffectiveParameters() {
236
+ return toEffectiveParameters({ own: this.parameters, shared: this.pathItem?.parameters });
237
+ }
238
+ /**
239
+ * Resolve all parameters and optionally filter by location: the resolved
240
+ * {@link OasOperation.toEffectiveParameters}.
227
241
  *
228
242
  * @param filter - only include parameters from specified locations
229
243
  * @returns
244
+ * @throws {DanglingRefError} when a parameter is a reference that names nothing
230
245
  */
231
246
  toParams(filter) {
232
- return toEffectiveParameters({ own: this.parameters, shared: this.pathItem?.parameters })
247
+ return this.toEffectiveParameters()
233
248
  .map(param => param.resolve())
234
249
  .filter(param => (filter?.length ? filter.includes(param.location) : true));
235
250
  }
@@ -254,12 +269,17 @@ export class OasOperation extends OasBase {
254
269
  *
255
270
  * @param options - Conversion options for nested components
256
271
  * @returns OpenAPI v3 operation object
272
+ * @throws {DanglingRefError} when a parameter is a reference that names
273
+ * nothing, as {@link OasOperation.toParams} does: there is no parameter to
274
+ * write, and the caller decides what to do
257
275
  */
258
276
  toJsonSchema(options) {
259
- const parameters = toEffectiveParameters({
260
- own: this.parameters,
261
- shared: this.pathItem?.parameters
262
- });
277
+ const parameters = this.toEffectiveParameters();
278
+ for (const parameter of parameters) {
279
+ if (parameter.isRef() && namesNothing(parameter)) {
280
+ throw new DanglingRefError(parameter.$ref);
281
+ }
282
+ }
263
283
  return {
264
284
  tags: this.tags,
265
285
  summary: this.summary,
@@ -11,6 +11,15 @@ type ToEffectiveParametersArgs = {
11
11
  * The parameters that apply to an operation or webhook, as OpenAPI defines
12
12
  * them: the path item's first, then its own. An own parameter with the same
13
13
  * name and location replaces the path item's. References stay references.
14
+ *
15
+ * A reference that names nothing (kept when `system.danglingRefs` lists
16
+ * `parameter`) has no name or location. It keeps its place, and only the
17
+ * same `$ref` replaces it.
14
18
  */
15
19
  export declare const toEffectiveParameters: ({ own, shared }: ToEffectiveParametersArgs) => Parameter[];
20
+ /**
21
+ * True for a parameter `$ref` that names nothing in the document, which the
22
+ * parser keeps when `system.danglingRefs` lists `parameter`.
23
+ */
24
+ export declare const namesNothing: (parameter: Parameter) => boolean;
16
25
  export {};
@@ -1,13 +1,40 @@
1
+ import { DanglingRefError } from '../ref/DanglingRefError.js';
1
2
  /**
2
3
  * The parameters that apply to an operation or webhook, as OpenAPI defines
3
4
  * them: the path item's first, then its own. An own parameter with the same
4
5
  * name and location replaces the path item's. References stay references.
6
+ *
7
+ * A reference that names nothing (kept when `system.danglingRefs` lists
8
+ * `parameter`) has no name or location. It keeps its place, and only the
9
+ * same `$ref` replaces it.
5
10
  */
6
11
  export const toEffectiveParameters = ({ own = [], shared = [] }) => {
7
12
  const ownKeys = new Set(own.map(toParameterKey));
8
13
  return [...shared.filter(parameter => !ownKeys.has(toParameterKey(parameter))), ...own];
9
14
  };
15
+ /**
16
+ * True for a parameter `$ref` that names nothing in the document, which the
17
+ * parser keeps when `system.danglingRefs` lists `parameter`.
18
+ */
19
+ export const namesNothing = (parameter) => {
20
+ if (!parameter.isRef()) {
21
+ return false;
22
+ }
23
+ try {
24
+ parameter.resolve();
25
+ return false;
26
+ }
27
+ catch (error) {
28
+ if (DanglingRefError.is(error)) {
29
+ return true;
30
+ }
31
+ throw error;
32
+ }
33
+ };
10
34
  const toParameterKey = (parameter) => {
35
+ if (parameter.isRef() && namesNothing(parameter)) {
36
+ return `$ref:${parameter.$ref}`;
37
+ }
11
38
  const { name, location } = parameter.resolve();
12
39
  return `${location}:${name}`;
13
40
  };
@@ -1,5 +1,18 @@
1
+ import "../../_dnt.polyfills.js";
2
+ type DanglingRefErrorOptions = {
3
+ /**
4
+ * When the chain of `$ref`s from `ref` never reaches a component: the
5
+ * `$ref`s written in the loop it comes back to, in the order it follows
6
+ * them. `A` a `$ref` to `B`, and `B` to `A`, give
7
+ * `['#/components/schemas/B', '#/components/schemas/A']`.
8
+ */
9
+ loop?: string[];
10
+ };
1
11
  /**
2
- * Thrown when a reference that names nothing in the document is resolved.
12
+ * Thrown when a reference that names nothing in the document is resolved:
13
+ * no component has its name, or its chain of `$ref`s comes back to one it
14
+ * has passed (`A` is a `$ref` to `B`, and `B` to `A`), so it never reaches a
15
+ * component.
3
16
  *
4
17
  * By default the parser removes what holds such a reference, so a generator
5
18
  * never meets one. When `client.json` `system.danglingRefs` lists the
@@ -7,11 +20,12 @@
7
20
  * this error and render something else in its place. Test for it with
8
21
  * {@link DanglingRefError.is}, not `instanceof`.
9
22
  */
10
- import "../../_dnt.polyfills.js";
11
23
  export declare class DanglingRefError extends Error {
12
24
  /** The `$ref`, or the component name, that names nothing. */
13
25
  ref: string;
14
- constructor(ref: string);
26
+ /** When the chain of `$ref`s loops: see {@link DanglingRefErrorOptions.loop}. */
27
+ loop: string[] | undefined;
28
+ constructor(ref: string, { loop }?: DanglingRefErrorOptions);
15
29
  /**
16
30
  * True when `error` is a `DanglingRefError`. It reads the error's name,
17
31
  * not its class, so it is also true for one thrown by another copy of
@@ -20,3 +34,4 @@ export declare class DanglingRefError extends Error {
20
34
  */
21
35
  static is(error: unknown): error is DanglingRefError;
22
36
  }
37
+ export {};
@@ -1,5 +1,9 @@
1
+ import "../../_dnt.polyfills.js";
1
2
  /**
2
- * Thrown when a reference that names nothing in the document is resolved.
3
+ * Thrown when a reference that names nothing in the document is resolved:
4
+ * no component has its name, or its chain of `$ref`s comes back to one it
5
+ * has passed (`A` is a `$ref` to `B`, and `B` to `A`), so it never reaches a
6
+ * component.
3
7
  *
4
8
  * By default the parser removes what holds such a reference, so a generator
5
9
  * never meets one. When `client.json` `system.danglingRefs` lists the
@@ -7,10 +11,11 @@
7
11
  * this error and render something else in its place. Test for it with
8
12
  * {@link DanglingRefError.is}, not `instanceof`.
9
13
  */
10
- import "../../_dnt.polyfills.js";
11
14
  export class DanglingRefError extends Error {
12
- constructor(ref) {
13
- super(`Ref "${ref}" names nothing in the document`);
15
+ constructor(ref, { loop } = {}) {
16
+ super(loop === undefined
17
+ ? `Ref "${ref}" names nothing in the document`
18
+ : `Ref "${ref}" leads into a loop of $refs (${[...loop, ...loop.slice(0, 1)].join(' → ')}) and never reaches a component`);
14
19
  /** The `$ref`, or the component name, that names nothing. */
15
20
  Object.defineProperty(this, "ref", {
16
21
  enumerable: true,
@@ -18,8 +23,16 @@ export class DanglingRefError extends Error {
18
23
  writable: true,
19
24
  value: void 0
20
25
  });
26
+ /** When the chain of `$ref`s loops: see {@link DanglingRefErrorOptions.loop}. */
27
+ Object.defineProperty(this, "loop", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
21
33
  this.name = 'DanglingRefError';
22
34
  this.ref = ref;
35
+ this.loop = loop;
23
36
  }
24
37
  /**
25
38
  * True when `error` is a `DanglingRefError`. It reads the error's name,
@@ -46,7 +46,7 @@ export type RefFields<T extends OasRefData['refType']> = SiblingKeywords & {
46
46
  * - **Type Safety**: Generic parameter ensures resolved types match the reference type
47
47
  * - **Lazy Resolution**: References are resolved on-demand, not during construction
48
48
  * - **Chain Resolution**: Handles references that point to other references
49
- * - **Circular Detection**: Prevents infinite loops with maximum lookup limits
49
+ * - **Loop Detection**: A chain of references that loops names nothing ({@link OasRef.toLoop})
50
50
  * - **Type Validation**: Ensures resolved objects match expected reference types
51
51
  *
52
52
  * @template T - The type of component this reference points to
@@ -210,8 +210,8 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
210
210
  /**
211
211
  * Recursively resolves this reference to its final target component.
212
212
  *
213
- * Follows reference chains until reaching a non-reference component,
214
- * with protection against infinite loops. A schema reference returns the
213
+ * Follows the chain of references until it reaches a component that is
214
+ * not a reference. A schema reference returns the
215
215
  * schema at this site: when the keywords next to it, or next to a
216
216
  * reference on the chain ({@link SiblingKeywords}), change the target, a
217
217
  * copy of the target with them applied. When they change nothing, the
@@ -221,11 +221,24 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
221
221
  * model every reference to it sees, and a copy is built once, kept on
222
222
  * this reference, and returned on every call.
223
223
  *
224
- * @param lookupsPerformed - Internal counter to prevent infinite recursion
225
224
  * @returns The resolved component
226
- * @throws Error if maximum lookup depth is exceeded
225
+ * @throws {DanglingRefError} when a reference on the chain names nothing,
226
+ * or the chain loops ({@link toLoop}), so it never reaches a component.
227
+ * The parser keeps either one only when `system.danglingRefs` lists its kind.
228
+ * @throws Error when a reference on the chain names a component of another
229
+ * kind (see {@link resolveOnce})
227
230
  */
228
- resolve(lookupsPerformed?: number): ResolvedRef<T>;
231
+ resolve(): ResolvedRef<T>;
232
+ /**
233
+ * The references that form the loop this reference's chain comes back to,
234
+ * starting with the first one it comes back to, or `undefined` when the
235
+ * chain reaches a component. `A` a `$ref` to `B`, and `B` to `A`, form a
236
+ * loop: from `A` the chain goes to `B`, then to `A` again, and never
237
+ * reaches a component. A reference that leads into a loop is not part of it.
238
+ *
239
+ * @throws what {@link resolveOnce} throws, at any step of the chain
240
+ */
241
+ toLoop(): OasRef<T>[] | undefined;
229
242
  /**
230
243
  * Resolves this reference one level. Dispatches on the document's
231
244
  * protocol — OAS reads from `document.components.<bucket>`; GQL
@@ -239,6 +252,12 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
239
252
  * `requestBody` ref to `#/components/schemas/…`
240
253
  */
241
254
  resolveOnce(): OasRef<T> | ResolvedRef<T>;
255
+ /**
256
+ * The component name the reference looks up.
257
+ *
258
+ * @throws {DanglingRefError} when the `$ref` has no name, such as
259
+ * `#/components/schemas/`: it names nothing
260
+ */
242
261
  toRefName(): RefName;
243
262
  /**
244
263
  * Narrows this reference to a schema reference.
@@ -297,18 +316,15 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
297
316
  * Whether the value is `readOnly` at this site, as Forge reads it: on this
298
317
  * reference (`{ allOf: [{ $ref }], readOnly: true }`), or on what it points
299
318
  * to, through any chain of references. A `readOnly: false` does not undo
300
- * another's `true`. `false` for a reference to anything but a schema.
301
- *
302
- * @param lookupsPerformed - Internal counter that stops a chain of references with no schema
319
+ * another's `true`. `false` for a reference to anything but a schema. In a
320
+ * loop ({@link toLoop}), which reaches no schema, only the references count.
303
321
  */
304
- isReadOnly(lookupsPerformed?: number): boolean;
322
+ isReadOnly(): boolean;
305
323
  /**
306
324
  * Whether the value is `writeOnly` at this site: on this reference, or on
307
325
  * what it points to, as {@link isReadOnly} reads `readOnly`.
308
- *
309
- * @param lookupsPerformed - Internal counter that stops a chain of references with no schema
310
326
  */
311
- isWriteOnly(lookupsPerformed?: number): boolean;
327
+ isWriteOnly(): boolean;
312
328
  /**
313
329
  * Returns the discriminated parsed document this ref resolves
314
330
  * through. OAS variant carries the parent `OasDocument`; GQL variant