@skmtc/core 0.45.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 (187) hide show
  1. package/esm/anchors/attribute.js +3 -2
  2. package/esm/context/CoreContext.d.ts +1 -1
  3. package/esm/context/CoreContext.js +1 -1
  4. package/esm/context/GenerateContext.d.ts +13 -10
  5. package/esm/context/GenerateContext.js +36 -25
  6. package/esm/context/ParseContext.d.ts +3 -0
  7. package/esm/context/ParseContext.js +151 -39
  8. package/esm/context/generateTypes.d.ts +7 -6
  9. package/esm/dsl/GeneratorKeys.d.ts +21 -15
  10. package/esm/dsl/GeneratorKeys.js +16 -11
  11. package/esm/dsl/Inserted.d.ts +1 -1
  12. package/esm/dsl/Inserted.js +1 -1
  13. package/esm/dsl/model/ModelDriver.d.ts +6 -5
  14. package/esm/dsl/model/ModelDriver.js +22 -20
  15. package/esm/dsl/model/toModelEntry.d.ts +18 -18
  16. package/esm/dsl/model/toModelEntry.js +9 -9
  17. package/esm/dsl/model/toModelProjectionBase.d.ts +17 -16
  18. package/esm/dsl/model/toModelProjectionBase.js +12 -8
  19. package/esm/dsl/model/types.d.ts +26 -17
  20. package/esm/dsl/operation/gql/toGqlOperationProjectionBase.d.ts +2 -2
  21. package/esm/dsl/operation/gql/toGqlOperationProjectionBase.js +2 -2
  22. package/esm/dsl/operation/oas/toOasOperationProjectionBase.d.ts +2 -2
  23. package/esm/dsl/operation/oas/toOasOperationProjectionBase.js +2 -2
  24. package/esm/dsl/webhook/toWebhookProjectionBase.d.ts +2 -2
  25. package/esm/dsl/webhook/toWebhookProjectionBase.js +2 -2
  26. package/esm/helpers/collateExamples.js +35 -17
  27. package/esm/helpers/refFns.d.ts +108 -43
  28. package/esm/helpers/refFns.js +166 -57
  29. package/esm/mod.d.ts +1 -0
  30. package/esm/mod.js +1 -0
  31. package/esm/oas/document/Document.d.ts +6 -0
  32. package/esm/oas/document/Document.js +44 -2
  33. package/esm/oas/intersection/Intersection.d.ts +122 -0
  34. package/esm/oas/intersection/Intersection.js +266 -0
  35. package/esm/oas/mediaType/jsonMediaType.d.ts +1 -1
  36. package/esm/oas/mediaType/jsonMediaType.js +3 -3
  37. package/esm/oas/ref/DanglingRefError.d.ts +5 -2
  38. package/esm/oas/ref/DanglingRefError.js +4 -3
  39. package/esm/oas/ref/Ref.d.ts +23 -12
  40. package/esm/oas/ref/Ref.js +68 -72
  41. package/esm/oas/requestBody/RequestBody.d.ts +1 -1
  42. package/esm/oas/requestBody/RequestBody.js +1 -1
  43. package/esm/oas/response/Response.d.ts +1 -1
  44. package/esm/oas/response/Response.js +1 -1
  45. package/esm/oas/schema/Schema.d.ts +4 -2
  46. package/esm/oas/schema/siblings.d.ts +10 -2
  47. package/esm/oas/schema/siblings.js +87 -22
  48. package/esm/oas/schemaPath/traverseSchema.d.ts +2 -0
  49. package/esm/oas/schemaPath/traverseSchema.js +51 -5
  50. package/esm/parse/allOfSystem.d.ts +3 -0
  51. package/esm/parse/allOfSystem.js +2 -1
  52. package/esm/parse/keptIntersection.d.ts +53 -0
  53. package/esm/parse/keptIntersection.js +155 -0
  54. package/esm/parse/keptRefs.d.ts +23 -0
  55. package/esm/parse/keptRefs.js +59 -0
  56. package/esm/parse/toCyclicRefs.d.ts +8 -0
  57. package/esm/parse/toCyclicRefs.js +126 -0
  58. package/esm/parse/toDeclaredTypes.d.ts +20 -0
  59. package/esm/parse/toDeclaredTypes.js +43 -0
  60. package/esm/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
  61. package/esm/parse/v3-0/_merge-all-of/merge-union.js +7 -1
  62. package/esm/parse/v3-0/_merge-all-of/merge.js +13 -0
  63. package/esm/parse/v3-0/_merge-all-of/types.d.ts +3 -0
  64. package/esm/parse/v3-0/intersection/toIntersection.d.ts +12 -0
  65. package/esm/parse/v3-0/intersection/toIntersection.js +61 -0
  66. package/esm/parse/v3-0/ref/toRefTargetV3.d.ts +23 -0
  67. package/esm/parse/v3-0/ref/toRefTargetV3.js +70 -0
  68. package/esm/parse/v3-0/schema/toSchemasV3.js +13 -1
  69. package/esm/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
  70. package/esm/parse/v3-1/_merge-all-of/merge-union.js +7 -1
  71. package/esm/parse/v3-1/_merge-all-of/merge.js +13 -0
  72. package/esm/parse/v3-1/_merge-all-of/types.d.ts +3 -0
  73. package/esm/parse/v3-1/intersection/toIntersection.d.ts +12 -0
  74. package/esm/parse/v3-1/intersection/toIntersection.js +61 -0
  75. package/esm/parse/v3-1/ref/toRefTargetV3.d.ts +23 -0
  76. package/esm/parse/v3-1/ref/toRefTargetV3.js +70 -0
  77. package/esm/parse/v3-1/schema/toSchemasV3.js +15 -2
  78. package/esm/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
  79. package/esm/parse/v3-2/_merge-all-of/merge-union.js +7 -1
  80. package/esm/parse/v3-2/_merge-all-of/merge.js +13 -0
  81. package/esm/parse/v3-2/_merge-all-of/types.d.ts +3 -0
  82. package/esm/parse/v3-2/intersection/toIntersection.d.ts +12 -0
  83. package/esm/parse/v3-2/intersection/toIntersection.js +61 -0
  84. package/esm/parse/v3-2/ref/toRefTargetV3.d.ts +23 -0
  85. package/esm/parse/v3-2/ref/toRefTargetV3.js +70 -0
  86. package/esm/parse/v3-2/schema/toSchemasV3.js +15 -2
  87. package/esm/types/GeneratorType.d.ts +1 -1
  88. package/esm/types/ProjectionOptions.d.ts +1 -1
  89. package/esm/types/ProjectionOptions.js +1 -1
  90. package/esm/types/RefName.d.ts +3 -3
  91. package/esm/types/Settings.d.ts +9 -0
  92. package/esm/types/Settings.js +2 -1
  93. package/esm/types/TypeSystem.d.ts +20 -5
  94. package/package.json +1 -1
  95. package/script/anchors/attribute.js +3 -2
  96. package/script/context/CoreContext.d.ts +1 -1
  97. package/script/context/CoreContext.js +1 -1
  98. package/script/context/GenerateContext.d.ts +13 -10
  99. package/script/context/GenerateContext.js +36 -25
  100. package/script/context/ParseContext.d.ts +3 -0
  101. package/script/context/ParseContext.js +150 -38
  102. package/script/context/generateTypes.d.ts +7 -6
  103. package/script/dsl/GeneratorKeys.d.ts +21 -15
  104. package/script/dsl/GeneratorKeys.js +16 -11
  105. package/script/dsl/Inserted.d.ts +1 -1
  106. package/script/dsl/Inserted.js +1 -1
  107. package/script/dsl/model/ModelDriver.d.ts +6 -5
  108. package/script/dsl/model/ModelDriver.js +22 -20
  109. package/script/dsl/model/toModelEntry.d.ts +18 -18
  110. package/script/dsl/model/toModelEntry.js +9 -9
  111. package/script/dsl/model/toModelProjectionBase.d.ts +17 -16
  112. package/script/dsl/model/toModelProjectionBase.js +12 -8
  113. package/script/dsl/model/types.d.ts +26 -17
  114. package/script/dsl/operation/gql/toGqlOperationProjectionBase.d.ts +2 -2
  115. package/script/dsl/operation/gql/toGqlOperationProjectionBase.js +2 -2
  116. package/script/dsl/operation/oas/toOasOperationProjectionBase.d.ts +2 -2
  117. package/script/dsl/operation/oas/toOasOperationProjectionBase.js +2 -2
  118. package/script/dsl/webhook/toWebhookProjectionBase.d.ts +2 -2
  119. package/script/dsl/webhook/toWebhookProjectionBase.js +2 -2
  120. package/script/helpers/collateExamples.js +35 -17
  121. package/script/helpers/refFns.d.ts +108 -43
  122. package/script/helpers/refFns.js +180 -59
  123. package/script/mod.d.ts +1 -0
  124. package/script/mod.js +1 -0
  125. package/script/oas/document/Document.d.ts +6 -0
  126. package/script/oas/document/Document.js +44 -2
  127. package/script/oas/intersection/Intersection.d.ts +122 -0
  128. package/script/oas/intersection/Intersection.js +270 -0
  129. package/script/oas/mediaType/jsonMediaType.d.ts +1 -1
  130. package/script/oas/mediaType/jsonMediaType.js +3 -3
  131. package/script/oas/ref/DanglingRefError.d.ts +5 -2
  132. package/script/oas/ref/DanglingRefError.js +4 -3
  133. package/script/oas/ref/Ref.d.ts +23 -12
  134. package/script/oas/ref/Ref.js +67 -71
  135. package/script/oas/requestBody/RequestBody.d.ts +1 -1
  136. package/script/oas/requestBody/RequestBody.js +1 -1
  137. package/script/oas/response/Response.d.ts +1 -1
  138. package/script/oas/response/Response.js +1 -1
  139. package/script/oas/schema/Schema.d.ts +4 -2
  140. package/script/oas/schema/siblings.d.ts +10 -2
  141. package/script/oas/schema/siblings.js +89 -23
  142. package/script/oas/schemaPath/traverseSchema.d.ts +2 -0
  143. package/script/oas/schemaPath/traverseSchema.js +50 -4
  144. package/script/parse/allOfSystem.d.ts +3 -0
  145. package/script/parse/allOfSystem.js +2 -1
  146. package/script/parse/keptIntersection.d.ts +53 -0
  147. package/script/parse/keptIntersection.js +164 -0
  148. package/script/parse/keptRefs.d.ts +23 -0
  149. package/script/parse/keptRefs.js +63 -0
  150. package/script/parse/toCyclicRefs.d.ts +8 -0
  151. package/script/parse/toCyclicRefs.js +130 -0
  152. package/script/parse/toDeclaredTypes.d.ts +20 -0
  153. package/script/parse/toDeclaredTypes.js +48 -0
  154. package/script/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
  155. package/script/parse/v3-0/_merge-all-of/merge-union.js +7 -1
  156. package/script/parse/v3-0/_merge-all-of/merge.js +13 -0
  157. package/script/parse/v3-0/_merge-all-of/types.d.ts +3 -0
  158. package/script/parse/v3-0/intersection/toIntersection.d.ts +12 -0
  159. package/script/parse/v3-0/intersection/toIntersection.js +65 -0
  160. package/script/parse/v3-0/ref/toRefTargetV3.d.ts +23 -0
  161. package/script/parse/v3-0/ref/toRefTargetV3.js +74 -0
  162. package/script/parse/v3-0/schema/toSchemasV3.js +13 -1
  163. package/script/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
  164. package/script/parse/v3-1/_merge-all-of/merge-union.js +7 -1
  165. package/script/parse/v3-1/_merge-all-of/merge.js +13 -0
  166. package/script/parse/v3-1/_merge-all-of/types.d.ts +3 -0
  167. package/script/parse/v3-1/intersection/toIntersection.d.ts +12 -0
  168. package/script/parse/v3-1/intersection/toIntersection.js +65 -0
  169. package/script/parse/v3-1/ref/toRefTargetV3.d.ts +23 -0
  170. package/script/parse/v3-1/ref/toRefTargetV3.js +74 -0
  171. package/script/parse/v3-1/schema/toSchemasV3.js +15 -2
  172. package/script/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
  173. package/script/parse/v3-2/_merge-all-of/merge-union.js +7 -1
  174. package/script/parse/v3-2/_merge-all-of/merge.js +13 -0
  175. package/script/parse/v3-2/_merge-all-of/types.d.ts +3 -0
  176. package/script/parse/v3-2/intersection/toIntersection.d.ts +12 -0
  177. package/script/parse/v3-2/intersection/toIntersection.js +65 -0
  178. package/script/parse/v3-2/ref/toRefTargetV3.d.ts +23 -0
  179. package/script/parse/v3-2/ref/toRefTargetV3.js +74 -0
  180. package/script/parse/v3-2/schema/toSchemasV3.js +15 -2
  181. package/script/types/GeneratorType.d.ts +1 -1
  182. package/script/types/ProjectionOptions.d.ts +1 -1
  183. package/script/types/ProjectionOptions.js +1 -1
  184. package/script/types/RefName.d.ts +3 -3
  185. package/script/types/Settings.d.ts +9 -0
  186. package/script/types/Settings.js +2 -1
  187. package/script/types/TypeSystem.d.ts +20 -5
