@skmtc/core 0.46.0 → 0.47.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 (107) hide show
  1. package/esm/context/ParseContext.d.ts +3 -0
  2. package/esm/context/ParseContext.js +17 -7
  3. package/esm/context/generateTypes.d.ts +2 -2
  4. package/esm/helpers/collateExamples.js +35 -17
  5. package/esm/mod.d.ts +1 -0
  6. package/esm/mod.js +1 -0
  7. package/esm/oas/intersection/Intersection.d.ts +122 -0
  8. package/esm/oas/intersection/Intersection.js +266 -0
  9. package/esm/oas/mediaType/jsonMediaType.d.ts +1 -1
  10. package/esm/oas/mediaType/jsonMediaType.js +3 -3
  11. package/esm/oas/requestBody/RequestBody.d.ts +1 -1
  12. package/esm/oas/requestBody/RequestBody.js +1 -1
  13. package/esm/oas/response/Response.d.ts +1 -1
  14. package/esm/oas/response/Response.js +1 -1
  15. package/esm/oas/schema/Schema.d.ts +4 -2
  16. package/esm/oas/schema/siblings.d.ts +10 -2
  17. package/esm/oas/schema/siblings.js +87 -22
  18. package/esm/oas/schemaPath/traverseSchema.d.ts +2 -0
  19. package/esm/oas/schemaPath/traverseSchema.js +40 -0
  20. package/esm/parse/allOfSystem.d.ts +3 -0
  21. package/esm/parse/allOfSystem.js +2 -1
  22. package/esm/parse/keptIntersection.d.ts +53 -0
  23. package/esm/parse/keptIntersection.js +155 -0
  24. package/esm/parse/keptRefs.d.ts +23 -0
  25. package/esm/parse/keptRefs.js +59 -0
  26. package/esm/parse/toCyclicRefs.d.ts +8 -0
  27. package/esm/parse/toCyclicRefs.js +126 -0
  28. package/esm/parse/toDeclaredTypes.d.ts +20 -0
  29. package/esm/parse/toDeclaredTypes.js +43 -0
  30. package/esm/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
  31. package/esm/parse/v3-0/_merge-all-of/merge-union.js +7 -1
  32. package/esm/parse/v3-0/_merge-all-of/merge.js +13 -0
  33. package/esm/parse/v3-0/_merge-all-of/types.d.ts +3 -0
  34. package/esm/parse/v3-0/intersection/toIntersection.d.ts +12 -0
  35. package/esm/parse/v3-0/intersection/toIntersection.js +61 -0
  36. package/esm/parse/v3-0/schema/toSchemasV3.js +13 -1
  37. package/esm/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
  38. package/esm/parse/v3-1/_merge-all-of/merge-union.js +7 -1
  39. package/esm/parse/v3-1/_merge-all-of/merge.js +13 -0
  40. package/esm/parse/v3-1/_merge-all-of/types.d.ts +3 -0
  41. package/esm/parse/v3-1/intersection/toIntersection.d.ts +12 -0
  42. package/esm/parse/v3-1/intersection/toIntersection.js +61 -0
  43. package/esm/parse/v3-1/schema/toSchemasV3.js +15 -2
  44. package/esm/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
  45. package/esm/parse/v3-2/_merge-all-of/merge-union.js +7 -1
  46. package/esm/parse/v3-2/_merge-all-of/merge.js +13 -0
  47. package/esm/parse/v3-2/_merge-all-of/types.d.ts +3 -0
  48. package/esm/parse/v3-2/intersection/toIntersection.d.ts +12 -0
  49. package/esm/parse/v3-2/intersection/toIntersection.js +61 -0
  50. package/esm/parse/v3-2/schema/toSchemasV3.js +15 -2
  51. package/esm/types/Settings.d.ts +9 -0
  52. package/esm/types/Settings.js +2 -1
  53. package/esm/types/TypeSystem.d.ts +17 -2
  54. package/package.json +1 -1
  55. package/script/context/ParseContext.d.ts +3 -0
  56. package/script/context/ParseContext.js +17 -7
  57. package/script/context/generateTypes.d.ts +2 -2
  58. package/script/helpers/collateExamples.js +35 -17
  59. package/script/mod.d.ts +1 -0
  60. package/script/mod.js +1 -0
  61. package/script/oas/intersection/Intersection.d.ts +122 -0
  62. package/script/oas/intersection/Intersection.js +270 -0
  63. package/script/oas/mediaType/jsonMediaType.d.ts +1 -1
  64. package/script/oas/mediaType/jsonMediaType.js +3 -3
  65. package/script/oas/requestBody/RequestBody.d.ts +1 -1
  66. package/script/oas/requestBody/RequestBody.js +1 -1
  67. package/script/oas/response/Response.d.ts +1 -1
  68. package/script/oas/response/Response.js +1 -1
  69. package/script/oas/schema/Schema.d.ts +4 -2
  70. package/script/oas/schema/siblings.d.ts +10 -2
  71. package/script/oas/schema/siblings.js +89 -23
  72. package/script/oas/schemaPath/traverseSchema.d.ts +2 -0
  73. package/script/oas/schemaPath/traverseSchema.js +40 -0
  74. package/script/parse/allOfSystem.d.ts +3 -0
  75. package/script/parse/allOfSystem.js +2 -1
  76. package/script/parse/keptIntersection.d.ts +53 -0
  77. package/script/parse/keptIntersection.js +164 -0
  78. package/script/parse/keptRefs.d.ts +23 -0
  79. package/script/parse/keptRefs.js +63 -0
  80. package/script/parse/toCyclicRefs.d.ts +8 -0
  81. package/script/parse/toCyclicRefs.js +130 -0
  82. package/script/parse/toDeclaredTypes.d.ts +20 -0
  83. package/script/parse/toDeclaredTypes.js +48 -0
  84. package/script/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
  85. package/script/parse/v3-0/_merge-all-of/merge-union.js +7 -1
  86. package/script/parse/v3-0/_merge-all-of/merge.js +13 -0
  87. package/script/parse/v3-0/_merge-all-of/types.d.ts +3 -0
  88. package/script/parse/v3-0/intersection/toIntersection.d.ts +12 -0
  89. package/script/parse/v3-0/intersection/toIntersection.js +65 -0
  90. package/script/parse/v3-0/schema/toSchemasV3.js +13 -1
  91. package/script/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
  92. package/script/parse/v3-1/_merge-all-of/merge-union.js +7 -1
  93. package/script/parse/v3-1/_merge-all-of/merge.js +13 -0
  94. package/script/parse/v3-1/_merge-all-of/types.d.ts +3 -0
  95. package/script/parse/v3-1/intersection/toIntersection.d.ts +12 -0
  96. package/script/parse/v3-1/intersection/toIntersection.js +65 -0
  97. package/script/parse/v3-1/schema/toSchemasV3.js +15 -2
  98. package/script/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
  99. package/script/parse/v3-2/_merge-all-of/merge-union.js +7 -1
  100. package/script/parse/v3-2/_merge-all-of/merge.js +13 -0
  101. package/script/parse/v3-2/_merge-all-of/types.d.ts +3 -0
  102. package/script/parse/v3-2/intersection/toIntersection.d.ts +12 -0
  103. package/script/parse/v3-2/intersection/toIntersection.js +65 -0
  104. package/script/parse/v3-2/schema/toSchemasV3.js +15 -2
  105. package/script/types/Settings.d.ts +9 -0
  106. package/script/types/Settings.js +2 -1
  107. package/script/types/TypeSystem.d.ts +17 -2
