@skmtc/core 0.40.0 → 0.42.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 (79) hide show
  1. package/esm/context/GenerateContext.d.ts +1 -1
  2. package/esm/context/GenerateContext.js +4 -3
  3. package/esm/context/ParseContext.d.ts +26 -0
  4. package/esm/context/ParseContext.js +112 -2
  5. package/esm/context/generateTypes.d.ts +16 -2
  6. package/esm/context/generateTypes.js +2 -0
  7. package/esm/context/tryParseAt.d.ts +4 -1
  8. package/esm/context/tryParseAt.js +10 -3
  9. package/esm/helpers/collateExamples.js +15 -5
  10. package/esm/helpers/refFns.d.ts +9 -3
  11. package/esm/helpers/refFns.js +29 -9
  12. package/esm/mod.d.ts +1 -0
  13. package/esm/mod.js +1 -0
  14. package/esm/oas/ref/DanglingRefError.d.ts +22 -0
  15. package/esm/oas/ref/DanglingRefError.js +33 -0
  16. package/esm/oas/ref/Ref.d.ts +5 -0
  17. package/esm/oas/ref/Ref.js +28 -2
  18. package/esm/oas/schema/siblings.js +12 -2
  19. package/esm/oas/union/Union.d.ts +2 -1
  20. package/esm/oas/union/Union.js +23 -12
  21. package/esm/parse/securityRequirements.d.ts +6 -0
  22. package/esm/parse/securityRequirements.js +8 -0
  23. package/esm/parse/v3-0/_merge-all-of/merge-union.js +9 -1
  24. package/esm/parse/v3-0/mediaType/toMediaTypeItemV3.js +4 -3
  25. package/esm/parse/v3-0/parameter/toParameterV3.js +4 -3
  26. package/esm/parse/v3-0/ref/toRefV31.js +5 -2
  27. package/esm/parse/v3-0/schema/toSchemasV3.js +6 -3
  28. package/esm/parse/v3-1/_merge-all-of/merge-union.js +16 -4
  29. package/esm/parse/v3-1/mediaType/toMediaTypeItemV3.js +4 -3
  30. package/esm/parse/v3-1/parameter/toParameterV3.js +4 -3
  31. package/esm/parse/v3-1/ref/toRefV31.js +5 -2
  32. package/esm/parse/v3-1/schema/toSchemasV3.js +6 -3
  33. package/esm/parse/v3-2/_merge-all-of/merge-union.js +16 -4
  34. package/esm/parse/v3-2/mediaType/toMediaTypeItemV3.js +4 -3
  35. package/esm/parse/v3-2/parameter/toParameterV3.js +4 -3
  36. package/esm/parse/v3-2/ref/toRefV31.js +5 -2
  37. package/esm/parse/v3-2/schema/toSchemasV3.js +6 -3
  38. package/esm/types/Settings.d.ts +21 -0
  39. package/esm/types/Settings.js +19 -1
  40. package/package.json +5 -1
  41. package/script/context/GenerateContext.d.ts +1 -1
  42. package/script/context/GenerateContext.js +4 -3
  43. package/script/context/ParseContext.d.ts +26 -0
  44. package/script/context/ParseContext.js +112 -2
  45. package/script/context/generateTypes.d.ts +16 -2
  46. package/script/context/generateTypes.js +2 -0
  47. package/script/context/tryParseAt.d.ts +4 -1
  48. package/script/context/tryParseAt.js +10 -3
  49. package/script/helpers/collateExamples.js +15 -5
  50. package/script/helpers/refFns.d.ts +9 -3
  51. package/script/helpers/refFns.js +29 -9
  52. package/script/mod.d.ts +1 -0
  53. package/script/mod.js +1 -0
  54. package/script/oas/ref/DanglingRefError.d.ts +22 -0
  55. package/script/oas/ref/DanglingRefError.js +37 -0
  56. package/script/oas/ref/Ref.d.ts +5 -0
  57. package/script/oas/ref/Ref.js +28 -2
  58. package/script/oas/schema/siblings.js +12 -2
  59. package/script/oas/union/Union.d.ts +2 -1
  60. package/script/oas/union/Union.js +23 -12
  61. package/script/parse/securityRequirements.d.ts +6 -0
  62. package/script/parse/securityRequirements.js +8 -0
  63. package/script/parse/v3-0/_merge-all-of/merge-union.js +9 -1
  64. package/script/parse/v3-0/mediaType/toMediaTypeItemV3.js +4 -3
  65. package/script/parse/v3-0/parameter/toParameterV3.js +4 -3
  66. package/script/parse/v3-0/ref/toRefV31.js +5 -2
  67. package/script/parse/v3-0/schema/toSchemasV3.js +6 -3
  68. package/script/parse/v3-1/_merge-all-of/merge-union.js +16 -4
  69. package/script/parse/v3-1/mediaType/toMediaTypeItemV3.js +4 -3
  70. package/script/parse/v3-1/parameter/toParameterV3.js +4 -3
  71. package/script/parse/v3-1/ref/toRefV31.js +5 -2
  72. package/script/parse/v3-1/schema/toSchemasV3.js +6 -3
  73. package/script/parse/v3-2/_merge-all-of/merge-union.js +16 -4
  74. package/script/parse/v3-2/mediaType/toMediaTypeItemV3.js +4 -3
  75. package/script/parse/v3-2/parameter/toParameterV3.js +4 -3
  76. package/script/parse/v3-2/ref/toRefV31.js +5 -2
  77. package/script/parse/v3-2/schema/toSchemasV3.js +6 -3
  78. package/script/types/Settings.d.ts +21 -0
  79. package/script/types/Settings.js +20 -2
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DanglingRefError = void 0;
4
+ /**
5
+ * Thrown when a reference that names nothing in the document is resolved.
6
+ *
7
+ * By default the parser removes what holds such a reference, so a generator
8
+ * never meets one. When `client.json` `system.danglingRefs` lists the
9
+ * reference's kind, it stays, and a generator that resolves it can catch
10
+ * this error and render something else in its place. Test for it with
11
+ * {@link DanglingRefError.is}, not `instanceof`.
12
+ */
13
+ require("../../_dnt.polyfills.js");
14
+ class DanglingRefError extends Error {
15
+ constructor(ref) {
16
+ super(`Ref "${ref}" names nothing in the document`);
17
+ /** The `$ref`, or the component name, that names nothing. */
18
+ Object.defineProperty(this, "ref", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ this.name = 'DanglingRefError';
25
+ this.ref = ref;
26
+ }
27
+ /**
28
+ * True when `error` is a `DanglingRefError`. It reads the error's name,
29
+ * not its class, so it is also true for one thrown by another copy of
30
+ * `@skmtc/core`, such as the copy a generator bundle resolves, where
31
+ * `instanceof` is false.
32
+ */
33
+ static is(error) {
34
+ return error instanceof Error && error.name === 'DanglingRefError';
35
+ }
36
+ }
37
+ exports.DanglingRefError = DanglingRefError;
@@ -232,6 +232,11 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
232
232
  * reads from `document.registry.schemas`.
233
233
  *
234
234
  * @returns Either the resolved component or another reference in the chain
235
+ * @throws {DanglingRefError} when the reference names nothing in the
236
+ * document (kept when `client.json` `system.danglingRefs` lists its kind),
237
+ * including a `$ref` with no name, such as `#/components/schemas/`
238
+ * @throws Error when it names a component of another kind, such as a
239
+ * `requestBody` ref to `#/components/schemas/…`
235
240
  */
236
241
  resolveOnce(): OasRef<T> | ResolvedRef<T>;
237
242
  toRefName(): RefName;
@@ -17,6 +17,7 @@ require("../../_dnt.polyfills.js");
17
17
  const traverseSchema_js_1 = require("../schemaPath/traverseSchema.js");
18
18
  const refFns_js_1 = require("../../helpers/refFns.js");
19
19
  const OasBase_js_1 = require("../../types/OasBase.js");
20
+ const DanglingRefError_js_1 = require("./DanglingRefError.js");
20
21
  const siblings_js_1 = require("../schema/siblings.js");
21
22
  const MAX_LOOKUPS = 10;
22
23
  /**
@@ -46,6 +47,18 @@ function refTypeToPluralPath(refType) {
46
47
  }
47
48
  }
48
49
  }
50
+ /**
51
+ * The name `resolveOnce` looks up. `toRefName` throws for a `$ref` whose last
52
+ * segment is empty (`#/components/schemas/`): that `$ref` names nothing.
53
+ */
54
+ const toLookupName = ($ref) => {
55
+ try {
56
+ return (0, refFns_js_1.toRefName)($ref);
57
+ }
58
+ catch {
59
+ throw new DanglingRefError_js_1.DanglingRefError($ref);
60
+ }
61
+ };
49
62
  /**
50
63
  * Represents an OpenAPI reference ($ref) in the SKMTC OAS processing system.
51
64
  *
@@ -448,14 +461,19 @@ class OasRef extends OasBase_js_1.OasBase {
448
461
  * reads from `document.registry.schemas`.
449
462
  *
450
463
  * @returns Either the resolved component or another reference in the chain
464
+ * @throws {DanglingRefError} when the reference names nothing in the
465
+ * document (kept when `client.json` `system.danglingRefs` lists its kind),
466
+ * including a `$ref` with no name, such as `#/components/schemas/`
467
+ * @throws Error when it names a component of another kind, such as a
468
+ * `requestBody` ref to `#/components/schemas/…`
451
469
  */
452
470
  resolveOnce() {
453
- const refName = (0, refFns_js_1.toRefName)(this.$ref);
471
+ const refName = toLookupName(this.$ref);
454
472
  const resolved = __classPrivateFieldGet(this, _OasRef_document, "f").type === 'oas'
455
473
  ? __classPrivateFieldGet(this, _OasRef_instances, "m", _OasRef_resolveOasOnce).call(this, __classPrivateFieldGet(this, _OasRef_document, "f").value, refName)
456
474
  : __classPrivateFieldGet(this, _OasRef_instances, "m", _OasRef_resolveGqlOnce).call(this, __classPrivateFieldGet(this, _OasRef_document, "f").value, refName);
457
475
  if (!resolved) {
458
- throw new Error(`Ref "${this.$ref}" not found`);
476
+ throw new DanglingRefError_js_1.DanglingRefError(this.$ref);
459
477
  }
460
478
  if (resolved.isRef()) {
461
479
  if (resolved.refType !== this.refType) {
@@ -648,6 +666,14 @@ _OasRef_document = new WeakMap(), _OasRef_resolution = new WeakMap(), _OasRef_in
648
666
  // OasSchema to ResolvedRef<T> for a generic T, as in resolveOnce().
649
667
  return (0, siblings_js_1.changesTarget)(copy, component, this) ? copy : target;
650
668
  }, _OasRef_resolveOasOnce = function _OasRef_resolveOasOnce(document, refName) {
669
+ // A `#/components/<bucket>/<name>` ref names its bucket. A request body
670
+ // that is a `$ref` to `#/components/schemas/Pet` names a schema, so it is
671
+ // never looked up in the request bodies.
672
+ const bucket = /^#\/components\/([^/]+)\//.exec(this.$ref)?.[1];
673
+ const expected = refTypeToPluralPath(this.refType);
674
+ if (bucket !== undefined && bucket !== expected) {
675
+ throw new Error(`Ref type mismatch for "${this.$ref}". Expected a ref into "components/${expected}" but it names one in "components/${bucket}"`);
676
+ }
651
677
  const c = document.components;
652
678
  const refType = this.refType;
653
679
  switch (refType) {
@@ -39,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.toSiblingEnums = exports.toSiblingValue = exports.isIntegerFormat = exports.isNumberFormat = exports.VALUE_CHECKS = exports.isObjectValue = exports.isArrayValue = exports.isBooleanValue = exports.isIntegerValue = exports.isNumberValue = exports.isStringValue = exports.toSiblingAnnotations = exports.toSkippedSiblings = exports.changesValues = exports.changesTarget = exports.hasSiblingKeywords = exports.mergeSiblingKeywords = exports.SIBLINGS_BY_TYPE = void 0;
40
40
  const v = __importStar(require("valibot"));
41
41
  const isEqual_js_1 = __importDefault(require("lodash-es/isEqual.js"));
42
+ const DanglingRefError_js_1 = require("../ref/DanglingRefError.js");
42
43
  const number_types_js_1 = require("../number/number-types.js");
43
44
  const integer_types_js_1 = require("../integer/integer-types.js");
44
45
  /**
@@ -227,8 +228,17 @@ const MAX_DEPTH = 10;
227
228
  */
228
229
  const agrees = (type, target, depth) => {
229
230
  if (target.type === 'union') {
230
- return depth < MAX_DEPTH &&
231
- target.members.every(member => agrees(type, member.isRef() ? member.resolve() : member, depth + 1));
231
+ try {
232
+ return depth < MAX_DEPTH &&
233
+ target.members.every(member => agrees(type, member.isRef() ? member.resolve() : member, depth + 1));
234
+ }
235
+ catch (error) {
236
+ // A member that is a dangling reference has no known type, so it does not agree.
237
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
238
+ return false;
239
+ }
240
+ throw error;
241
+ }
232
242
  }
233
243
  return type === target.type || (type === 'number' && target.type === 'integer');
234
244
  };
@@ -216,7 +216,8 @@ export declare class OasUnion extends OasBase {
216
216
  * an object that is not nullable, that requires the discriminator's
217
217
  * property, and that allows only string literals for it. No value may
218
218
  * belong to two members. A member that is a union, or anything other than
219
- * an object, makes it `false`.
219
+ * an object, makes it `false`, as does a reference on the way that names
220
+ * nothing in the document (kept when `system.danglingRefs` lists `schema`).
220
221
  *
221
222
  * It reads the schemas only. A generator may refuse more: one that leaves
222
223
  * out `readOnly` properties cannot discriminate on one.
@@ -4,6 +4,7 @@ exports.OasUnion = void 0;
4
4
  const traverseSchema_js_1 = require("../schemaPath/traverseSchema.js");
5
5
  const siblings_js_1 = require("../schema/siblings.js");
6
6
  const OasBase_js_1 = require("../../types/OasBase.js");
7
+ const DanglingRefError_js_1 = require("../ref/DanglingRefError.js");
7
8
  /**
8
9
  * Represents a union type schema in the OpenAPI Specification.
9
10
  *
@@ -286,7 +287,8 @@ class OasUnion extends OasBase_js_1.OasBase {
286
287
  * an object that is not nullable, that requires the discriminator's
287
288
  * property, and that allows only string literals for it. No value may
288
289
  * belong to two members. A member that is a union, or anything other than
289
- * an object, makes it `false`.
290
+ * an object, makes it `false`, as does a reference on the way that names
291
+ * nothing in the document (kept when `system.danglingRefs` lists `schema`).
290
292
  *
291
293
  * It reads the schemas only. A generator may refuse more: one that leaves
292
294
  * out `readOnly` properties cannot discriminate on one.
@@ -297,17 +299,26 @@ class OasUnion extends OasBase_js_1.OasBase {
297
299
  return false;
298
300
  }
299
301
  const seen = new Set();
300
- return this.members.every(member => {
301
- const values = toDiscriminatorValues(member, key);
302
- return (values !== undefined &&
303
- values.every(value => {
304
- if (seen.has(value)) {
305
- return false;
306
- }
307
- seen.add(value);
308
- return true;
309
- }));
310
- });
302
+ try {
303
+ return this.members.every(member => {
304
+ const values = toDiscriminatorValues(member, key);
305
+ return (values !== undefined &&
306
+ values.every(value => {
307
+ if (seen.has(value)) {
308
+ return false;
309
+ }
310
+ seen.add(value);
311
+ return true;
312
+ }));
313
+ });
314
+ }
315
+ catch (error) {
316
+ // A dangling reference: the schema it stands for is not known.
317
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
318
+ return false;
319
+ }
320
+ throw error;
321
+ }
311
322
  }
312
323
  toJsonSchema(options) {
313
324
  return {
@@ -58,6 +58,12 @@ export type ToOperationsWithValidSecurityArgs<Item extends SecuredItem> = {
58
58
  * failed to parse or is not defined, or that has no `security` and
59
59
  * inherits an unusable root. Each dropped item logs one `INVALID_OPERATION`
60
60
  * error on its own trail. `security: []` inherits nothing and is kept.
61
+ *
62
+ * A kept item depends on each scheme its security names (its own, else the
63
+ * root's), as it would on a `$ref` to it, so it is registered as a consumer
64
+ * of the scheme. When the parser removes the scheme later (one that is a
65
+ * `$ref` that names nothing), the item goes with it, and so is never kept
66
+ * without its auth.
61
67
  */
62
68
  export declare const toOperationsWithValidSecurity: <Item extends SecuredItem>({ items, check }: ToOperationsWithValidSecurityArgs<Item>) => Item[];
63
69
  export {};
@@ -112,6 +112,12 @@ exports.toSecurityCheck = toSecurityCheck;
112
112
  * failed to parse or is not defined, or that has no `security` and
113
113
  * inherits an unusable root. Each dropped item logs one `INVALID_OPERATION`
114
114
  * error on its own trail. `security: []` inherits nothing and is kept.
115
+ *
116
+ * A kept item depends on each scheme its security names (its own, else the
117
+ * root's), as it would on a `$ref` to it, so it is registered as a consumer
118
+ * of the scheme. When the parser removes the scheme later (one that is a
119
+ * `$ref` that names nothing), the item goes with it, and so is never kept
120
+ * without its auth.
115
121
  */
116
122
  const toOperationsWithValidSecurity = ({ items, check }) => {
117
123
  return items.filter(item => {
@@ -119,6 +125,8 @@ const toOperationsWithValidSecurity = ({ items, check }) => {
119
125
  ? toSchemeProblems(item.security, check).map(capitalize).join('. ') || undefined
120
126
  : check.rootProblem;
121
127
  if (message === undefined) {
128
+ const names = (item.security ?? check.rootSecurity ?? []).flatMap(({ requirement }) => Object.keys(requirement));
129
+ new Set(names).forEach(name => check.context.registerRef(item.stackTrail.clone(), `#/components/securitySchemes/${name}`));
122
130
  return true;
123
131
  }
124
132
  check.context.logIssueNoKey({
@@ -5,6 +5,7 @@ const decompose_union_js_1 = require("./decompose-union.js");
5
5
  const merge_js_1 = require("./merge.js");
6
6
  const cross_product_js_1 = require("./cross-product.js");
7
7
  const refFns_js_1 = require("../../../helpers/refFns.js");
8
+ const DanglingRefError_js_1 = require("../../../oas/ref/DanglingRefError.js");
8
9
  const mergeUnion = ({ schema, getRef, system, groupType }) => {
9
10
  const { beforeExcluded, decomposed, afterExcluded } = (0, decompose_union_js_1.decomposeUnion)({ schema, groupType });
10
11
  // if (decomposed.length === 1) {
@@ -54,13 +55,20 @@ exports.toOwnSystem = toOwnSystem;
54
55
  * ({@link toOwnSystem}). Inside an outer first pass under `latestWins`, a
55
56
  * cross product with no pair that merges fails its pair: the outer pass then
56
57
  * leaves the pair out, or lets the later member win.
58
+ *
59
+ * A `$ref` that names nothing is not a contradiction, so its pair is not left
60
+ * out for it: the `DanglingRefError` is thrown on, and `system.danglingRefs`
61
+ * decides (`allOfMember`).
57
62
  */
58
63
  const mergePairs = ({ pairs, merge, system }) => {
59
64
  const mergeEach = (pairSystem) => pairs.flatMap(pair => {
60
65
  try {
61
66
  return [merge(pair, pairSystem)];
62
67
  }
63
- catch (_error) {
68
+ catch (error) {
69
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
70
+ throw error;
71
+ }
64
72
  return [];
65
73
  }
66
74
  });
@@ -120,13 +120,14 @@ const toMediaTypeItemV3 = ({ mediaTypeItem, mediaType, stackTrail, context }) =>
120
120
  mediaType,
121
121
  encoding,
122
122
  schema: stackTrail.trace('schema', st => (0, toSchemasV3_js_1.toOptionalSchemaV3)({ schema, stackTrail: st, context })),
123
- examples: stackTrail.trace('examples', st => (0, toExamplesV3_js_1.toExamplesV3)({
123
+ // toExamplesV3 traces `examples` (or `example`) itself.
124
+ examples: (0, toExamplesV3_js_1.toExamplesV3)({
124
125
  example,
125
126
  examples,
126
127
  exampleKey: mediaType,
127
- stackTrail: st,
128
+ stackTrail,
128
129
  context
129
- })),
130
+ }),
130
131
  extensionFields
131
132
  };
132
133
  return context.withStackTrail(stackTrail, () => new MediaType_js_1.OasMediaType(fields, context));
@@ -141,13 +141,14 @@ const toParameterV3 = ({ parameter, stackTrail, context }) => {
141
141
  allowEmptyValue,
142
142
  allowReserved,
143
143
  schema: stackTrail.trace('schema', st => (0, toSchemasV3_js_1.toOptionalSchemaV3)({ schema, stackTrail: st, context })),
144
- examples: stackTrail.trace('examples', st => (0, toExamplesV3_js_1.toExamplesV3)({
144
+ // toExamplesV3 traces `examples` (or `example`) itself.
145
+ examples: (0, toExamplesV3_js_1.toExamplesV3)({
145
146
  examples,
146
147
  example,
147
148
  exampleKey: `${name}-${parsedLocation}`,
148
- stackTrail: st,
149
+ stackTrail,
149
150
  context
150
- })),
151
+ }),
151
152
  content: stackTrail.trace('content', st => (0, toMediaTypeItemV3_js_1.toOptionalMediaTypeItemsV3)({ content, stackTrail: st, context })),
152
153
  extensionFields
153
154
  };
@@ -8,7 +8,10 @@ const toRefV31 = ({ ref, refType, stackTrail, context, nullable, readOnly, write
8
8
  if (!(0, isEmpty_js_1.isEmpty)(skipped)) {
9
9
  context.logSkippedFields({ skipped, parent: ref, parentType: 'ref', stackTrail });
10
10
  }
11
- context.registerRef(stackTrail.clone(), $ref);
12
- return context.withStackTrail(stackTrail, () => new Ref_js_1.OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
11
+ const oasRef = context.withStackTrail(stackTrail, () => new Ref_js_1.OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
12
+ // The parse context checks each ref once the document is parsed, and
13
+ // removes what holds a ref that names nothing (`system.danglingRefs`).
14
+ context.registerOasRef(stackTrail.clone(), oasRef);
15
+ return oasRef;
13
16
  };
14
17
  exports.toRefV31 = toRefV31;
@@ -182,7 +182,7 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
182
182
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
183
183
  const merged = (0, merge_intersection_js_1.mergeIntersection)({
184
184
  schema,
185
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
185
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
186
186
  system
187
187
  });
188
188
  (0, allOfSystem_js_1.logAllOfContradictions)({ system, stackTrail, parent: schema, context });
@@ -196,7 +196,9 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
196
196
  const merged = (0, merge_union_js_1.mergeUnion)({
197
197
  // A null member makes the union nullable: `oneOf: [$ref, null]` is a nullable reference.
198
198
  schema: withoutNullMembers(schema, 'oneOf'),
199
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
199
+ // A member's own `allOf`, or a member merged with the keywords beside
200
+ // the `oneOf`, is merged here, so it follows `allOfMember` too.
201
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
200
202
  system,
201
203
  groupType: 'oneOf'
202
204
  });
@@ -264,7 +266,8 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
264
266
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
265
267
  const merged = (0, merge_union_js_1.mergeUnion)({
266
268
  schema: withoutNullMembers(schema, 'anyOf'),
267
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
269
+ // As for `oneOf`: what is merged here follows `allOfMember`.
270
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
268
271
  system,
269
272
  groupType: 'anyOf'
270
273
  });
@@ -5,6 +5,7 @@ const decompose_union_js_1 = require("./decompose-union.js");
5
5
  const merge_js_1 = require("./merge.js");
6
6
  const cross_product_js_1 = require("./cross-product.js");
7
7
  const refFns_js_1 = require("../../../helpers/refFns.js");
8
+ const DanglingRefError_js_1 = require("../../../oas/ref/DanglingRefError.js");
8
9
  const mergeUnion = ({ schema, getRef, normalize, system, groupType }) => {
9
10
  const { beforeExcluded, decomposed, afterExcluded } = (0, decompose_union_js_1.decomposeUnion)({ schema, groupType });
10
11
  // if (decomposed.length === 1) {
@@ -61,13 +62,20 @@ exports.toOwnSystem = toOwnSystem;
61
62
  * ({@link toOwnSystem}). Inside an outer first pass under `latestWins`, a
62
63
  * cross product with no pair that merges fails its pair: the outer pass then
63
64
  * leaves the pair out, or lets the later member win.
65
+ *
66
+ * A `$ref` that names nothing is not a contradiction, so its pair is not left
67
+ * out for it: the `DanglingRefError` is thrown on, and `system.danglingRefs`
68
+ * decides (`allOfMember`).
64
69
  */
65
70
  const mergePairs = ({ pairs, merge, system }) => {
66
71
  const mergeEach = (pairSystem) => pairs.flatMap(pair => {
67
72
  try {
68
73
  return [merge(pair, pairSystem)];
69
74
  }
70
- catch (_error) {
75
+ catch (error) {
76
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
77
+ throw error;
78
+ }
71
79
  return [];
72
80
  }
73
81
  });
@@ -120,8 +128,9 @@ exports.mergeCrossProduct = mergeCrossProduct;
120
128
  * to a `oneOf` with `minLength` beside it. Union-level keys (`nullable`,
121
129
  * `description`, …) are not merged in, and a `$ref` member is returned
122
130
  * unchanged. A member that conflicts with the
123
- * siblings is dropped, as in the cross product. A schema with no group is
124
- * its own only member and is returned unchanged.
131
+ * siblings is dropped, as in the cross product, but not one with a `$ref`
132
+ * that names nothing: that is no conflict. A schema with no group is its own
133
+ * only member and is returned unchanged.
125
134
  */
126
135
  const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupType }) => {
127
136
  if (!Array.isArray(schema[groupType])) {
@@ -141,7 +150,10 @@ const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupTy
141
150
  try {
142
151
  return (0, merge_js_1.mergeSchemasOrRefs)({ first: siblingSchema, second: member, getRef, normalize, system });
143
152
  }
144
- catch (_error) {
153
+ catch (error) {
154
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
155
+ throw error;
156
+ }
145
157
  return undefined;
146
158
  }
147
159
  })
@@ -120,13 +120,14 @@ const toMediaTypeItemV3 = ({ mediaTypeItem, mediaType, stackTrail, context }) =>
120
120
  mediaType,
121
121
  encoding,
122
122
  schema: stackTrail.trace('schema', st => (0, toSchemasV3_js_1.toOptionalSchemaV3)({ schema, stackTrail: st, context })),
123
- examples: stackTrail.trace('examples', st => (0, toExamplesV3_js_1.toExamplesV3)({
123
+ // toExamplesV3 traces `examples` (or `example`) itself.
124
+ examples: (0, toExamplesV3_js_1.toExamplesV3)({
124
125
  example,
125
126
  examples,
126
127
  exampleKey: mediaType,
127
- stackTrail: st,
128
+ stackTrail,
128
129
  context
129
- })),
130
+ }),
130
131
  extensionFields
131
132
  };
132
133
  return context.withStackTrail(stackTrail, () => new MediaType_js_1.OasMediaType(fields, context));
@@ -141,13 +141,14 @@ const toParameterV3 = ({ parameter, stackTrail, context }) => {
141
141
  allowEmptyValue,
142
142
  allowReserved,
143
143
  schema: stackTrail.trace('schema', st => (0, toSchemasV3_js_1.toOptionalSchemaV3)({ schema, stackTrail: st, context })),
144
- examples: stackTrail.trace('examples', st => (0, toExamplesV3_js_1.toExamplesV3)({
144
+ // toExamplesV3 traces `examples` (or `example`) itself.
145
+ examples: (0, toExamplesV3_js_1.toExamplesV3)({
145
146
  examples,
146
147
  example,
147
148
  exampleKey: `${name}-${parsedLocation}`,
148
- stackTrail: st,
149
+ stackTrail,
149
150
  context
150
- })),
151
+ }),
151
152
  content: stackTrail.trace('content', st => (0, toMediaTypeItemV3_js_1.toOptionalMediaTypeItemsV3)({ content, stackTrail: st, context })),
152
153
  extensionFields
153
154
  };
@@ -8,7 +8,10 @@ const toRefV31 = ({ ref, refType, stackTrail, context, nullable, readOnly, write
8
8
  if (!(0, isEmpty_js_1.isEmpty)(skipped)) {
9
9
  context.logSkippedFields({ skipped, parent: ref, parentType: 'ref', stackTrail });
10
10
  }
11
- context.registerRef(stackTrail.clone(), $ref);
12
- return context.withStackTrail(stackTrail, () => new Ref_js_1.OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
11
+ const oasRef = context.withStackTrail(stackTrail, () => new Ref_js_1.OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
12
+ // The parse context checks each ref once the document is parsed, and
13
+ // removes what holds a ref that names nothing (`system.danglingRefs`).
14
+ context.registerOasRef(stackTrail.clone(), oasRef);
15
+ return oasRef;
13
16
  };
14
17
  exports.toRefV31 = toRefV31;
@@ -234,7 +234,7 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
234
234
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
235
235
  const merged = (0, merge_intersection_js_1.mergeIntersection)({
236
236
  schema: wrapper,
237
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
237
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
238
238
  normalize: normalize_schema_shape_js_1.normalizeMergeMember,
239
239
  system
240
240
  });
@@ -266,7 +266,9 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
266
266
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
267
267
  const merged = (0, merge_union_js_1.mergeUnion)({
268
268
  schema: withoutNull,
269
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
269
+ // A member's own `allOf`, or a member merged with the keywords beside
270
+ // the `oneOf`, is merged here, so it follows `allOfMember` too.
271
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
270
272
  normalize: normalize_schema_shape_js_1.normalizeMergeMember,
271
273
  system,
272
274
  groupType: 'oneOf'
@@ -347,7 +349,8 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
347
349
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
348
350
  const merged = (0, merge_union_js_1.mergeUnion)({
349
351
  schema: withoutNull,
350
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
352
+ // As for `oneOf`: what is merged here follows `allOfMember`.
353
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
351
354
  normalize: normalize_schema_shape_js_1.normalizeMergeMember,
352
355
  system,
353
356
  groupType: 'anyOf'
@@ -5,6 +5,7 @@ const decompose_union_js_1 = require("./decompose-union.js");
5
5
  const merge_js_1 = require("./merge.js");
6
6
  const cross_product_js_1 = require("./cross-product.js");
7
7
  const refFns_js_1 = require("../../../helpers/refFns.js");
8
+ const DanglingRefError_js_1 = require("../../../oas/ref/DanglingRefError.js");
8
9
  const mergeUnion = ({ schema, getRef, normalize, system, groupType }) => {
9
10
  const { beforeExcluded, decomposed, afterExcluded } = (0, decompose_union_js_1.decomposeUnion)({ schema, groupType });
10
11
  // if (decomposed.length === 1) {
@@ -61,13 +62,20 @@ exports.toOwnSystem = toOwnSystem;
61
62
  * ({@link toOwnSystem}). Inside an outer first pass under `latestWins`, a
62
63
  * cross product with no pair that merges fails its pair: the outer pass then
63
64
  * leaves the pair out, or lets the later member win.
65
+ *
66
+ * A `$ref` that names nothing is not a contradiction, so its pair is not left
67
+ * out for it: the `DanglingRefError` is thrown on, and `system.danglingRefs`
68
+ * decides (`allOfMember`).
64
69
  */
65
70
  const mergePairs = ({ pairs, merge, system }) => {
66
71
  const mergeEach = (pairSystem) => pairs.flatMap(pair => {
67
72
  try {
68
73
  return [merge(pair, pairSystem)];
69
74
  }
70
- catch (_error) {
75
+ catch (error) {
76
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
77
+ throw error;
78
+ }
71
79
  return [];
72
80
  }
73
81
  });
@@ -120,8 +128,9 @@ exports.mergeCrossProduct = mergeCrossProduct;
120
128
  * to a `oneOf` with `minLength` beside it. Union-level keys (`nullable`,
121
129
  * `description`, …) are not merged in, and a `$ref` member is returned
122
130
  * unchanged. A member that conflicts with the
123
- * siblings is dropped, as in the cross product. A schema with no group is
124
- * its own only member and is returned unchanged.
131
+ * siblings is dropped, as in the cross product, but not one with a `$ref`
132
+ * that names nothing: that is no conflict. A schema with no group is its own
133
+ * only member and is returned unchanged.
125
134
  */
126
135
  const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupType }) => {
127
136
  if (!Array.isArray(schema[groupType])) {
@@ -141,7 +150,10 @@ const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupTy
141
150
  try {
142
151
  return (0, merge_js_1.mergeSchemasOrRefs)({ first: siblingSchema, second: member, getRef, normalize, system });
143
152
  }
144
- catch (_error) {
153
+ catch (error) {
154
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
155
+ throw error;
156
+ }
145
157
  return undefined;
146
158
  }
147
159
  })
@@ -135,13 +135,14 @@ const toMediaTypeItemV3 = ({ mediaTypeItem, mediaType, stackTrail, context }) =>
135
135
  itemEncoding,
136
136
  schema: stackTrail.trace('schema', st => (0, toSchemasV3_js_1.toOptionalSchemaV3)({ schema, stackTrail: st, context })),
137
137
  itemSchema: stackTrail.trace('itemSchema', st => (0, toSchemasV3_js_1.toOptionalSchemaV3)({ schema: itemSchema, stackTrail: st, context })),
138
- examples: stackTrail.trace('examples', st => (0, toExamplesV3_js_1.toExamplesV3)({
138
+ // toExamplesV3 traces `examples` (or `example`) itself.
139
+ examples: (0, toExamplesV3_js_1.toExamplesV3)({
139
140
  example,
140
141
  examples,
141
142
  exampleKey: mediaType,
142
- stackTrail: st,
143
+ stackTrail,
143
144
  context
144
- })),
145
+ }),
145
146
  extensionFields
146
147
  };
147
148
  return context.withStackTrail(stackTrail, () => new MediaType_js_1.OasMediaType(fields, context));
@@ -158,13 +158,14 @@ const toParameterV3 = ({ parameter, stackTrail, context }) => {
158
158
  allowEmptyValue,
159
159
  allowReserved,
160
160
  schema: stackTrail.trace('schema', st => (0, toSchemasV3_js_1.toOptionalSchemaV3)({ schema, stackTrail: st, context })),
161
- examples: stackTrail.trace('examples', st => (0, toExamplesV3_js_1.toExamplesV3)({
161
+ // toExamplesV3 traces `examples` (or `example`) itself.
162
+ examples: (0, toExamplesV3_js_1.toExamplesV3)({
162
163
  examples,
163
164
  example,
164
165
  exampleKey: `${name}-${parsedLocation}`,
165
- stackTrail: st,
166
+ stackTrail,
166
167
  context
167
- })),
168
+ }),
168
169
  content: stackTrail.trace('content', st => (0, toMediaTypeItemV3_js_1.toOptionalMediaTypeItemsV3)({ content, stackTrail: st, context })),
169
170
  extensionFields
170
171
  };
@@ -8,7 +8,10 @@ const toRefV31 = ({ ref, refType, stackTrail, context, nullable, readOnly, write
8
8
  if (!(0, isEmpty_js_1.isEmpty)(skipped)) {
9
9
  context.logSkippedFields({ skipped, parent: ref, parentType: 'ref', stackTrail });
10
10
  }
11
- context.registerRef(stackTrail.clone(), $ref);
12
- return context.withStackTrail(stackTrail, () => new Ref_js_1.OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
11
+ const oasRef = context.withStackTrail(stackTrail, () => new Ref_js_1.OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
12
+ // The parse context checks each ref once the document is parsed, and
13
+ // removes what holds a ref that names nothing (`system.danglingRefs`).
14
+ context.registerOasRef(stackTrail.clone(), oasRef);
15
+ return oasRef;
13
16
  };
14
17
  exports.toRefV31 = toRefV31;
@@ -234,7 +234,7 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
234
234
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
235
235
  const merged = (0, merge_intersection_js_1.mergeIntersection)({
236
236
  schema: wrapper,
237
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
237
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
238
238
  normalize: normalize_schema_shape_js_1.normalizeMergeMember,
239
239
  system
240
240
  });
@@ -266,7 +266,9 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
266
266
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
267
267
  const merged = (0, merge_union_js_1.mergeUnion)({
268
268
  schema: withoutNull,
269
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
269
+ // A member's own `allOf`, or a member merged with the keywords beside
270
+ // the `oneOf`, is merged here, so it follows `allOfMember` too.
271
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
270
272
  normalize: normalize_schema_shape_js_1.normalizeMergeMember,
271
273
  system,
272
274
  groupType: 'oneOf'
@@ -347,7 +349,8 @@ const toSchemaV3 = ({ schema, stackTrail, context }) => {
347
349
  const system = (0, allOfSystem_js_1.toAllOfSystem)(context);
348
350
  const merged = (0, merge_union_js_1.mergeUnion)({
349
351
  schema: withoutNull,
350
- getRef: (0, refFns_js_2.toGetRef)(context.documentObject),
352
+ // As for `oneOf`: what is merged here follows `allOfMember`.
353
+ getRef: (0, refFns_js_2.toGetRef)(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
351
354
  normalize: normalize_schema_shape_js_1.normalizeMergeMember,
352
355
  system,
353
356
  groupType: 'anyOf'