@@ -0,0 +1,70 @@
1
+ import { isPlainObject } from '../../isPlainObject.js';
2
+ import { toOptionalSchemasV3 } from '../schema/toSchemasV3.js';
3
+ import { toOptionalParametersV3 } from '../parameter/toParameterV3.js';
4
+ import { toOptionalResponsesV3 } from '../response/toResponseV3.js';
5
+ import { toRequestBodiesV3 } from '../requestBody/toRequestBodiesV3.js';
6
+ import { toComponentHeadersV3 } from '../header/toHeadersV3.js';
7
+ import { toLinksV3 } from '../link/toLinksV3.js';
8
+ import { toExampleV3 } from '../example/toExamplesV3.js';
9
+ import { toSecuritySchemesV3 } from '../securitySchemes/toSecuritySchemes.js';
10
+ import { tryParseAt } from '../../../context/tryParseAt.js';
11
+ /**
12
+ * The raw value read as the OpenAPI type its parser takes, as the components
13
+ * parser reads `documentObject.components`: the parser checks what it reads.
14
+ */
15
+ const isRawEntry = (value) => isPlainObject(value);
16
+ /**
17
+ * Parses the target of a ref that does not point at a component
18
+ * (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
19
+ * at its own position, with the parser that components of its kind use. Gives
20
+ * `undefined` when the target does not parse (the parser has logged why).
21
+ */
22
+ export const toRefTargetV3 = ({ refType, name, value, stackTrail, context }) => {
23
+ switch (refType) {
24
+ case 'schema':
25
+ return isRawEntry(value)
26
+ ? toOptionalSchemasV3({ schemas: { [name]: value }, stackTrail, context })?.[name]
27
+ : undefined;
28
+ case 'parameter':
29
+ return isRawEntry(value)
30
+ ? toOptionalParametersV3({ parameters: { [name]: value }, stackTrail, context })?.[name]
31
+ : undefined;
32
+ case 'response':
33
+ return isRawEntry(value)
34
+ ? toOptionalResponsesV3({ responses: { [name]: value }, stackTrail, context })?.[name]
35
+ : undefined;
36
+ case 'requestBody':
37
+ return isRawEntry(value)
38
+ ? toRequestBodiesV3({ requestBodies: { [name]: value }, stackTrail, context })?.[name]
39
+ : undefined;
40
+ case 'header':
41
+ return isRawEntry(value)
42
+ ? toComponentHeadersV3({ headers: { [name]: value }, stackTrail, context })?.[name]
43
+ : undefined;
44
+ case 'link':
45
+ return isRawEntry(value)
46
+ ? toLinksV3({ links: { [name]: value }, stackTrail, context })?.[name]
47
+ : undefined;
48
+ case 'example':
49
+ // No components parser reads a single example by key, so the target
50
+ // is parsed as `toExamplesV3` parses each entry, under `tryParseAt`.
51
+ return isRawEntry(value)
52
+ ? tryParseAt({
53
+ stackTrail,
54
+ key: name,
55
+ context,
56
+ type: 'INVALID_EXAMPLE',
57
+ parent: value,
58
+ fn: st => toExampleV3({ example: value, stackTrail: st, context })
59
+ })
60
+ : undefined;
61
+ case 'securityScheme':
62
+ return isRawEntry(value)
63
+ ? toSecuritySchemesV3({ securitySchemes: { [name]: value }, stackTrail, context })?.[name]
64
+ : undefined;
65
+ default: {
66
+ const _exhaustive = refType;
67
+ throw new Error(`Unhandled ref type: ${_exhaustive}`);
68
+ }
69
+ }
70
+ };
@@ -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
@@ -43,7 +43,7 @@ import type { ModelEntry } from '../dsl/model/toModelEntry.js';
43
43
  *