@@ -1,5 +1,6 @@
1
1
  import type { OpenAPIV3 } from 'openapi-types';
2
2
  import * as v from 'valibot';
3
+ import type { KeptRefs } from '../../keptRefs.js';
3
4
  export type GetRefFn = (ref: OpenAPIV3.ReferenceObject) => OpenAPIV3.SchemaObject;
4
5
  /**
5
6
  * Maps a schema the merge reads (a member, a nested property or item schema,
@@ -28,6 +29,8 @@ export type MergeSystem = {
28
29
  anyOf?: 'union' | 'unknown';
29
30
  /** The keywords the merge read as any value: `anyOf` under `system.anyOf: unknown`. */
30
31
  unknown?: Set<string>;
32
+ /** The refs that stay refs (`system.intersections`). Left out: every ref is followed. */
33
+ keptRefs?: KeptRefs;
31
34
  };
32
35
  export type OneOfObject = {
33
36
  oneOf?: (OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject)[];
@@ -0,0 +1,12 @@
1
+ import type { OpenAPIV3 } from 'openapi-types';
2
+ import type { ParseContextType } from '../../../context/parseTypes.js';
3
+ import type { StackTrail } from '../../../context/StackTrail.js';
4
+ import { OasIntersection } from '../../../oas/intersection/Intersection.js';
5
+ export type ToIntersectionArgs = {
6
+ /** The intersection's own keywords, without `allOf`. */
7
+ value: OpenAPIV3.SchemaObject;
8
+ members: (OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject)[];
9
+ stackTrail: StackTrail;
10
+ context: ParseContextType;
11
+ };
12
+ export declare const toIntersection: ({ value, members, stackTrail, context }: ToIntersectionArgs) => OasIntersection;
@@ -0,0 +1,61 @@
1
+ import { OasIntersection } from '../../../oas/intersection/Intersection.js';
2
+ import { isRef } from '../../../helpers/refFns.js';
3
+ import { hasShapeKeyword } from '../../anyOfSystem.js';
4
+ import { withKeywordType } from '../../keptIntersection.js';
5
+ import { toSchemaV3 } from '../schema/toSchemasV3.js';
6
+ import { toUnknown } from '../unknown/toUnknown.js';
7
+ import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
8
+ import { parseNullable } from '../_helpers/parseNullable.js';
9
+ import { parseExample } from '../_helpers/parseExample.js';
10
+ import { parseDefault } from '../_helpers/parseDefault.js';
11
+ import { toExternalDocs } from '../externalDocs/toExternalDocs.js';
12
+ export const toIntersection = ({ value, members, stackTrail, context }) => {
13
+ const { nullable, value: valueWithoutNullable } = parseNullable({ value, context, stackTrail });
14
+ const { title, description, externalDocs, readOnly, writeOnly, deprecated, example: unparsedExample, default: unparsedDefaultValue, ...skipped } = valueWithoutNullable;
15
+ const example = parseExample({
16
+ value: unparsedExample,
17
+ context,
18
+ parent: value,
19
+ nullable,
20
+ check: isPresent,
21
+ toMessage: item => `Removed invalid example. Expected a non-null value, got: ${item}`,
22
+ stackTrail
23
+ });
24
+ const defaultValue = parseDefault({
25
+ value: unparsedDefaultValue,
26
+ context,
27
+ parent: value,
28
+ nullable,
29
+ check: isPresent,
30
+ toMessage: item => `Removed invalid default. Expected a non-null value, got: ${item}`,
31
+ stackTrail
32
+ });
33
+ const extensionFields = toSpecificationExtensionsV3({
34
+ skipped,
35
+ parent: value,
36
+ context,
37
+ stackTrail,
38
+ parentType: 'schema:allOf'
39
+ });
40
+ // A member with no shape of its own (`{ description }`) is any value: the
41
+ // parser must not guess a type from its `example`.
42
+ const membersParsed = members.map(withKeywordType).map((member, index) => stackTrail.trace(`${index}`, st => isRef(member) || hasShapeKeyword(member) || 'anyOf' in member || 'not' in member
43
+ ? toSchemaV3({ schema: member, stackTrail: st, context })
44
+ : toUnknown({ value: member, stackTrail: st, context })));
45
+ return context.withStackTrail(stackTrail, () => new OasIntersection({
46
+ title,
47
+ description,
48
+ externalDocs: stackTrail.trace('externalDocs', st => toExternalDocs({ externalDocs, stackTrail: st, context })),
49
+ nullable,
50
+ readOnly,
51
+ writeOnly,
52
+ deprecated,
53
+ default: defaultValue,
54
+ members: membersParsed,
55
+ example,
56
+ extensionFields
57
+ }, context));
58
+ };
59
+ // A value matches every member, so without resolving them the most we can
60
+ // check is that a present default/example is not `null` on a non-nullable schema.
61
+ const isPresent = (value) => value !== null && value !== undefined;
@@ -10,6 +10,8 @@ import { toBoolean } from '../boolean/toBoolean.js';
10
10
  import { toString } from '../string/toString.js';
11
11
  import { toUnknown } from '../unknown/toUnknown.js';
12
12
  import { toUnion } from '../union/toUnion.js';
13
+ import { toIntersection } from '../intersection/toIntersection.js';
14
+ import { toIntersectionParts, toSiteIntersection } from '../../keptIntersection.js';
13
15
  import { toGetRef } from '../../../helpers/refFns.js';
14
16
  import { toSiblingKeywords } from '../../toSiblingKeywords.js';
15
17
  import { logAllOfContradictions, toAllOfSystem } from '../../allOfSystem.js';
@@ -132,8 +134,9 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
132
134
  });
