@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,23 @@
1
+ import type { ParseContextType } from '../../../context/parseTypes.js';
2
+ import type { StackTrail } from '../../../context/StackTrail.js';
3
+ import type { ComponentsEntry } from '../../../oas/components/Components.js';
4
+ import type { OasRefData } from '../../../oas/ref/ref-types.js';
5
+ /** What {@link toRefTargetV3} parses. */
6
+ export type ToRefTargetArgs = {
7
+ /** The kind of component the ref is written for. */
8
+ refType: OasRefData['refType'];
9
+ /** The last segment of the target's pointer, unescaped: the key it parses under. */
10
+ name: string;
11
+ /** The raw value at the pointer. */
12
+ value: Record<string, unknown>;
13
+ /** The trail of the target's parent, so the target parses at its own position. */
14
+ stackTrail: StackTrail;
15
+ context: ParseContextType;
16
+ };
17
+ /**
18
+ * Parses the target of a ref that does not point at a component
19
+ * (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
20
+ * at its own position, with the parser that components of its kind use. Gives
21
+ * `undefined` when the target does not parse (the parser has logged why).
22
+ */
23
+ export declare const toRefTargetV3: ({ refType, name, value, stackTrail, context }: ToRefTargetArgs) => ComponentsEntry | undefined;
@@ -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';
@@ -182,7 +184,17 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
182
184
  });
183
185
  logAllOfContradictions({ system, stackTrail, parent: schema, context });
184
186
  logMergedUnknownAnyOf({ system, stackTrail, parent: schema, context });
185
- return toSchemaV3({ schema: merged, stackTrail: st, context });
187
+ const parts = system.keptRefs && toIntersectionParts(merged, system.keptRefs);
188
+ if (parts === undefined) {
189
+ return toSchemaV3({ schema: merged, stackTrail: st, context });
190
+ }
191
+ const { own, members: kept } = toSiteIntersection(parts);
192
+ const [only] = kept;
193
+ if (kept.length > 1 || only === undefined) {
194
+ return toIntersection({ value: own, members: kept, stackTrail: st, context });
195
+ }
196
+ // An intersection of one ref is that ref with the keywords beside it.
197
+ return toSchemaV3({ schema: isEmpty(own) ? only : { ...own, allOf: [only] }, stackTrail: st, context });
186
198
  });
187
199
  }