44
44
  * const modelEntry = toModelEntry({
45
45
  * id: 'typescript-models',
46
- * transform: ({ context, refName }) => {}
46
+ * transform: ({ context, ref }) => {}
47
47
  * });
48
48
  * // modelEntry.type === 'model'
49
49
  * ```
@@ -2,7 +2,7 @@
2
2
  * @fileoverview Caller options for projections.
3
3
  *
4
4
  * Options are typed data the CALLING generator passes when it inserts a
5
- * peer: `context.insertModel(Peer, refName, { options })`. Enrichments are
5
+ * peer: `context.insertModel(Peer, ref, { options })`. Enrichments are
6
6
  * the user's per-subject configuration and a variant selects one of them;
7
7
  * options belong to the caller.
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * @fileoverview Caller options for projections.
3
3
  *
4
4
  * Options are typed data the CALLING generator passes when it inserts a
5
- * peer: `context.insertModel(Peer, refName, { options })`. Enrichments are
5
+ * peer: `context.insertModel(Peer, ref, { options })`. Enrichments are
6
6
  * the user's per-subject configuration and a variant selects one of them;
7
7
  * options belong to the caller.
8
8
  *
@@ -99,14 +99,14 @@ import type { Brand } from './Brand.js';
99
99
  * @example In generator contexts
100
100
  * ```typescript