133
135
  }
134
136
  const members = Array.isArray(allOf) ? allOf : [];
137
+ const bareRef = context.keptRefs?.copy(schema, { $ref }) ?? { $ref };
135
138
  return Object.keys(keywords).length > 0
136
- ? toSchemaV3({ schema: { ...keywords, allOf: [{ $ref }, ...members] }, stackTrail, context })
139
+ ? toSchemaV3({ schema: { ...keywords, allOf: [bareRef, ...members] }, stackTrail, context })
137
140
  : toRefV31({ ref: schema, refType: 'schema', stackTrail, context });
138
141
  }
139
142
  // OpenAPI `not` has no faithful TypeScript representation: a generated type
@@ -235,7 +238,17 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
235
238
  });
236
239
  logAllOfContradictions({ system, stackTrail, parent: schema, context });
237
240
  logMergedUnknownAnyOf({ system, stackTrail, parent: schema, context });
238
- return toSchemaV3({ schema: merged, stackTrail: st, context });
241
+ const parts = system.keptRefs && toIntersectionParts(merged, system.keptRefs);
242
+ if (parts === undefined) {
243
+ return toSchemaV3({ schema: merged, stackTrail: st, context });
244
+ }
245
+ const { own, members: kept } = toSiteIntersection(parts);
246
+ const [only] = kept;
247
+ if (kept.length > 1 || only === undefined) {
248
+ return toIntersection({ value: own, members: kept, stackTrail: st, context });
249
+ }
250
+ // An intersection of one ref is that ref with the keywords beside it.
251
+ return toSchemaV3({ schema: isEmpty(own) ? only : { ...own, allOf: [only] }, stackTrail: st, context });
239
252
  });