188
200
  if ('oneOf' in schema && Array.isArray(schema.oneOf)) {
@@ -1,16 +1,20 @@
1
1
  import { decomposeIntersection } from './decompose-intersection.js';
2
2
  import { mergeSchemasOrRefs } from './merge.js';
3
+ import { isEmpty } from '../../../helpers/isEmpty.js';
4
+ import { splitOwnKeywords, withOwnKeywords } from '../../keptIntersection.js';
3
5
  export const mergeIntersection = ({ schema, getRef, normalize, system }) => {
4
- const decomposed = decomposeIntersection({ schema });
5
- if (decomposed.length === 1) {
6
+ // Where a ref stays a ref, the wrapper's own keywords stay beside the `allOf`.
7
+ const { own, rest } = system.keptRefs?.holds(schema) ? splitOwnKeywords(schema) : { own: {}, rest: schema };
8
+ const decomposed = decomposeIntersection({ schema: rest });
9
+ if (decomposed.length === 1 && isEmpty(own)) {
6
10
  return decomposed[0];
7
11
  }
8
12
  const dereffed = decomposed.map(decomposed => {
9
- return '$ref' in decomposed ? getRef(decomposed) : decomposed;
13
+ return '$ref' in decomposed && !system.keptRefs?.has(decomposed) ? getRef(decomposed) : decomposed;
10
14
  });
11
15
  const result = dereffed.reduce((acc, decomposed) => {
12
16
  const merged = mergeSchemasOrRefs({ first: acc, second: decomposed, getRef, normalize, system });
13
17
  return merged;
14
18
  }, {});
15
- return result;
19
+ return withOwnKeywords(result, own);
16
20
  };
@@ -101,7 +101,13 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, system, gr
101
101
  secondGroup.length !== secondMembers.length;
102
102
  const mergedGroup = mergePairs({
103
103
  pairs: crossProduct(withGroupSiblings({ schema: first, members: firstGroup, getRef, normalize, system, groupType }), withGroupSiblings({ schema: second, members: secondGroup, getRef, normalize, system, groupType })),
104
- merge: ([firstItem, secondItem], pairSystem) => mergeSchemasOrRefs({ first: firstItem, second: secondItem, getRef, normalize, system: pairSystem }),
104
+ merge: ([firstItem, secondItem], pairSystem) => {
105
+ // A kept ref is not read into the merge, so its type is compared here.
106
+ if (pairSystem.allOf === 'strict' && system.keptRefs?.contradict(firstItem, secondItem)) {
107
+ throw new Error('Cannot merge schemas: a member has another type than the reference');
108
+ }
109
+ return mergeSchemasOrRefs({ first: firstItem, second: secondItem, getRef, normalize, system: pairSystem });
110
+ },
105
111
  system
106
112
  });
107
113
  // A nested group is spread into this one below; its `nullable` carries over.
@@ -17,6 +17,7 @@ import { mergeIntersection } from './merge-intersection.js';
17
17
  import { collapseNullOnlyUnion, mergeCrossProduct, toOwnSystem } from './merge-union.js';
18
18
  import { isEmpty } from '../../../helpers/isEmpty.js';
19
19
  import { isNullOnly, mergeNullOnly } from './nullable-merge.js';
20
+ import { mergeKept, toIntersectionParts } from '../../keptIntersection.js';
20
21
  import { withoutUnknownAnyOf } from '../../anyOfSystem.js';
21
22
  export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, getRef, normalize, system }) => {
22
23
  const first = isRef(inputFirst)
@@ -25,6 +26,18 @@ export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, get
25
26
  const second = isRef(inputSecond)
26
27
  ? inputSecond
27
28
  : collapseNullOnlyUnion(normalize(inputSecond), normalize);
29
+ const { keptRefs } = system;
30
+ if (keptRefs && (toIntersectionParts(first, keptRefs) || toIntersectionParts(second, keptRefs))) {
31
+ return mergeKept({
32
+ first,
33
+ second,
34
+ keptRefs,
35
+ merge: (a, b) => mergeSchemasOrRefs({ first: a, second: b, getRef, normalize, system }),
36
+ mergeAllOf: schema => mergeIntersection({ schema, getRef, normalize, system: toOwnSystem(system) }),
37
+ readRef: ref => normalize(getRef(ref)),
38
+ normalize
39
+ });
40
+ }
28
41
  if (containsRef(first, second)) {
29
42
  return mergeWithRef({ first, second, getRef, normalize, system });
30
43
  }
@@ -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;
@@ -0,0 +1,23 @@
1
+ import type { ParseContextType } from '../../../context/parseTypes.js';
2
+ import type { StackTrail } from '../../../context/StackTrail.js';
3
+ import type { ComponentsEntry } from '../../../oas/components/Components.js';
4
+ import type { OasRefData } from '../../../oas/ref/ref-types.js';
5
+ /** What {@link toRefTargetV3} parses. */
6
+ export type ToRefTargetArgs = {
7
+ /** The kind of component the ref is written for. */
8
+ refType: OasRefData['refType'];
9
+ /** The last segment of the target's pointer, unescaped: the key it parses under. */
10
+ name: string;
11
+ /** The raw value at the pointer. */
12
+ value: Record<string, unknown>;
13
+ /** The trail of the target's parent, so the target parses at its own position. */
14
+ stackTrail: StackTrail;
15
+ context: ParseContextType;
16
+ };
17
+ /**
18
+ * Parses the target of a ref that does not point at a component
19
+ * (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
20
+ * at its own position, with the parser that components of its kind use. Gives
21
+ * `undefined` when the target does not parse (the parser has logged why).
22
+ */
23
+ export declare const toRefTargetV3: ({ refType, name, value, stackTrail, context }: ToRefTargetArgs) => ComponentsEntry | undefined;
@@ -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
@@ -1,16 +1,20 @@
1
1
  import { decomposeIntersection } from './decompose-intersection.js';
2
2
  import { mergeSchemasOrRefs } from './merge.js';
3
+ import { isEmpty } from '../../../helpers/isEmpty.js';
4
+ import { splitOwnKeywords, withOwnKeywords } from '../../keptIntersection.js';
3
5
  export const mergeIntersection = ({ schema, getRef, normalize, system }) => {
4
- const decomposed = decomposeIntersection({ schema });
5
- if (decomposed.length === 1) {
6
+ // Where a ref stays a ref, the wrapper's own keywords stay beside the `allOf`.
7
+ const { own, rest } = system.keptRefs?.holds(schema) ? splitOwnKeywords(schema) : { own: {}, rest: schema };
8
+ const decomposed = decomposeIntersection({ schema: rest });
9
+ if (decomposed.length === 1 && isEmpty(own)) {
6
10
  return decomposed[0];
7
11
  }
8
12
  const dereffed = decomposed.map(decomposed => {
9
- return '$ref' in decomposed ? getRef(decomposed) : decomposed;
13
+ return '$ref' in decomposed && !system.keptRefs?.has(decomposed) ? getRef(decomposed) : decomposed;
10
14
  });
11
15
  const result = dereffed.reduce((acc, decomposed) => {
12
16
  const merged = mergeSchemasOrRefs({ first: acc, second: decomposed, getRef, normalize, system });
13
17
  return merged;
14
18
  }, {});
15
- return result;
19
+ return withOwnKeywords(result, own);
16
20
  };
@@ -101,7 +101,13 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, system, gr
101
101
  secondGroup.length !== secondMembers.length;
102
102
  const mergedGroup = mergePairs({
103
103
  pairs: crossProduct(withGroupSiblings({ schema: first, members: firstGroup, getRef, normalize, system, groupType }), withGroupSiblings({ schema: second, members: secondGroup, getRef, normalize, system, groupType })),
104
- merge: ([firstItem, secondItem], pairSystem) => mergeSchemasOrRefs({ first: firstItem, second: secondItem, getRef, normalize, system: pairSystem }),
104
+ merge: ([firstItem, secondItem], pairSystem) => {
105
+ // A kept ref is not read into the merge, so its type is compared here.
106
+ if (pairSystem.allOf === 'strict' && system.keptRefs?.contradict(firstItem, secondItem)) {
107
+ throw new Error('Cannot merge schemas: a member has another type than the reference');
108
+ }
109
+ return mergeSchemasOrRefs({ first: firstItem, second: secondItem, getRef, normalize, system: pairSystem });
110
+ },
105
111
  system
106
112
  });
107
113
  // A nested group is spread into this one below; its `nullable` carries over.
@@ -17,6 +17,7 @@ import { mergeIntersection } from './merge-intersection.js';
17
17
  import { collapseNullOnlyUnion, mergeCrossProduct, toOwnSystem } from './merge-union.js';
18
18
  import { isEmpty } from '../../../helpers/isEmpty.js';
19
19
  import { isNullOnly, mergeNullOnly } from './nullable-merge.js';
20
+ import { mergeKept, toIntersectionParts } from '../../keptIntersection.js';
20
21
  import { withoutUnknownAnyOf } from '../../anyOfSystem.js';
21
22
  export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, getRef, normalize, system }) => {
22
23
  const first = isRef(inputFirst)
@@ -25,6 +26,18 @@ export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, get
25
26
  const second = isRef(inputSecond)
26
27
  ? inputSecond
27
28
  : collapseNullOnlyUnion(normalize(inputSecond), normalize);
29
+ const { keptRefs } = system;
30
+ if (keptRefs && (toIntersectionParts(first, keptRefs) || toIntersectionParts(second, keptRefs))) {
31
+ return mergeKept({
32
+ first,
33
+ second,
34
+ keptRefs,
35
+ merge: (a, b) => mergeSchemasOrRefs({ first: a, second: b, getRef, normalize, system }),
36
+ mergeAllOf: schema => mergeIntersection({ schema, getRef, normalize, system: toOwnSystem(system) }),
37
+ readRef: ref => normalize(getRef(ref)),
38
+ normalize
39
+ });
40
+ }
28
41
  if (containsRef(first, second)) {
29
42
  return mergeWithRef({ first, second, getRef, normalize, system });
30
43
  }
@@ -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;
@@ -0,0 +1,23 @@
1
+ import type { ParseContextType } from '../../../context/parseTypes.js';
2
+ import type { StackTrail } from '../../../context/StackTrail.js';
3
+ import type { ComponentsEntry } from '../../../oas/components/Components.js';
4
+ import type { OasRefData } from '../../../oas/ref/ref-types.js';
5
+ /** What {@link toRefTargetV3} parses. */
6
+ export type ToRefTargetArgs = {
7
+ /** The kind of component the ref is written for. */
8
+ refType: OasRefData['refType'];
9
+ /** The last segment of the target's pointer, unescaped: the key it parses under. */
10
+ name: string;
11
+ /** The raw value at the pointer. */
12
+ value: Record<string, unknown>;
13
+ /** The trail of the target's parent, so the target parses at its own position. */
14
+ stackTrail: StackTrail;
15
+ context: ParseContextType;
16
+ };
17
+ /**
18
+ * Parses the target of a ref that does not point at a component
19
+ * (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
20
+ * at its own position, with the parser that components of its kind use. Gives
21
+ * `undefined` when the target does not parse (the parser has logged why).
22
+ */
23
+ export declare const toRefTargetV3: ({ refType, name, value, stackTrail, context }: ToRefTargetArgs) => ComponentsEntry | undefined;