101
101
  * class ModelGenerator extends ModelProjectionBase {
102
- * constructor(args: { refName: RefName; ... }) {
102
+ * constructor(args: { ref: RefParts; ... }) {
103
103
  * super(args);
104
- * // this.refName is guaranteed to be a valid schema reference
104
+ * // this.ref.refName is the name of the model's ref
105
105
  * }
106
106
  *
107
107
  * toDefinition(): Definition {
108
108
  * // RefName ensures we're working with valid schema identifiers
109
- * const schemaName = this.refName; // Type: RefName
109
+ * const schemaName = this.ref.refName; // Type: RefName
110
110
  * return new Definition({
111
111
  * identifier: createType(schemaName),
112
112
  * // ...
@@ -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/); ' +
@@ -7,7 +7,7 @@ import type { GenerateContextType } from '../context/generateTypes.js';
7
7
  import type { CustomValue } from '../dsl/CustomValue.js';
8
8
  import type { OasVoid } from '../oas/void/Void.js';
9
9
  import type { GeneratorKey } from '../dsl/GeneratorKeys.js';
10
- import type { RefName } from './RefName.js';
10
+ import type { RefParts } from '../helpers/refFns.js';
11
11
  /**
12
12
  * Union type representing all possible type system values in the SKMTC code generation system.
13
13
  *
@@ -19,7 +19,7 @@ import type { RefName } from './RefName.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 { RefName } from './RefName.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;
@@ -539,8 +554,8 @@ export type TypeSystemArgs<Schema extends SchemaType> = {
539
554
  destinationPath: string;
540
555
  /** The schema to transform */
541
556
  schema: Schema;
542
- /** Optional root reference name for schema references */
543
- rootRef?: RefName;
557
+ /** The ref of the model whose value tree holds the schema */
558
+ rootRef?: RefParts;
544
559
  /** Whether the schema field is required */
545
560
  required: boolean | undefined;
546
561
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.45.0",
3
+ "version": "0.47.0",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.attribute = void 0;
18
18
  const GeneratorKeys_js_1 = require("../dsl/GeneratorKeys.js");
19
19
  const Definition_js_1 = require("../dsl/Definition.js");
20
+ const refFns_js_1 = require("../helpers/refFns.js");
20
21
  /**
21
22
  * Derive the attribution tuple for a producer.
22
23
  *
@@ -57,7 +58,7 @@ exports.attribute = attribute;
57
58
  *
58
59
  * - OAS operation → `#/paths/<escaped-path>/<method>`
59
60
  * - GQL operation → `<rootKind>.<fieldName>`
60
- * - Model → `#/components/schemas/<refName>`
61
+ * - Model → its ref's pointer: `#/components/schemas/<refName>` for a component
61
62
  * - Container / generator-only / no key → `''` (no schema location to point
62
63
  * at — a container is a place many subjects insert into, so it has no
63
64
  * position of its own; its MEMBERS carry their subjects' keys)
@@ -73,7 +74,7 @@ const schemaPointerFromKey = (parsed) => {
73
74
  case 'gqlOperation':
74
75
  return `${parsed.rootKind}.${parsed.fieldName}`;
75
76
  case 'model':
76
- return `#/components/schemas/${parsed.refName}`;
77
+ return (0, refFns_js_1.toRefPointer)(parsed.ref);
77
78
  case 'container':
78
79
  case 'generator-only':
79
80
  return '';
@@ -264,7 +264,7 @@ export declare class CoreContext {
264
264
  * toGeneratorConfigMap: () => ({
265
265
  * 'typescript-models': toModelEntry({
266
266
  * id: 'typescript-models',
267
- * transform: ({ context, refName }) => {}
267
+ * transform: ({ context, ref }) => {}
268
268
  * }),
269
269
  * 'api-client': toOasOperationEntry({
270
270
  * id: 'api-client',
@@ -299,7 +299,7 @@ class CoreContext {
299
299
  * toGeneratorConfigMap: () => ({
300
300
  * 'typescript-models': toModelEntry({
301
301
  * id: 'typescript-models',
302
- * transform: ({ context, refName }) => {}
302
+ * transform: ({ context, ref }) => {}
303
303
  * }),
304
304
  * 'api-client': toOasOperationEntry({
305
305
  * id: 'api-client',
@@ -2,6 +2,7 @@ import type { DefinitionBase } from '../dsl/Definition.js';
2
2
  import type { PendingDefinition } from '../dsl/PendingDefinition.js';
3
3
  import type { OasSchema } from '../oas/schema/Schema.js';
4
4
  import type { OasRef } from '../oas/ref/Ref.js';
5
+ import { type RefParts } from '../helpers/refFns.js';
5
6
  import type { SkmtcParsedDocument } from '../types/SkmtcDocument.js';
6
7
  import type { SupportedSubjects } from '../types/SupportedSubjects.js';
7
8
  import type { EnrichmentDefaults } from '../types/EnrichmentDefaults.js';
@@ -78,8 +79,8 @@ export type CreateAndRegisterDefinition<Schema extends SchemaType> = {
78
79
  destinationPath: string;
79
80
  /** Function to transform the schema into a generated value */
80
81
  schemaToValueFn: SchemaToValueFn;
81
- /** Optional root reference name for the schema */
82
- rootRef?: RefName;
82
+ /** The ref of the model whose value tree holds the schema */
83
+ rootRef?: RefParts;
83
84
  /** Whether to exclude this definition from exports */
84
85
  noExport?: boolean;
85
86
  };
@@ -120,8 +121,8 @@ export type AddRenderDependencyArgs = {
120
121
  export type ToModelSettingsArgs = {
121
122
  /** The ID of the generator requesting model settings */
122
123
  generatorId: string;
123
- /** The reference name of the model */
124
- refName: RefName;
124
+ /** The model's ref */
125
+ ref: RefParts;
125
126
  };
126
127
  /**
127
128
  * Return type for insert operations in the generation context.
@@ -301,7 +302,7 @@ export declare class GenerateContext implements GenerateContextType {
301
302
  *
302
303
  * @mutates this.files
303
304
  */
304
- insertModel<V extends GeneratedValue, EnrichmentType, ProjectionOptions = undefined>(projection: ModelProjection<V, EnrichmentType, ProjectionOptions>, refName: RefName, ...rest: ProjectionOptionsRest<InsertModelOptions<ProjectionOptions>, ProjectionOptions>): Inserted<V, EnrichmentType>;
305
+ insertModel<V extends GeneratedValue, EnrichmentType, ProjectionOptions = undefined>(projection: ModelProjection<V, EnrichmentType, ProjectionOptions>, ref: RefParts, ...rest: ProjectionOptionsRest<InsertModelOptions<ProjectionOptions>, ProjectionOptions>): Inserted<V, EnrichmentType>;
305
306
  /**
306
307
  * Build content settings for an operation projection by calling its
307
308
  * statics `toIdentifierName` + `toIdentifierType`, `toExportPath`, and
@@ -319,18 +320,20 @@ export declare class GenerateContext implements GenerateContextType {
319
320
  * Build content settings for a model projection by calling its statics
320
321
  * `toIdentifierName` (pure, for the name) + `toIdentifierType`
321
322
  * (context-aware, for type/typeName/exported), `toExportPath`, and
322
- * `toEnrichments` against the given `refName` and `variant`. The full
323
+ * `toEnrichments` against the given `ref` and `variant`. The full
323
324
  * identifier is assembled through the projection's language
324
325
  * (`lang.toIdentifier`).
325
326
  */
326
327
  toModelContentSettings<V extends GeneratedValue, EnrichmentType, ProjectionOptions>(args: BuildModelSettingsArgs<V, EnrichmentType, ProjectionOptions>): ContentSettings<EnrichmentType>;
327
328
  /**
328
- * Perform one lookup of schema by `refName`.
329
- * @param refName
329
+ * Perform one lookup of a model's schema: a component by `refName`, or,
330
+ * with a `refPath` other than `#/components/schemas`, the ref target the
331
+ * parser kept under its whole pointer.
332
+ * @param ref - The model's ref; an `OasRef<'schema'>` fits as it is
330
333
  * @returns Matching schema or ref
331
- * @throws {DanglingRefError} when no schema has that name
334
+ * @throws {DanglingRefError} when no schema is found there
332
335
  */
333
- resolveSchemaRefOnce(refName: RefName): OasSchema | OasRef<'schema'>;
336
+ resolveSchemaRefOnce(ref: RefParts): OasSchema | OasRef<'schema'>;
334
337
  /**
335
338
  * Check if definition name `name` in file with path `exportPath`
336
339
  * has already been created and registered.
@@ -19,6 +19,7 @@ exports.toModelSource = exports.toGqlOperationSource = exports.toWebhookSource =
19
19
  const normalizeWorkspacePath_js_1 = require("../helpers/normalizeWorkspacePath.js");
20
20
  const applyGeneratedSuffix_js_1 = require("../helpers/applyGeneratedSuffix.js");
21
21
  const DanglingRefError_js_1 = require("../oas/ref/DanglingRefError.js");
22
+ const refFns_js_1 = require("../helpers/refFns.js");
22
23
  const Method_js_1 = require("../types/Method.js");
23
24
  const OasOperationDriver_js_1 = require("../dsl/operation/oas/OasOperationDriver.js");
24
25
  const WebhookDriver_js_1 = require("../dsl/webhook/WebhookDriver.js");
@@ -68,11 +69,11 @@ const EnrichmentWarning_js_1 = require("../enrichments/EnrichmentWarning.js");
68
69
  *
69
70
  * class TypeScriptInterface extends ModelProjectionBase {
70
71
  * generate(): Definition {
71
- * const schema = this.context.getSchema(this.refName);
72
+ * const schema = this.context.resolveSchemaRefOnce(this.ref);
72
73
  *
73
74
  * return new Definition({
74
75
  * context: this.context,
75
- * identifier: createType(this.refName),
76
+ * identifier: createType(this.ref.refName),
76
77
  * description: schema.description,
77
78
  * value: {
78
79
  * generatorKey: this.generatorKey,
@@ -369,7 +370,7 @@ class GenerateContext {
369
370
  break;
370
371
  }
371
372
  case 'model': {
372
- const models = __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_schemaRefNames).call(this).filter(refName => generatorConfig.isSupported({ refName, context: this, variant: Variant_js_1.DEFAULT_VARIANT }));
373
+ const models = __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_schemaRefNames).call(this).filter(refName => generatorConfig.isSupported({ ref: (0, refFns_js_1.toSchemaComponentRef)(refName), context: this, variant: Variant_js_1.DEFAULT_VARIANT }));
373
374
  out[generatorConfig.id] = { type: 'model', models };
374
375
  break;
375
376
  }
@@ -450,10 +451,10 @@ class GenerateContext {
450
451
  // `undefined` for schemas it doesn't seed.
451
452
  const models = {};
452
453
  for (const refName of __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_schemaRefNames).call(this)) {
453
- if (!generatorConfig.isSupported({ refName, context: this, variant: Variant_js_1.DEFAULT_VARIANT })) {
454
+ if (!generatorConfig.isSupported({ ref: (0, refFns_js_1.toSchemaComponentRef)(refName), context: this, variant: Variant_js_1.DEFAULT_VARIANT })) {
454
455
  continue;
455
456
  }
456
- const leaf = __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_subjectDefaults).call(this, () => toDefaults({ refName, context: this, variant: Variant_js_1.DEFAULT_VARIANT }));
457
+ const leaf = __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_subjectDefaults).call(this, () => toDefaults({ ref: (0, refFns_js_1.toSchemaComponentRef)(refName), context: this, variant: Variant_js_1.DEFAULT_VARIANT }));
457
458
  if (leaf === undefined)
458
459
  continue;
459
460
  models[refName] = { [Variant_js_1.DEFAULT_VARIANT]: leaf };
@@ -654,7 +655,7 @@ class GenerateContext {
654
655
  insertNormalizedModel(projection, { schema, fallbackName, destinationPath }, ...rest) {
655
656
  const insertOptions = (0, InsertOptions_js_1.toPeerInsertOptions)(rest);
656
657
  if (schema.isRef()) {
657
- const { definition } = this.insertModel(projection, schema.toRefName(), {
658
+ const { definition } = this.insertModel(projection, schema, {
658
659
  ...insertOptions,
659
660
  destinationPath
660
661
  });
@@ -701,14 +702,14 @@ class GenerateContext {
701
702
  *
702
703
  * @mutates this.files
703
704
  */
704
- insertModel(projection, refName, ...rest) {
705
+ insertModel(projection, ref, ...rest) {
705
706
  // The Driver defaults `variant` and `noExport` and reads the caller's
706
707
  // options off the call.
707
708
  const { settings, definition } = new ModelDriver_js_1.ModelDriver({
708
709
  context: this,
709
710
  projection,
710
- refName,
711
- rootRef: refName,
711
+ ref,
712
+ rootRef: ref,
712
713
  ...(0, InsertOptions_js_1.toInsertOptions)(rest)
713
714
  });
714
715
  return new Inserted_js_1.Inserted({ settings, definition });
@@ -815,36 +816,43 @@ class GenerateContext {
815
816
  * Build content settings for a model projection by calling its statics
816
817
  * `toIdentifierName` (pure, for the name) + `toIdentifierType`
817
818
  * (context-aware, for type/typeName/exported), `toExportPath`, and
818
- * `toEnrichments` against the given `refName` and `variant`. The full
819
+ * `toEnrichments` against the given `ref` and `variant`. The full
819
820
  * identifier is assembled through the projection's language
820
821
  * (`lang.toIdentifier`).
821
822
  */
822
823
  toModelContentSettings(args) {
823
- const { refName, projection, variant = Variant_js_1.DEFAULT_VARIANT } = args;
824
+ const { ref, projection, variant = Variant_js_1.DEFAULT_VARIANT } = args;
824
825
  const options = (0, ProjectionOptions_js_1.readProjectionOptions)(args);
825
- const enrichments = projection.toEnrichments({ refName, context: this, variant });
826
+ const enrichments = projection.toEnrichments({ ref, context: this, variant });
826
827
  return new ContentSettings_js_1.ContentSettings({
827
828
  identifier: projection.lang.toIdentifier({
828
- name: projection.toIdentifierName({ refName, enrichments, variant, options }),
829
- ...projection.toIdentifierType(refName, this)
829
+ name: projection.toIdentifierName({ ref, enrichments, variant, options }),
830
+ ...projection.toIdentifierType(ref, this)
830
831
  }),
831
- exportPath: __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_toContentSettingsExportPath).call(this, projection.toExportPath({ refName, enrichments, variant, options }), projection.id),
832
+ exportPath: __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_toContentSettingsExportPath).call(this, projection.toExportPath({ ref, enrichments, variant, options }), projection.id),
832
833
  enrichments,
833
834
  variant
834
835
  });
835
836
  }
836
837
  /**
837
- * Perform one lookup of schema by `refName`.
838
- * @param refName
838
+ * Perform one lookup of a model's schema: a component by `refName`, or,
839
+ * with a `refPath` other than `#/components/schemas`, the ref target the
840
+ * parser kept under its whole pointer.
841
+ * @param ref - The model's ref; an `OasRef<'schema'>` fits as it is
839
842
  * @returns Matching schema or ref
840
- * @throws {DanglingRefError} when no schema has that name
843
+ * @throws {DanglingRefError} when no schema is found there
841
844
  */
842
- resolveSchemaRefOnce(refName) {
845
+ resolveSchemaRefOnce(ref) {
846
+ const { refName, refPath } = ref;
843
847
  switch (this.document.type) {
844
848
  case 'oas': {
845
- const component = this.document.value.components?.schemas?.[refName];
846
- if (!component) {
847
- throw new DanglingRefError_js_1.DanglingRefError(refName);
849
+ const document = this.document.value;
850
+ const component = (0, refFns_js_1.isDefaultRefPath)({ refPath, refType: 'schema' })
851
+ ? document.components?.schemas?.[refName]
852
+ : document.toRefTarget((0, refFns_js_1.toRefPointer)({ refName, refPath }));
853
+ // The parser keeps ref targets of every kind; a model is built only from a schema.
854
+ if (component === undefined || !isSchemaEntry(component)) {
855
+ throw new DanglingRefError_js_1.DanglingRefError((0, refFns_js_1.toModelKey)(ref), { remote: (0, refFns_js_1.isRemoteRefPath)(refPath) });
848
856
  }
849
857
  const target = component.resolveOnce();
850
858
  // One step, with what the component adds next to its reference
@@ -1148,6 +1156,7 @@ _GenerateContext_files = new WeakMap(), _GenerateContext_failedItems = new WeakM
1148
1156
  ? (document.value.components?.toSchemasRefNames() ?? [])
1149
1157
  : document.value.registry.toSchemasRefNames();
1150
1158
  refNames.forEach(refName => {
1159
+ const ref = (0, refFns_js_1.toSchemaComponentRef)(refName);
1151
1160
  stackTrail.trace(refName, refTrail => {
1152
1161
  // Resolve the variant list to fan out over. The consumer's
1153
1162
  // enrichments are keyed `[generatorId][refName][variant]`; the
@@ -1170,7 +1179,7 @@ _GenerateContext_files = new WeakMap(), _GenerateContext_failedItems = new WeakM
1170
1179
  refTrail.trace(['variant', variant], st => this.withSubjectTrail(st, () => {
1171
1180
  try {
1172
1181
  if (typeof generatorConfig.isSupported === 'function' &&
1173
- !generatorConfig.isSupported({ refName, context: this, variant })) {
1182
+ !generatorConfig.isSupported({ ref, context: this, variant })) {
1174
1183
  this.captureCurrentResult('notSupported', st);
1175
1184
  return;
1176
1185
  }
@@ -1199,7 +1208,7 @@ _GenerateContext_files = new WeakMap(), _GenerateContext_failedItems = new WeakM
1199
1208
  }
1200
1209
  generatorConfig.transform({
1201
1210
  context: this,
1202
- refName,
1211
+ ref,
1203
1212
  variant
1204
1213
  });
1205
1214
  const source = (0, exports.toModelSource)({
@@ -1207,7 +1216,7 @@ _GenerateContext_files = new WeakMap(), _GenerateContext_failedItems = new WeakM
1207
1216
  generatorId: generatorConfig.id,
1208
1217
  variant
1209
1218
  });
1210
- __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_addPreview).call(this, source, generatorConfig.toPreviewModule?.({ context: this, refName, variant }));
1219
+ __classPrivateFieldGet(this, _GenerateContext_instances, "m", _GenerateContext_addPreview).call(this, source, generatorConfig.toPreviewModule?.({ context: this, ref, variant }));
1211
1220
  this.captureCurrentResult('success', st);
1212
1221
  }
1213
1222
  catch (error) {
@@ -1488,3 +1497,5 @@ const toIncludeModels = (include, generatorId) => {
1488
1497
  }
1489
1498
  return undefined;
1490
1499
  };
1500
+ /** Whether a ref target is a schema, or a ref to one: what a model is built from. */
1501
+ const isSchemaEntry = (entry) => entry.isRef() ? entry.isSchemaRef() : entry.oasType === 'schema';
@@ -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. */