240
253
  }
241
254
  // A `oneOf`/`anyOf` of only null members reads as nullable, not as an
@@ -470,6 +470,15 @@ export type SystemSettings = {
470
470
  * default, `[]`, lists none.
471
471
  */
472
472
  danglingRefs?: DanglingRefKind[];
473
+ /**
474
+ * The `$ref`s that stay refs where core would merge their target in: the
475
+ * members of an `allOf`, and the members of a `oneOf` or `anyOf` with a
476
+ * keyword beside it that merges into each member. The schema is then an
477
+ * `OasIntersection` of the refs and one schema that the other members merge
478
+ * into. `cycles` (the default): a `$ref` whose target leads back to the
479
+ * schema that holds it. `all`: each one. `merge`: none.
480
+ */
481
+ intersections?: 'merge' | 'cycles' | 'all';
473
482
  };
474
483
  export type ClientSettings = {
475
484
  /** Base output path for generated files */
@@ -192,7 +192,8 @@ export const systemSettings = v.object({
192
192
  responseContradictions: v.optional(v.picklist(['strict', 'unknown'])),
193
193
  not: v.optional(v.picklist(['strict', 'unknown'])),
194
194
  anyOf: v.optional(v.picklist(['union', 'unknown'])),
195
- danglingRefs: v.optional(v.array(v.picklist(danglingRefKinds)))
195
+ danglingRefs: v.optional(v.array(v.picklist(danglingRefKinds))),
196
+ intersections: v.optional(v.picklist(['merge', 'cycles', 'all']))
196
197
  });
197
198
  export const clientSettings = v.object({
198
199
  basePath: v.optional(v.pipe(v.string(), v.check(basePath => !isAbsolutePath(basePath), 'basePath must be relative to the SKMTC root (the directory containing .skmtc/); ' +
@@ -19,7 +19,7 @@ import type { RefParts } from '../helpers/refFns.js';
19
19
  * ## Type Categories
20
20
  *
21
21
  * - **Primitive Types**: `string`, `number`, `integer`, `boolean`, `null`
22
- * - **Complex Types**: `array`, `object`, `union`
22
+ * - **Complex Types**: `array`, `object`, `union`, `intersection`
23
23
  * - **Special Types**: `void`, `never`, `unknown`, `custom`
24
24
  * - **Reference Types**: `ref` for schema references
25
25
  *
@@ -63,7 +63,7 @@ import type { RefParts } from '../helpers/refFns.js';
63
63
  * }
64
64
  * ```
65
65
  */
66
- export type TypeSystemValue = TypeSystemArray | TypeSystemObject | TypeSystemUnion | TypeSystemString | TypeSystemNumber | TypeSystemInteger | TypeSystemBoolean | TypeSystemUnknown | TypeSystemVoid | TypeSystemNever | TypeSystemRef | TypeSystemNull | TypeSystemCustom;
66
+ export type TypeSystemValue = TypeSystemArray | TypeSystemObject | TypeSystemUnion | TypeSystemIntersection | TypeSystemString | TypeSystemNumber | TypeSystemInteger | TypeSystemBoolean | TypeSystemUnknown | TypeSystemVoid | TypeSystemNever | TypeSystemRef | TypeSystemNull | TypeSystemCustom;
67
67
  /**
68
68
  * Type system representation of a reference to another schema.
69
69
  *
@@ -360,6 +360,17 @@ export type TypeSystemUnion = {
360
360
  /** Optional generator-specific key for tracking */
361
361
  generatorKey?: GeneratorKey;
362
362
  };
363
+ /** Type system representation of an intersection: a value that is of every member's type. */
364
+ export type TypeSystemIntersection = {
365
+ /** Discriminator for the intersection type */
366
+ type: 'intersection';
367
+ /** The types a value has all of */
368
+ members: TypeSystemValue[];
369
+ /** Type modifiers (optional, nullable, etc.) */
370
+ modifiers: Modifiers;
371
+ /** Optional generator-specific key for tracking */
372
+ generatorKey?: GeneratorKey;
373
+ };
363
374
  /**
364
375
  * Type system representation of object types.
365
376
  *
@@ -464,6 +475,10 @@ export type SchemaToTypeSystemMap = {
464
475
  source: OasSchema;
465
476
  output: TypeSystemUnion;
466
477
  };
478
+ intersection: {
479
+ source: OasSchema;
480
+ output: TypeSystemIntersection;
481
+ };
467
482
  custom: {
468
483
  source: CustomValue;
469
484
  output: TypeSystemCustom;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.46.0",
3
+ "version": "0.47.0",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -25,6 +25,7 @@ import { GqlDocument } from '../gql/document/GqlDocument.js';
25
25
  import type { Logger } from '../types/Logger.js';
26
26
  export type { AttributionState } from '../types/AttributionState.js';
27
27
  import { StackTrail } from './StackTrail.js';
28
+ import { type KeptRefs } from '../parse/keptRefs.js';
28
29
  import type { SkmtcDocumentInput, SkmtcParsedDocument } from '../types/SkmtcDocument.js';
29
30
  import type { GqlParseOptions, LogAtArgs, LogIssueArgs, LogIssueAtArgs, LogIssueNoKeyArgs, LogSkippedValuesArgs } from './parseTypes.js';
30
31
  import type { ParseIssue } from './ParseIssue.js';
@@ -101,6 +102,8 @@ export declare class ParseContext {
101
102
  get oasDocument(): OasDocument;
102
103
  /** OAS-only accessor; symmetric reason to {@link oasDocument}. */
103
104
  get documentObject(): OpenAPIV3.Document;
105
+ /** The refs of the document that a merge keeps as refs (`system.intersections`); `undefined` under `merge`. */
106
+ get keptRefs(): KeptRefs | undefined;
104
107
  /** GQL-only accessor; throws if called on an OAS context. */
105
108
  get schema(): GraphQLSchema;
106
109
  /** GQL-only accessor; throws if called on an OAS context. */
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ 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");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
2
7
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
8
  if (kind === "m") throw new TypeError("Private method is not writable");
4
9
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
11
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
12
  };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- 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");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_oasRefs, _ParseContext_refTargets, _ParseContext_refTarget, _ParseContext_issueIdentities, _ParseContext_rootFrames, _ParseContext_removeComponent, _ParseContext_parseRefTargets, _ParseContext_inRefTarget, _ParseContext_addIssue, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_logDependencyIssue, _ParseContext_toDocumentTrail, _ParseContext_checkRefs, _ParseContext_toRefError, _ParseContext_toLoopOf, _ParseContext_removeHolder;
13
+ var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_oasRefs, _ParseContext_refTargets, _ParseContext_refTarget, _ParseContext_keptRefs, _ParseContext_issueIdentities, _ParseContext_rootFrames, _ParseContext_removeComponent, _ParseContext_parseRefTargets, _ParseContext_inRefTarget, _ParseContext_addIssue, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_logDependencyIssue, _ParseContext_toDocumentTrail, _ParseContext_checkRefs, _ParseContext_toRefError, _ParseContext_toLoopOf, _ParseContext_removeHolder;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.ParseContext = void 0;
16
16
  /**
@@ -42,6 +42,7 @@ const GqlDocument_js_1 = require("../gql/document/GqlDocument.js");
42
42
  const parseGqlDocument_js_1 = require("../gql/document/parseGqlDocument.js");
43
43
  const StackTrail_js_1 = require("./StackTrail.js");
44
44
  const isPlainObject_js_1 = require("../parse/isPlainObject.js");
45
+ const keptRefs_js_1 = require("../parse/keptRefs.js");
45
46
  const pathParameters_js_1 = require("../parse/pathParameters.js");
46
47
  const SerializedError_js_1 = require("../types/SerializedError.js");
47
48
  const tryParseAt_js_1 = require("./tryParseAt.js");
@@ -174,6 +175,8 @@ class ParseContext {
174
175
  );
175
176
  /** The key of the ref target `#inRefTarget` runs for; `undefined` in the document's own walk. */
176
177
  _ParseContext_refTarget.set(this, void 0);
178
+ /** {@link keptRefs}, once a merge has asked for it. */
179
+ _ParseContext_keptRefs.set(this, void 0);
177
180
  /** What identifies each issue logged so far (`toIssueIdentity`). */
178
181
  _ParseContext_issueIdentities.set(this, new Set()
179
182
  /**
@@ -239,6 +242,13 @@ class ParseContext {
239
242
  }
240
243
  return this.protocol.documentObject;
241
244
  }
245
+ /** The refs of the document that a merge keeps as refs (`system.intersections`); `undefined` under `merge`. */
246
+ get keptRefs() {
247
+ __classPrivateFieldSet(this, _ParseContext_keptRefs, __classPrivateFieldGet(this, _ParseContext_keptRefs, "f") ?? {
248
+ value: (0, keptRefs_js_1.toKeptRefs)({ documentObject: this.documentObject, setting: this.system.intersections })
249
+ }, "f");
250
+ return __classPrivateFieldGet(this, _ParseContext_keptRefs, "f").value;
251
+ }
242
252
  /** GQL-only accessor; throws if called on an OAS context. */
243
253
  get schema() {
244
254
  if (this.protocol.type !== 'gql') {
@@ -623,7 +633,7 @@ class ParseContext {
623
633
  }
624
634
  }
625
635
  exports.ParseContext = ParseContext;
626
- _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMap(), _ParseContext_oasRefs = new WeakMap(), _ParseContext_refTargets = new WeakMap(), _ParseContext_refTarget = new WeakMap(), _ParseContext_issueIdentities = new WeakMap(), _ParseContext_rootFrames = new WeakMap(), _ParseContext_instances = new WeakSet(), _ParseContext_removeComponent = function _ParseContext_removeComponent(refKey) {
636
+ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMap(), _ParseContext_oasRefs = new WeakMap(), _ParseContext_refTargets = new WeakMap(), _ParseContext_refTarget = new WeakMap(), _ParseContext_keptRefs = new WeakMap(), _ParseContext_issueIdentities = new WeakMap(), _ParseContext_rootFrames = new WeakMap(), _ParseContext_instances = new WeakSet(), _ParseContext_removeComponent = function _ParseContext_removeComponent(refKey) {
627
637
  if (this.protocol.type !== 'oas') {
628
638
  return;
629
639
  }
@@ -170,8 +170,8 @@ export type AnyOasDocument = OpenAPIV2.Document | OpenAPIV3.Document | OpenAPIV3
170
170
  *
171
171
  * - `WILDCARD_MEDIA_TYPE` (`warning`) — the body has no JSON media type
172
172
  * (see `isJsonMediaType`) and is read from `*\/*` as JSON, since its schema
173
- * there is an object, array, union or `$ref`. The document does not say the
174
- * format; springdoc writes `*\/*` for every response.
173
+ * there is an object, array, union, intersection or `$ref`. The document
174
+ * does not say the format; springdoc writes `*\/*` for every response.
175
175
  * - `NO_JSON_MEDIA_TYPE` (`debug`) — no lookup without a media type reads the
176
176
  * body, and its keys include a range: `application/*`, or `*\/*` with a
177
177
  * scalar schema (a text body or a file). A body with only specific
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.collateExamples = void 0;
4
4
  require("../_dnt.polyfills.js");
5
5
  const isEmpty_js_1 = require("./isEmpty.js");
6
+ const siblings_js_1 = require("../oas/schema/siblings.js");
6
7
  const DanglingRefError_js_1 = require("../oas/ref/DanglingRefError.js");
7
8
  /**
8
9
  * Recursively collates and builds example values from OpenAPI schemas.
@@ -166,23 +167,7 @@ const collateExamples = ({ objectSchema, depth, seen }) => {
166
167
  // legitimately deep one fail identically, and a caller that only wanted a
167
168
  // sample body gets an exception instead. Cutting the cycle here is what
168
169
  // makes the depth limit mean what it says.
169
- if (seen?.has(objectSchema.$ref)) {
170
- return undefined;
171
- }
172
- try {
173
- return (0, exports.collateExamples)({
174
- objectSchema: objectSchema.resolve(),
175
- depth: depth + 1,
176
- seen: new Set(seen ?? []).add(objectSchema.$ref)
177
- });
178
- }
179
- catch (error) {
180
- // A dangling reference names no schema, so it has no example either.
181
- if (DanglingRefError_js_1.DanglingRefError.is(error)) {
182
- return undefined;
183
- }
184
- throw error;
185
- }
170
+ return collateRef({ ref: objectSchema, depth: depth + 1, seen });
186
171
  }
187
172
  case 'object': {
188
173
  if (objectSchema.example) {
@@ -238,6 +223,19 @@ const collateExamples = ({ objectSchema, depth, seen }) => {
238
223
  }
239
224
  return undefined;
240
225
  }
226
+ case 'intersection': {
227
+ if (objectSchema.example) {
228
+ return objectSchema.example;
229
+ }
230
+ // A value matches every member, so the examples of objects are one object.
231
+ // Its members are as deep as the intersection: they are one value.
232
+ const examples = objectSchema.members
233
+ .map(member => member.isRef()
234
+ ? collateRef({ ref: member, depth, seen })
235
+ : (0, exports.collateExamples)({ objectSchema: member, depth, seen }))
236
+ .filter(example => Boolean(example));
237
+ return examples.every(siblings_js_1.isObjectValue) ? toMergedExample(examples) : examples[0];
238
+ }
241
239
  default: {
242
240
  const _exhaustive = objectSchema;
243
241
  throw new Error(`Unhandled schema type: ${_exhaustive.type}`);
@@ -245,3 +243,23 @@ const collateExamples = ({ objectSchema, depth, seen }) => {
245
243
  }
246
244
  };
247
245
  exports.collateExamples = collateExamples;
246
+ /** The example of what a reference resolves to. A reference on the path to itself, or one that names nothing, has none. */
247
+ const collateRef = ({ ref, depth, seen }) => {
248
+ if (seen?.has(ref.$ref)) {
249
+ return undefined;
250
+ }
251
+ try {
252
+ return (0, exports.collateExamples)({
253
+ objectSchema: ref.resolve(),
254
+ depth,
255
+ seen: new Set(seen ?? []).add(ref.$ref)
256
+ });
257
+ }
258
+ catch (error) {
259
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
260
+ return undefined;
261
+ }
262
+ throw error;
263
+ }
264
+ };
265
+ const toMergedExample = (examples) => examples.length === 0 ? undefined : Object.assign({}, ...examples);
package/script/mod.d.ts CHANGED
@@ -181,6 +181,7 @@ export * from './parse/v3-0/string/toString.js';
181
181
  export * from './oas/tag/Tag.js';
182
182
  export * from './oas/tag/tag-types.js';
183
183
  export * from './oas/union/Union.js';
184
+ export * from './oas/intersection/Intersection.js';
184
185
  export * from './parse/v3-0/union/toUnion.js';
185
186
  export * from './oas/unknown/Unknown.js';
186
187
  export * from './oas/unknown/unknown-types.js';
package/script/mod.js CHANGED
@@ -197,6 +197,7 @@ __exportStar(require("./parse/v3-0/string/toString.js"), exports);
197
197
  __exportStar(require("./oas/tag/Tag.js"), exports);
198
198
  __exportStar(require("./oas/tag/tag-types.js"), exports);
199
199
  __exportStar(require("./oas/union/Union.js"), exports);
200
+ __exportStar(require("./oas/intersection/Intersection.js"), exports);
200
201
  __exportStar(require("./parse/v3-0/union/toUnion.js"), exports);
201
202
  __exportStar(require("./oas/unknown/Unknown.js"), exports);
202
203
  __exportStar(require("./oas/unknown/unknown-types.js"), exports);
@@ -0,0 +1,122 @@
1
+ import type { OasUnknown } from '../unknown/Unknown.js';
2
+ import type { SchemaPath } from '../../types/SchemaPath.js';
3
+ import type { OasExternalDocs } from '../externalDocs/ExternalDocs.js';
4
+ import type { OasSchema, ToJsonSchemaOptions } from '../schema/Schema.js';
5
+ import type { OasRef } from '../ref/Ref.js';
6
+ import type { OpenAPIV3 } from 'openapi-types';
7
+ import { OasBase } from '../../types/OasBase.js';
8
+ import type { ParseContextType } from '../../context/parseTypes.js';
9
+ /**
10
+ * Constructor fields for {@link OasIntersection}.
11
+ */
12
+ export type IntersectionFields = {
13
+ /** A short summary of the intersection */
14
+ title?: string;
15
+ /** A description of the intersection */
16
+ description?: string;
17
+ /** External documentation for the intersection */
18
+ externalDocs?: OasExternalDocs | undefined;
19
+ /** Whether the value can be null */
20
+ nullable?: boolean;
21
+ /** Whether the value is sent by the API only */
22
+ readOnly?: boolean;
23
+ /** Whether the value is sent in a request only */
24
+ writeOnly?: boolean;
25
+ /** Whether the value is deprecated */
26
+ deprecated?: boolean;
27
+ /** Example value */
28
+ example?: unknown;
29
+ /** Default value */
30
+ default?: unknown;
31
+ /** The schemas a value matches all of */
32
+ members: (OasSchema | OasRef<'schema'>)[];
33
+ /** Custom extension fields (x-* properties) */
34
+ extensionFields?: Record<string, unknown>;
35
+ };
36
+ /**
37
+ * An `allOf` that keeps at least one reference: a value matches every member.
38
+ *
39
+ * Core merges the members of an `allOf` into one schema. A reference that
40
+ * `client.json` `system.intersections` keeps is not merged: the schema is an
41
+ * `OasIntersection` of the kept references, and at most one other member, which
42
+ * the other members of the `allOf` merge into. By default those are the
43
+ * references that close a cycle.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // LineString: { allOf: [{ $ref: Geometry }, { properties: { coordinates } }] },
48
+ * // where a property of Geometry leads back to LineString
49
+ * lineString.members // [OasRef Geometry, OasObject { coordinates }]
50
+ * ```
51
+ */
52
+ export declare class OasIntersection extends OasBase {
53
+ #private;
54
+ /**
55
+ * Object is part the 'schema' set which is used
56
+ * to define data types in an OpenAPI document.
57
+ */
58
+ oasType: "schema";
59
+ /**
60
+ * Constant value 'intersection' useful for type narrowing and tagged unions.
61
+ */
62
+ type: "intersection";
63
+ /** A short summary of the intersection. */
64
+ title: string | undefined;
65
+ /** A description of the intersection. */
66
+ description: string | undefined;
67
+ /** External documentation for the intersection. */
68
+ externalDocs: OasExternalDocs | undefined;
69
+ /** Indicates whether the value can be null. */
70
+ nullable: boolean | undefined;
71
+ /** Whether the value is sent by the API only. */
72
+ readOnly: boolean | undefined;
73
+ /** Whether the value is sent in a request only. */
74
+ writeOnly: boolean | undefined;
75
+ /** Whether the value is deprecated. */
76
+ deprecated: boolean | undefined;
77
+ /** The schemas, or references to schemas, that a value matches all of. */
78
+ members: (OasSchema | OasRef<'schema'>)[];
79
+ /** Specification Extension fields */
80
+ extensionFields: Record<string, unknown> | undefined;
81
+ /** An example of the value. */
82
+ example?: unknown;
83
+ /** The default value. */
84
+ default?: unknown;
85
+ /**
86
+ * The schema's `not`, kept as an `OasUnknown` under `client.json`
87
+ * `system.not: unknown`. `undefined` otherwise.
88
+ */
89
+ not: OasUnknown | undefined;
90
+ constructor(fields: IntersectionFields, context?: ParseContextType);
91
+ isRef(): this is OasRef<'schema'>;
92
+ /** Whether the value is `readOnly`: the intersection or one of its members is. */
93
+ isReadOnly(): boolean;
94
+ /** Whether the value is `writeOnly`: the intersection or one of its members is. */
95
+ isWriteOnly(): boolean;
96
+ /**
97
+ * The schemas a value matches all of, with each reference resolved and each
98
+ * intersection inside replaced by its own members. Each is given once, so a
99
+ * member that leads back to an intersection ends. A reference that names
100
+ * nothing is left out.
101
+ */
102
+ toParts(): Exclude<OasSchema, OasIntersection>[];
103
+ /**
104
+ * {@link toParts} without the keywords next to the references. `clone` reads
105
+ * these while a reference to this intersection resolves, so no reference may
106
+ * be resolved with its keywords here.
107
+ */
108
+ toTargetParts(): Exclude<OasSchema, OasIntersection>[];
109
+ /**
110
+ * A copy of this schema with what `ref` adds at its site applied, as
111
+ * {@link OasUnion.clone} does: the keywords that this type takes, a
112
+ * sibling's value winning. The keywords of an object next to `ref`
113
+ * (`properties`, `required`) are one more member, unless the members say
114
+ * the same. This schema does not
115
+ * change, and the copy shares its members.
116
+ */
117
+ clone(ref?: OasRef<'schema'>): OasIntersection;
118
+ traverse(path: SchemaPath): OasSchema | OasRef<'schema'>;
119
+ resolve(): OasIntersection;
120
+ resolveOnce(): OasIntersection;
121
+ toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.NonArraySchemaObject;
122
+ }