@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,4 +1,5 @@
1
1
  import type { ParseContextType } from '../context/parseTypes.js';
2
+ import type { KeptRefs } from './keptRefs.js';
2
3
  import type { StackTrail } from '../context/StackTrail.js';
3
4
  /** What the `allOf` merge reads of `client.json#settings.system`, and what it reports back. */
4
5
  type AllOfSystem = {
@@ -9,6 +10,8 @@ type AllOfSystem = {
9
10
  replaced: Set<string>;
10
11
  /** The keywords the merge read as any value: `anyOf` under `system.anyOf: unknown`. */
11
12
  unknown: Set<string>;
13
+ /** The refs that stay refs (`system.intersections`). */
14
+ keptRefs: KeptRefs | undefined;
12
15
  };
13
16
  /**
14
17
  * The `system` settings for one merge: the `allOf` mode, `strict` when the
@@ -12,7 +12,8 @@ const toAllOfSystem = (context) => ({
12
12
  not: context.system?.not ?? 'strict',
13
13
  anyOf: context.system?.anyOf ?? 'union',
14
14
  replaced: new Set(),
15
- unknown: new Set()
15
+ unknown: new Set(),
16
+ keptRefs: context.keptRefs
16
17
  });
17
18
  exports.toAllOfSystem = toAllOfSystem;
18
19
  /**
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The raw form of an intersection inside a merge: an `allOf` of kept refs
3
+ * (`KeptRefs`) and at most one other schema, which the other members merged
4
+ * into, with only `OWN_KEYWORDS` beside it.
5
+ */
6
+ import type { OpenAPIV3 } from 'openapi-types';
7
+ import type { KeptRefs } from './keptRefs.js';
8
+ type SchemaObject = OpenAPIV3.SchemaObject;
9
+ type ReferenceObject = OpenAPIV3.ReferenceObject;
10
+ type SchemaOrReference = SchemaObject | ReferenceObject;
11
+ type Split = {
12
+ own: SchemaObject;
13
+ rest: SchemaObject;
14
+ };
15
+ /** A schema's keywords that stay beside its `allOf`, and the others. */
16
+ export declare const splitOwnKeywords: (schema: SchemaObject) => Split;
17
+ /** A merge result with the wrapper's own keywords, which win, put back on it. */
18
+ export declare const withOwnKeywords: (merged: SchemaOrReference, own: SchemaObject) => SchemaOrReference;
19
+ type Intersection = {
20
+ own: SchemaObject;
21
+ members: SchemaOrReference[];
22
+ };
23
+ /** The parts of a kept ref, or of a schema in the raw form of an intersection; `undefined` for any other schema. */
24
+ export declare const toIntersectionParts: (schema: SchemaOrReference, keptRefs: KeptRefs) => Intersection | undefined;
25
+ type MergeKeptArgs = {
26
+ first: SchemaOrReference;
27
+ second: SchemaOrReference;
28
+ keptRefs: KeptRefs;
29
+ /** Merges two schemas that hold no kept ref. */
30
+ merge: (first: SchemaOrReference, second: SchemaOrReference) => SchemaOrReference;
31
+ /** Merges a schema's own `allOf`. */
32
+ mergeAllOf: (schema: SchemaObject) => SchemaOrReference;
33
+ readRef: (ref: ReferenceObject) => SchemaObject;
34
+ normalize: (schema: SchemaObject) => SchemaObject;
35
+ };
36
+ /**
37
+ * Merges two schemas where at least one is a kept ref or an intersection: the
38
+ * kept refs stay, and the others merge into one member, at the place of the
39
+ * first of them.
40
+ */
41
+ export declare const mergeKept: ({ first, second, keptRefs, merge, mergeAllOf, readRef, normalize }: MergeKeptArgs) => SchemaOrReference;
42
+ /**
43
+ * The parts of a finished merge, to parse: the keywords that say how the whole
44
+ * value is used move from the one member that is not a ref to the intersection.
45
+ */
46
+ export declare const toSiteIntersection: ({ own, members }: Intersection) => Intersection;
47
+ /**
48
+ * A member with the keywords of one type and no shape (`{ required: ['id'] }`),
49
+ * as that type, and the same for the members of its `oneOf` or `anyOf`: the
50
+ * type was in the kept ref, and a schema with no type loses them.
51
+ */
52
+ export declare const withKeywordType: (member: SchemaOrReference) => SchemaOrReference;
53
+ export {};
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ /**
3
+ * The raw form of an intersection inside a merge: an `allOf` of kept refs
4
+ * (`KeptRefs`) and at most one other schema, which the other members merged
5
+ * into, with only `OWN_KEYWORDS` beside it.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.withKeywordType = exports.toSiteIntersection = exports.mergeKept = exports.toIntersectionParts = exports.withOwnKeywords = exports.splitOwnKeywords = void 0;
9
+ const refFns_js_1 = require("../helpers/refFns.js");
10
+ const isEmpty_js_1 = require("../helpers/isEmpty.js");
11
+ const anyOfSystem_js_1 = require("./anyOfSystem.js");
12
+ const isPlainObject_js_1 = require("./isPlainObject.js");
13
+ const nullable_merge_js_1 = require("./v3-0/_merge-all-of/nullable-merge.js");
14
+ /** The keywords of a wrapper that stay beside its `allOf`, out of the merge. `x-*` is one too. */
15
+ const OWN_KEYWORDS = ['title', 'description', 'externalDocs', 'example'];
16
+ /** The keywords that say how the whole value is used. They merge as any keyword, and then are the intersection's own. */
17
+ const SITE_KEYWORDS = ['nullable', 'readOnly', 'writeOnly', 'deprecated', 'default'];
18
+ const isOwnKeyword = (key) => OWN_KEYWORDS.includes(key) || key.startsWith('x-');
19
+ const split = (schema, isOwn) => {
20
+ const entries = Object.entries(schema);
21
+ return {
22
+ own: Object.fromEntries(entries.filter(([key]) => isOwn(key))),
23
+ rest: Object.fromEntries(entries.filter(([key]) => !isOwn(key)))
24
+ };
25
+ };
26
+ /** A schema's keywords that stay beside its `allOf`, and the others. */
27
+ const splitOwnKeywords = (schema) => split(schema, isOwnKeyword);
28
+ exports.splitOwnKeywords = splitOwnKeywords;
29
+ /** A merge result with the wrapper's own keywords, which win, put back on it. */
30
+ const withOwnKeywords = (merged, own) => {
31
+ if ((0, isEmpty_js_1.isEmpty)(own)) {
32
+ return merged;
33
+ }
34
+ return (0, refFns_js_1.isRef)(merged) ? { ...own, allOf: [merged] } : { ...merged, ...own };
35
+ };
36
+ exports.withOwnKeywords = withOwnKeywords;
37
+ /** The parts of a kept ref, or of a schema in the raw form of an intersection; `undefined` for any other schema. */
38
+ const toIntersectionParts = (schema, keptRefs) => {
39
+ if ((0, refFns_js_1.isRef)(schema)) {
40
+ return keptRefs.has(schema) ? { own: {}, members: [schema] } : undefined;
41
+ }
42
+ if (!Array.isArray(schema.allOf)) {
43
+ return undefined;
44
+ }
45
+ const { allOf, ...keywords } = schema;
46
+ const isKept = (member) => (0, refFns_js_1.isRef)(member) && keptRefs.has(member);
47
+ const others = allOf.filter(member => !isKept(member));
48
+ return Object.keys(keywords).every(isOwnKeyword) &&
49
+ allOf.some(isKept) &&
50
+ others.length <= 1 &&
51
+ others.every(member => !(0, refFns_js_1.isRef)(member) && !Array.isArray(member.allOf))
52
+ ? { own: keywords, members: allOf }
53
+ : undefined;
54
+ };
55
+ exports.toIntersectionParts = toIntersectionParts;
56
+ /**
57
+ * Merges two schemas where at least one is a kept ref or an intersection: the
58
+ * kept refs stay, and the others merge into one member, at the place of the
59
+ * first of them.
60
+ */
61
+ const mergeKept = ({ first, second, keptRefs, merge, mergeAllOf, readRef, normalize }) => {
62
+ const toParts = (schema, other) => {
63
+ const parts = (0, exports.toIntersectionParts)(schema, keptRefs);
64
+ if (parts !== undefined) {
65
+ return { ...parts, members: parts.members.map(member => ((0, refFns_js_1.isRef)(member) ? member : normalize(member))) };
66
+ }
67
+ if ((0, refFns_js_1.isRef)(schema)) {
68
+ // The other side keeps a ref to the same target: the two are one ref.
69
+ const isKept = (0, exports.toIntersectionParts)(other, keptRefs)?.members.some(member => (0, refFns_js_1.isRef)(member) && isSameTarget(member, schema));
70
+ return isKept ? { own: {}, members: [schema] } : toParts(readRef(schema), other);
71
+ }
72
+ if (schema.allOf?.length === 0) {
73
+ throw new Error('Empty allOf');
74
+ }
75
+ return schema.allOf ? toParts(mergeAllOf(schema), other) : { own: {}, members: [schema] };
76
+ };
77
+ const firstParts = toParts(first, second);
78
+ const secondParts = toParts(second, first);
79
+ const members = secondParts.members.reduce((acc, member) => {
80
+ const index = acc.findIndex(other => ((0, refFns_js_1.isRef)(member) ? (0, refFns_js_1.isRef)(other) && isSameTarget(other, member) : !(0, refFns_js_1.isRef)(other)));
81
+ const other = acc[index];
82
+ if (other === undefined) {
83
+ return [...acc, member];
84
+ }
85
+ const joined = (0, refFns_js_1.isRef)(other) && (0, refFns_js_1.isRef)(member)
86
+ ? keptRefs.copy(member, keptRefs.copy(other, { ...other, ...member }))
87
+ : merge(other, member);
88
+ return acc.map((each, at) => (at === index ? joined : each));
89
+ }, firstParts.members.filter(member => !(0, isEmpty_js_1.isEmpty)(member)));
90
+ return toRawIntersection({ own: { ...firstParts.own, ...secondParts.own }, members });
91
+ };
92
+ exports.mergeKept = mergeKept;
93
+ const isSameTarget = (first, second) => (0, refFns_js_1.toRefPointer)((0, refFns_js_1.toRefParts)({ $ref: first.$ref })) === (0, refFns_js_1.toRefPointer)((0, refFns_js_1.toRefParts)({ $ref: second.$ref }));
94
+ const toRawIntersection = ({ own, members }) => {
95
+ const [only] = members;
96
+ return (0, isEmpty_js_1.isEmpty)(own) && members.length === 1 && only !== undefined && (0, refFns_js_1.isRef)(only) ? only : { ...own, allOf: members };
97
+ };
98
+ /**
99
+ * The parts of a finished merge, to parse: the keywords that say how the whole
100
+ * value is used move from the one member that is not a ref to the intersection.
101
+ */
102
+ const toSiteIntersection = ({ own, members }) => {
103
+ const moved = members.map(member => ((0, refFns_js_1.isRef)(member) ? { own: {}, rest: member } : toSiteSplit(member)));
104
+ return {
105
+ own: moved.reduce((acc, { own: site }) => ({ ...acc, ...site }), own),
106
+ members: moved.map(({ rest }) => rest).filter(member => !(0, isEmpty_js_1.isEmpty)(member))
107
+ };
108
+ };
109
+ exports.toSiteIntersection = toSiteIntersection;
110
+ const OBJECT_KEYWORDS = ['required', 'minProperties', 'maxProperties', 'additionalProperties'];
111
+ const ARRAY_KEYWORDS = ['minItems', 'maxItems', 'uniqueItems'];
112
+ const STRING_KEYWORDS = ['minLength', 'maxLength', 'pattern'];
113
+ const NUMBER_KEYWORDS = ['minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'multipleOf'];
114
+ /**
115
+ * A member with the keywords of one type and no shape (`{ required: ['id'] }`),
116
+ * as that type, and the same for the members of its `oneOf` or `anyOf`: the
117
+ * type was in the kept ref, and a schema with no type loses them.
118
+ */
119
+ const withKeywordType = (member) => {
120
+ if ((0, refFns_js_1.isRef)(member) || !(0, isPlainObject_js_1.isPlainObject)(member) || ['type', 'properties', 'items', 'enum'].some(key => key in member)) {
121
+ return member;
122
+ }
123
+ if (Array.isArray(member.oneOf) || Array.isArray(member.anyOf)) {
124
+ return {
125
+ ...member,
126
+ ...(Array.isArray(member.oneOf) ? { oneOf: member.oneOf.map(exports.withKeywordType) } : {}),
127
+ ...(Array.isArray(member.anyOf) ? { anyOf: member.anyOf.map(exports.withKeywordType) } : {})
128
+ };
129
+ }
130
+ const hasKeyword = (keywords) => keywords.some(keyword => keyword in member);
131
+ if (hasKeyword(OBJECT_KEYWORDS)) {
132
+ return { ...member, type: 'object' };
133
+ }
134
+ if (hasKeyword(ARRAY_KEYWORDS)) {
135
+ return { ...member, type: 'array', items: {} };
136
+ }
137
+ if (hasKeyword(STRING_KEYWORDS)) {
138
+ return { ...member, type: 'string' };
139
+ }
140
+ return hasKeyword(NUMBER_KEYWORDS) ? { ...member, type: 'number' } : member;
141
+ };
142
+ exports.withKeywordType = withKeywordType;
143
+ /** `schema` with a `null` member of its `oneOf` or `anyOf` written as `nullable`. */
144
+ const withoutNullMember = (schema) => ['oneOf', 'anyOf'].reduce((acc, group) => {
145
+ const members = acc[group];
146
+ if (!Array.isArray(members) || !members.every(isPlainObject_js_1.isPlainObject) || !(0, anyOfSystem_js_1.hasNullMember)(members)) {
147
+ return acc;
148
+ }
149
+ const { [group]: _group, ...rest } = acc;
150
+ const others = members.filter(member => !(0, anyOfSystem_js_1.hasNullMember)([member]));
151
+ return { ...rest, ...(others.length > 0 ? { [group]: others } : {}), nullable: true };
152
+ }, schema);
153
+ const toSiteSplit = (written) => {
154
+ const member = withoutNullMember(written);
155
+ if ((0, nullable_merge_js_1.isNullOnly)(member)) {
156
+ return { own: { nullable: true }, rest: {} };
157
+ }
158
+ // `null` goes with the `nullable` that moves: the member is not nullable without it.
159
+ const isSite = (key) => SITE_KEYWORDS.includes(key) || (key === 'example' && member.example === null && member.nullable === true);
160
+ const { own, rest } = split(member, isSite);
161
+ return member.nullable === true && Array.isArray(rest.enum)
162
+ ? { own, rest: { ...rest, enum: rest.enum.filter((value) => value !== null) } }
163
+ : { own, rest };
164
+ };
@@ -0,0 +1,23 @@
1
+ import type { OpenAPIV3 } from 'openapi-types';
2
+ type SchemaObject = OpenAPIV3.SchemaObject;
3
+ type ReferenceObject = OpenAPIV3.ReferenceObject;
4
+ type SchemaOrReference = SchemaObject | ReferenceObject;
5
+ /** The refs that `system.intersections` keeps out of a merge. */
6
+ export type KeptRefs = {
7
+ /** Whether a merge keeps this `$ref` object as a ref. */
8
+ has: (ref: ReferenceObject) => boolean;
9
+ /** Returns `to`, a `$ref` object made from `from`, kept when `from` is. */
10
+ copy: (from: ReferenceObject, to: ReferenceObject) => ReferenceObject;
11
+ /** Whether merging this schema gives an intersection: it is a kept ref or has one in its `allOf`, also through refs that are not kept. */
12
+ holds: (schema: SchemaOrReference) => boolean;
13
+ /** Whether a pair that holds a kept ref allows no value: the two types, read through refs, have none in common. */
14
+ contradict: (first: SchemaOrReference, second: SchemaOrReference) => boolean;
15
+ };
16
+ type ToKeptRefsArgs = {
17
+ documentObject: OpenAPIV3.Document;
18
+ /** `client.json` `system.intersections`. */
19
+ setting: 'merge' | 'cycles' | 'all' | undefined;
20
+ };
21
+ /** The refs a merge of this document keeps, or `undefined` under `merge`. */
22
+ export declare const toKeptRefs: ({ documentObject, setting }: ToKeptRefsArgs) => KeptRefs | undefined;
23
+ export {};
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toKeptRefs = void 0;
4
+ const refFns_js_1 = require("../helpers/refFns.js");
5
+ const DanglingRefError_js_1 = require("../oas/ref/DanglingRefError.js");
6
+ const isPlainObject_js_1 = require("./isPlainObject.js");
7
+ const toCyclicRefs_js_1 = require("./toCyclicRefs.js");
8
+ const toDeclaredTypes_js_1 = require("./toDeclaredTypes.js");
9
+ /** The refs a merge of this document keeps, or `undefined` under `merge`. */
10
+ const toKeptRefs = ({ documentObject, setting = 'cycles' }) => {
11
+ if (setting === 'merge') {
12
+ return undefined;
13
+ }
14
+ const cyclicRefs = setting === 'cycles' ? (0, toCyclicRefs_js_1.toCyclicRefs)(documentObject) : new WeakSet();
15
+ const has = (ref) => setting === 'all' || cyclicRefs.has(ref);
16
+ const getRef = (0, refFns_js_1.toGetRef)(documentObject);
17
+ // A ref that names nothing is reported where the merge follows it.
18
+ const peek = (ref) => {
19
+ try {
20
+ return getRef(ref);
21
+ }
22
+ catch (error) {
23
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
24
+ return undefined;
25
+ }
26
+ throw error;
27
+ }
28
+ };
29
+ const held = new WeakMap();
30
+ const isHolder = (schema) => {
31
+ if ((0, refFns_js_1.isRef)(schema)) {
32
+ const target = has(schema) ? undefined : peek(schema);
33
+ return has(schema) || (target !== undefined && holds(target));
34
+ }
35
+ return Array.isArray(schema.allOf) && schema.allOf.some(holds);
36
+ };
37
+ const holds = (schema) => {
38
+ // A document in the wild may hold a string where a member belongs.
39
+ if (!(0, isPlainObject_js_1.isPlainObject)(schema)) {
40
+ return false;
41
+ }
42
+ const known = held.get(schema);
43
+ if (known !== undefined) {
44
+ return known;
45
+ }
46
+ const result = isHolder(schema);
47
+ held.set(schema, result);
48
+ return result;
49
+ };
50
+ return {
51
+ has,
52
+ holds,
53
+ copy: (from, to) => {
54
+ if (cyclicRefs.has(from)) {
55
+ cyclicRefs.add(to);
56
+ }
57
+ return to;
58
+ },
59
+ contradict: (first, second) => (holds(first) || holds(second)) &&
60
+ (0, toDeclaredTypes_js_1.haveNoCommonType)((0, toDeclaredTypes_js_1.toDeclaredTypes)({ schema: first, peek }), (0, toDeclaredTypes_js_1.toDeclaredTypes)({ schema: second, peek }))
61
+ };
62
+ };
63
+ exports.toKeptRefs = toKeptRefs;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The `$ref` objects of a raw document that are in a cycle: the ref's target
3
+ * leads back, through any refs, to a schema that holds the ref. A ref inside a
4
+ * deep ref target is held by that target and by the schema around it.
5
+ *
6
+ * The answer depends on the document only, not on where a parse enters it.
7
+ */
8
+ export declare const toCyclicRefs: (document: unknown) => WeakSet<object>;
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toCyclicRefs = void 0;
4
+ const isPlainObject_js_1 = require("./isPlainObject.js");
5
+ const refFns_js_1 = require("../helpers/refFns.js");
6
+ /** Keywords whose value is data, so a `$ref` inside it is not a reference. */
7
+ const DATA_KEYWORDS = ['default', 'example', 'examples', 'enum', 'const'];
8
+ /** Keywords whose value is a map of names to schemas: a name in it is not a keyword. */
9
+ const SCHEMA_MAP_KEYWORDS = ['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas'];
10
+ /** Every `$ref` object anywhere inside a raw schema. */
11
+ const toSchemaRefs = (value) => {
12
+ if (Array.isArray(value)) {
13
+ return value.flatMap(toSchemaRefs);
14
+ }
15
+ if (!(0, isPlainObject_js_1.isPlainObject)(value)) {
16
+ return [];
17
+ }
18
+ const nested = Object.entries(value).flatMap(([key, child]) => {
19
+ if (DATA_KEYWORDS.includes(key) || key.startsWith('x-')) {
20
+ return [];
21
+ }
22
+ return SCHEMA_MAP_KEYWORDS.includes(key) && (0, isPlainObject_js_1.isPlainObject)(child)
23
+ ? Object.values(child).flatMap(toSchemaRefs)
24
+ : toSchemaRefs(child);
25
+ });
26
+ return (0, refFns_js_1.isRef)(value) ? [value, ...nested] : nested;
27
+ };
28
+ /** Every `$ref` object anywhere in a raw document. */
29
+ const toDocumentRefs = (value) => {
30
+ if (Array.isArray(value)) {
31
+ return value.flatMap(toDocumentRefs);
32
+ }
33
+ if (!(0, isPlainObject_js_1.isPlainObject)(value)) {
34
+ return [];
35
+ }
36
+ const nested = Object.values(value).flatMap(toDocumentRefs);
37
+ return (0, refFns_js_1.isRef)(value) ? [value, ...nested] : nested;
38
+ };
39
+ /** Whether a ref can point at a schema: not a remote one, and not one to another kind of component. */
40
+ const canNameSchema = ({ refName, refPath }) => {
41
+ const bucket = (0, refFns_js_1.toRefPathBucket)(refPath);
42
+ return refName !== '' && !(0, refFns_js_1.isRemoteRefPath)(refPath) && (bucket === undefined || bucket === 'schemas');
43
+ };
44
+ /** The document's schema ref graph: each schema component and each other ref target, by `toRefPointer`, with the refs inside it. */
45
+ const toRefGraph = (document) => {
46
+ const components = (0, isPlainObject_js_1.isPlainObject)(document) ? document.components : undefined;
47
+ const schemas = (0, isPlainObject_js_1.isPlainObject)(components) ? components.schemas : undefined;
48
+ const names = (0, isPlainObject_js_1.isPlainObject)(schemas) ? Object.keys(schemas) : [];
49
+ const graph = new Map();
50
+ const pending = [
51
+ ...names.map(name => (0, refFns_js_1.toSchemaComponentRef)(name)),
52
+ ...toDocumentRefs(document).map(({ $ref }) => (0, refFns_js_1.toRefParts)({ $ref }))
53
+ ];
54
+ // `pending` grows while it is walked: each target may hold refs to more targets.
55
+ for (const parts of pending) {
56
+ const key = (0, refFns_js_1.toRefPointer)(parts);
57
+ if (graph.has(key) || !canNameSchema(parts)) {
58
+ continue;
59
+ }
60
+ const edges = toSchemaRefs((0, refFns_js_1.toRawRefTarget)(document, parts)).map(ref => ({
61
+ ref,
62
+ parts: (0, refFns_js_1.toRefParts)({ $ref: ref.$ref })
63
+ }));
64
+ graph.set(key, edges.map(edge => ({ ref: edge.ref, target: (0, refFns_js_1.toRefPointer)(edge.parts) })));
65
+ edges.forEach(edge => pending.push(edge.parts));
66
+ }
67
+ return graph;
68
+ };
69
+ /**
70
+ * The strongly connected set of each node, as a number (Tarjan). Iterative, so
71
+ * a long chain of refs does not run out of stack.
72
+ */
73
+ const toConnectedSets = (graph) => {
74
+ const visits = new Map();
75
+ const open = [];
76
+ const sets = new Map();
77
+ const enter = (node) => {
78
+ visits.set(node, { index: visits.size, low: visits.size });
79
+ open.push(node);
80
+ return { node, edges: graph.get(node) ?? [], next: 0 };
81
+ };
82
+ for (const root of graph.keys()) {
83
+ if (visits.has(root)) {
84
+ continue;
85
+ }
86
+ const frames = [enter(root)];
87
+ for (let frame = frames.at(-1); frame !== undefined; frame = frames.at(-1)) {
88
+ const visit = visits.get(frame.node);
89
+ const edge = frame.edges[frame.next];
90
+ if (visit === undefined) {
91
+ break;
92
+ }
93
+ if (edge !== undefined) {
94
+ frame.next += 1;
95
+ const seen = visits.get(edge.target);
96
+ if (seen === undefined) {
97
+ frames.push(enter(edge.target));
98
+ }
99
+ else if (!sets.has(edge.target)) {
100
+ visit.low = Math.min(visit.low, seen.index);
101
+ }
102
+ continue;
103
+ }
104
+ frames.pop();
105
+ const caller = frames.at(-1);
106
+ const parent = caller && visits.get(caller.node);
107
+ if (parent) {
108
+ parent.low = Math.min(parent.low, visit.low);
109
+ }
110
+ if (visit.low === visit.index) {
111
+ const id = visit.index;
112
+ open.splice(open.lastIndexOf(frame.node)).forEach(node => sets.set(node, id));
113
+ }
114
+ }
115
+ }
116
+ return sets;
117
+ };
118
+ /**
119
+ * The `$ref` objects of a raw document that are in a cycle: the ref's target
120
+ * leads back, through any refs, to a schema that holds the ref. A ref inside a
121
+ * deep ref target is held by that target and by the schema around it.
122
+ *
123
+ * The answer depends on the document only, not on where a parse enters it.
124
+ */
125
+ const toCyclicRefs = (document) => {
126
+ const graph = toRefGraph(document);
127
+ const sets = toConnectedSets(graph);
128
+ return new WeakSet([...graph].flatMap(([holder, edges]) => edges.filter(({ target }) => sets.get(target) === sets.get(holder)).map(({ ref }) => ref)));
129
+ };
130
+ exports.toCyclicRefs = toCyclicRefs;
@@ -0,0 +1,20 @@
1
+ import type { OpenAPIV3 } from 'openapi-types';
2
+ type SchemaOrReference = OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject;
3
+ /** The types a schema allows, or `any`. */
4
+ export type DeclaredTypes = string[] | 'any';
5
+ type ToDeclaredTypesArgs = {
6
+ schema: SchemaOrReference;
7
+ /** The schema a ref names, or `undefined` when it names nothing. */
8
+ peek: (ref: OpenAPIV3.ReferenceObject) => OpenAPIV3.SchemaObject | undefined;
9
+ /** The types of each ref target met so far: `any` for one that is being read. */
10
+ known?: Map<string, DeclaredTypes>;
11
+ };
12
+ /**
13
+ * The types a schema declares, itself or through its refs and its `allOf`,
14
+ * `oneOf` and `anyOf` members. A member that leads back to a schema that is
15
+ * being read counts as any type, so the types are never too few.
16
+ */
17
+ export declare const toDeclaredTypes: ({ schema, peek, known }: ToDeclaredTypesArgs) => DeclaredTypes;
18
+ /** Whether two schemas' types have none in common. */
19
+ export declare const haveNoCommonType: (first: DeclaredTypes, second: DeclaredTypes) => boolean;
20
+ export {};
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.haveNoCommonType = exports.toDeclaredTypes = void 0;
4
+ const refFns_js_1 = require("../helpers/refFns.js");
5
+ /**
6
+ * The types a schema declares, itself or through its refs and its `allOf`,
7
+ * `oneOf` and `anyOf` members. A member that leads back to a schema that is
8
+ * being read counts as any type, so the types are never too few.
9
+ */
10
+ const toDeclaredTypes = ({ schema, peek, known = new Map() }) => {
11
+ if ((0, refFns_js_1.isRef)(schema)) {
12
+ const pointer = (0, refFns_js_1.toRefPointer)((0, refFns_js_1.toRefParts)({ $ref: schema.$ref }));
13
+ const found = known.get(pointer);
14
+ if (found !== undefined) {
15
+ return found;
16
+ }
17
+ known.set(pointer, 'any');
18
+ const target = peek(schema);
19
+ const types = target === undefined ? 'any' : (0, exports.toDeclaredTypes)({ schema: target, peek, known });
20
+ known.set(pointer, types);
21
+ return types;
22
+ }
23
+ const toMemberTypes = (members) => (Array.isArray(members) ? members : [])
24
+ .filter((member) => typeof member === 'object' && member !== null)
25
+ .map(member => (0, exports.toDeclaredTypes)({ schema: member, peek, known }));
26
+ const constraints = [
27
+ toOwnTypes(schema.type),
28
+ ...toMemberTypes(schema.allOf),
29
+ toUnionTypes(toMemberTypes(schema.oneOf)),
30
+ toUnionTypes(toMemberTypes(schema.anyOf))
31
+ ].filter(isTypeList);
32
+ const [first, ...rest] = constraints;
33
+ return first === undefined ? 'any' : rest.reduce(toCommonTypes, first);
34
+ };
35
+ exports.toDeclaredTypes = toDeclaredTypes;
36
+ /** Whether two schemas' types have none in common. */
37
+ const haveNoCommonType = (first, second) => isTypeList(first) && isTypeList(second) && toCommonTypes(first, second).length === 0;
38
+ exports.haveNoCommonType = haveNoCommonType;
39
+ /** A `type` as a list without `null`, which 3.1 writes in one. Every integer is a number. */
40
+ const toOwnTypes = (type) => {
41
+ const types = (Array.isArray(type) ? type : [type])
42
+ .filter(item => typeof item === 'string' && item !== 'null')
43
+ .flatMap(item => (item === 'number' ? ['number', 'integer'] : [item]));
44
+ return types.length > 0 ? types : 'any';
45
+ };
46
+ const toUnionTypes = (members) => members.length > 0 && members.every(isTypeList) ? [...new Set(members.flat())] : 'any';
47
+ const isTypeList = (types) => Array.isArray(types);
48
+ const toCommonTypes = (first, second) => first.filter(type => second.includes(type));
@@ -3,18 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergeIntersection = void 0;
4
4
  const decompose_intersection_js_1 = require("./decompose-intersection.js");
5
5
  const merge_js_1 = require("./merge.js");
6
+ const isEmpty_js_1 = require("../../../helpers/isEmpty.js");
7
+ const keptIntersection_js_1 = require("../../keptIntersection.js");
6
8
  const mergeIntersection = ({ schema, getRef, system }) => {
7
- const decomposed = (0, decompose_intersection_js_1.decomposeIntersection)({ schema });
8
- if (decomposed.length === 1) {
9
+ // Where a ref stays a ref, the wrapper's own keywords stay beside the `allOf`.
10
+ const { own, rest } = system.keptRefs?.holds(schema) ? (0, keptIntersection_js_1.splitOwnKeywords)(schema) : { own: {}, rest: schema };
11
+ const decomposed = (0, decompose_intersection_js_1.decomposeIntersection)({ schema: rest });
12
+ if (decomposed.length === 1 && (0, isEmpty_js_1.isEmpty)(own)) {
9
13
  return decomposed[0];
10
14
  }
11
15
  const dereffed = decomposed.map(decomposed => {
12
- return '$ref' in decomposed ? getRef(decomposed) : decomposed;
16
+ return '$ref' in decomposed && !system.keptRefs?.has(decomposed) ? getRef(decomposed) : decomposed;
13
17
  });
14
18
  const result = dereffed.reduce((acc, decomposed) => {
15
19
  const merged = (0, merge_js_1.mergeSchemasOrRefs)({ first: acc, second: decomposed, getRef, system });
16
20
  return merged;
17
21
  }, {});
18
- return result;
22
+ return (0, keptIntersection_js_1.withOwnKeywords)(result, own);
19
23
  };
20
24
  exports.mergeIntersection = mergeIntersection;
@@ -90,7 +90,13 @@ const mergeCrossProduct = ({ first, second, getRef, system, groupType }) => {
90
90
  const secondGroup = Array.isArray(second[groupType]) ? second[groupType] : [second];
91
91
  const mergedGroup = mergePairs({
92
92
  pairs: (0, cross_product_js_1.crossProduct)(firstGroup, secondGroup),
93
- merge: ([firstItem, secondItem], pairSystem) => (0, merge_js_1.mergeSchemasOrRefs)({ first: firstItem, second: secondItem, getRef, system: pairSystem }),
93
+ merge: ([firstItem, secondItem], pairSystem) => {
94
+ // A kept ref is not read into the merge, so its type is compared here.
95
+ if (pairSystem.allOf === 'strict' && system.keptRefs?.contradict(firstItem, secondItem)) {
96
+ throw new Error('Cannot merge schemas: a member has another type than the reference');
97
+ }
98
+ return (0, merge_js_1.mergeSchemasOrRefs)({ first: firstItem, second: secondItem, getRef, system: pairSystem });
99
+ },
94
100
  system
95
101
  });
96
102
  return {
@@ -20,8 +20,21 @@ const merge_intersection_js_1 = require("./merge-intersection.js");
20
20
  const merge_union_js_1 = require("./merge-union.js");
21
21
  const isEmpty_js_1 = require("../../../helpers/isEmpty.js");
22
22
  const nullable_merge_js_1 = require("./nullable-merge.js");
23
+ const keptIntersection_js_1 = require("../../keptIntersection.js");
23
24
  const anyOfSystem_js_1 = require("../../anyOfSystem.js");
24
25
  const mergeSchemasOrRefs = ({ first, second, getRef, system }) => {
26
+ const { keptRefs } = system;
27
+ if (keptRefs && ((0, keptIntersection_js_1.toIntersectionParts)(first, keptRefs) || (0, keptIntersection_js_1.toIntersectionParts)(second, keptRefs))) {
28
+ return (0, keptIntersection_js_1.mergeKept)({
29
+ first,
30
+ second,
31
+ keptRefs,
32
+ merge: (a, b) => (0, exports.mergeSchemasOrRefs)({ first: a, second: b, getRef, system }),
33
+ mergeAllOf: schema => (0, merge_intersection_js_1.mergeIntersection)({ schema, getRef, system: (0, merge_union_js_1.toOwnSystem)(system) }),
34
+ readRef: getRef,
35
+ normalize: schema => schema
36
+ });
37
+ }
25
38
  if (containsRef(first, second)) {
26
39
  return mergeWithRef({ first, second, getRef, system });
27
40
  }
@@ -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
  * The `system` settings as the merge reads them (`client.json#settings.system`),
@@ -22,6 +23,8 @@ export type MergeSystem = {
22
23
  anyOf?: 'union' | 'unknown';
23
24
  /** The keywords the merge read as any value: `anyOf` under `system.anyOf: unknown`. */
24
25
  unknown?: Set<string>;
26
+ /** The refs that stay refs (`system.intersections`). Left out: every ref is followed. */
27
+ keptRefs?: KeptRefs;
25
28
  };
26
29
  export type OneOfObject = {
27
30
  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;