@skmtc/core 0.40.0 → 0.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/context/GenerateContext.d.ts +1 -1
- package/esm/context/GenerateContext.js +4 -3
- package/esm/context/ParseContext.d.ts +26 -0
- package/esm/context/ParseContext.js +112 -2
- package/esm/context/generateTypes.d.ts +16 -2
- package/esm/context/generateTypes.js +2 -0
- package/esm/context/tryParseAt.d.ts +4 -1
- package/esm/context/tryParseAt.js +10 -3
- package/esm/helpers/collateExamples.js +15 -5
- package/esm/helpers/refFns.d.ts +9 -3
- package/esm/helpers/refFns.js +29 -9
- package/esm/mod.d.ts +1 -0
- package/esm/mod.js +1 -0
- package/esm/oas/ref/DanglingRefError.d.ts +22 -0
- package/esm/oas/ref/DanglingRefError.js +33 -0
- package/esm/oas/ref/Ref.d.ts +5 -0
- package/esm/oas/ref/Ref.js +28 -2
- package/esm/oas/schema/siblings.js +12 -2
- package/esm/oas/union/Union.d.ts +2 -1
- package/esm/oas/union/Union.js +23 -12
- package/esm/parse/securityRequirements.d.ts +6 -0
- package/esm/parse/securityRequirements.js +8 -0
- package/esm/parse/v3-0/_merge-all-of/merge-union.js +9 -1
- package/esm/parse/v3-0/mediaType/toMediaTypeItemV3.js +4 -3
- package/esm/parse/v3-0/parameter/toParameterV3.js +4 -3
- package/esm/parse/v3-0/ref/toRefV31.js +5 -2
- package/esm/parse/v3-0/schema/toSchemasV3.js +6 -3
- package/esm/parse/v3-1/_merge-all-of/merge-union.js +16 -4
- package/esm/parse/v3-1/mediaType/toMediaTypeItemV3.js +4 -3
- package/esm/parse/v3-1/parameter/toParameterV3.js +4 -3
- package/esm/parse/v3-1/ref/toRefV31.js +5 -2
- package/esm/parse/v3-1/schema/toSchemasV3.js +6 -3
- package/esm/parse/v3-2/_merge-all-of/merge-union.js +16 -4
- package/esm/parse/v3-2/mediaType/toMediaTypeItemV3.js +4 -3
- package/esm/parse/v3-2/parameter/toParameterV3.js +4 -3
- package/esm/parse/v3-2/ref/toRefV31.js +5 -2
- package/esm/parse/v3-2/schema/toSchemasV3.js +6 -3
- package/esm/types/Settings.d.ts +21 -0
- package/esm/types/Settings.js +19 -1
- package/package.json +5 -1
- package/script/context/GenerateContext.d.ts +1 -1
- package/script/context/GenerateContext.js +4 -3
- package/script/context/ParseContext.d.ts +26 -0
- package/script/context/ParseContext.js +112 -2
- package/script/context/generateTypes.d.ts +16 -2
- package/script/context/generateTypes.js +2 -0
- package/script/context/tryParseAt.d.ts +4 -1
- package/script/context/tryParseAt.js +10 -3
- package/script/helpers/collateExamples.js +15 -5
- package/script/helpers/refFns.d.ts +9 -3
- package/script/helpers/refFns.js +29 -9
- package/script/mod.d.ts +1 -0
- package/script/mod.js +1 -0
- package/script/oas/ref/DanglingRefError.d.ts +22 -0
- package/script/oas/ref/DanglingRefError.js +37 -0
- package/script/oas/ref/Ref.d.ts +5 -0
- package/script/oas/ref/Ref.js +28 -2
- package/script/oas/schema/siblings.js +12 -2
- package/script/oas/union/Union.d.ts +2 -1
- package/script/oas/union/Union.js +23 -12
- package/script/parse/securityRequirements.d.ts +6 -0
- package/script/parse/securityRequirements.js +8 -0
- package/script/parse/v3-0/_merge-all-of/merge-union.js +9 -1
- package/script/parse/v3-0/mediaType/toMediaTypeItemV3.js +4 -3
- package/script/parse/v3-0/parameter/toParameterV3.js +4 -3
- package/script/parse/v3-0/ref/toRefV31.js +5 -2
- package/script/parse/v3-0/schema/toSchemasV3.js +6 -3
- package/script/parse/v3-1/_merge-all-of/merge-union.js +16 -4
- package/script/parse/v3-1/mediaType/toMediaTypeItemV3.js +4 -3
- package/script/parse/v3-1/parameter/toParameterV3.js +4 -3
- package/script/parse/v3-1/ref/toRefV31.js +5 -2
- package/script/parse/v3-1/schema/toSchemasV3.js +6 -3
- package/script/parse/v3-2/_merge-all-of/merge-union.js +16 -4
- package/script/parse/v3-2/mediaType/toMediaTypeItemV3.js +4 -3
- package/script/parse/v3-2/parameter/toParameterV3.js +4 -3
- package/script/parse/v3-2/ref/toRefV31.js +5 -2
- package/script/parse/v3-2/schema/toSchemasV3.js +6 -3
- package/script/types/Settings.d.ts +21 -0
- package/script/types/Settings.js +20 -2
package/esm/oas/ref/Ref.js
CHANGED
|
@@ -14,6 +14,7 @@ import "../../_dnt.polyfills.js";
|
|
|
14
14
|
import { traverseSchema } from '../schemaPath/traverseSchema.js';
|
|
15
15
|
import { toRefName } from '../../helpers/refFns.js';
|
|
16
16
|
import { OasBase } from '../../types/OasBase.js';
|
|
17
|
+
import { DanglingRefError } from './DanglingRefError.js';
|
|
17
18
|
import { changesTarget, changesValues, hasSiblingKeywords, mergeSiblingKeywords, toSkippedSiblings } from '../schema/siblings.js';
|
|
18
19
|
const MAX_LOOKUPS = 10;
|
|
19
20
|
/**
|
|
@@ -43,6 +44,18 @@ function refTypeToPluralPath(refType) {
|
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* The name `resolveOnce` looks up. `toRefName` throws for a `$ref` whose last
|
|
49
|
+
* segment is empty (`#/components/schemas/`): that `$ref` names nothing.
|
|
50
|
+
*/
|
|
51
|
+
const toLookupName = ($ref) => {
|
|
52
|
+
try {
|
|
53
|
+
return toRefName($ref);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
throw new DanglingRefError($ref);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
46
59
|
/**
|
|
47
60
|
* Represents an OpenAPI reference ($ref) in the SKMTC OAS processing system.
|
|
48
61
|
*
|
|
@@ -445,14 +458,19 @@ export class OasRef extends OasBase {
|
|
|
445
458
|
* reads from `document.registry.schemas`.
|
|
446
459
|
*
|
|
447
460
|
* @returns Either the resolved component or another reference in the chain
|
|
461
|
+
* @throws {DanglingRefError} when the reference names nothing in the
|
|
462
|
+
* document (kept when `client.json` `system.danglingRefs` lists its kind),
|
|
463
|
+
* including a `$ref` with no name, such as `#/components/schemas/`
|
|
464
|
+
* @throws Error when it names a component of another kind, such as a
|
|
465
|
+
* `requestBody` ref to `#/components/schemas/…`
|
|
448
466
|
*/
|
|
449
467
|
resolveOnce() {
|
|
450
|
-
const refName =
|
|
468
|
+
const refName = toLookupName(this.$ref);
|
|
451
469
|
const resolved = __classPrivateFieldGet(this, _OasRef_document, "f").type === 'oas'
|
|
452
470
|
? __classPrivateFieldGet(this, _OasRef_instances, "m", _OasRef_resolveOasOnce).call(this, __classPrivateFieldGet(this, _OasRef_document, "f").value, refName)
|
|
453
471
|
: __classPrivateFieldGet(this, _OasRef_instances, "m", _OasRef_resolveGqlOnce).call(this, __classPrivateFieldGet(this, _OasRef_document, "f").value, refName);
|
|
454
472
|
if (!resolved) {
|
|
455
|
-
throw new
|
|
473
|
+
throw new DanglingRefError(this.$ref);
|
|
456
474
|
}
|
|
457
475
|
if (resolved.isRef()) {
|
|
458
476
|
if (resolved.refType !== this.refType) {
|
|
@@ -644,6 +662,14 @@ _OasRef_document = new WeakMap(), _OasRef_resolution = new WeakMap(), _OasRef_in
|
|
|
644
662
|
// OasSchema to ResolvedRef<T> for a generic T, as in resolveOnce().
|
|
645
663
|
return changesTarget(copy, component, this) ? copy : target;
|
|
646
664
|
}, _OasRef_resolveOasOnce = function _OasRef_resolveOasOnce(document, refName) {
|
|
665
|
+
// A `#/components/<bucket>/<name>` ref names its bucket. A request body
|
|
666
|
+
// that is a `$ref` to `#/components/schemas/Pet` names a schema, so it is
|
|
667
|
+
// never looked up in the request bodies.
|
|
668
|
+
const bucket = /^#\/components\/([^/]+)\//.exec(this.$ref)?.[1];
|
|
669
|
+
const expected = refTypeToPluralPath(this.refType);
|
|
670
|
+
if (bucket !== undefined && bucket !== expected) {
|
|
671
|
+
throw new Error(`Ref type mismatch for "${this.$ref}". Expected a ref into "components/${expected}" but it names one in "components/${bucket}"`);
|
|
672
|
+
}
|
|
647
673
|
const c = document.components;
|
|
648
674
|
const refType = this.refType;
|
|
649
675
|
switch (refType) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as v from 'valibot';
|
|
2
2
|
import isEqual from 'lodash-es/isEqual.js';
|
|
3
|
+
import { DanglingRefError } from '../ref/DanglingRefError.js';
|
|
3
4
|
import { numberFormat } from '../number/number-types.js';
|
|
4
5
|
import { integerFormat } from '../integer/integer-types.js';
|
|
5
6
|
/**
|
|
@@ -183,8 +184,17 @@ const MAX_DEPTH = 10;
|
|
|
183
184
|
*/
|
|
184
185
|
const agrees = (type, target, depth) => {
|
|
185
186
|
if (target.type === 'union') {
|
|
186
|
-
|
|
187
|
-
|
|
187
|
+
try {
|
|
188
|
+
return depth < MAX_DEPTH &&
|
|
189
|
+
target.members.every(member => agrees(type, member.isRef() ? member.resolve() : member, depth + 1));
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
// A member that is a dangling reference has no known type, so it does not agree.
|
|
193
|
+
if (DanglingRefError.is(error)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
188
198
|
}
|
|
189
199
|
return type === target.type || (type === 'number' && target.type === 'integer');
|
|
190
200
|
};
|
package/esm/oas/union/Union.d.ts
CHANGED
|
@@ -216,7 +216,8 @@ export declare class OasUnion extends OasBase {
|
|
|
216
216
|
* an object that is not nullable, that requires the discriminator's
|
|
217
217
|
* property, and that allows only string literals for it. No value may
|
|
218
218
|
* belong to two members. A member that is a union, or anything other than
|
|
219
|
-
* an object, makes it `false
|
|
219
|
+
* an object, makes it `false`, as does a reference on the way that names
|
|
220
|
+
* nothing in the document (kept when `system.danglingRefs` lists `schema`).
|
|
220
221
|
*
|
|
221
222
|
* It reads the schemas only. A generator may refuse more: one that leaves
|
|
222
223
|
* out `readOnly` properties cannot discriminate on one.
|
package/esm/oas/union/Union.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { traverseSchema } from '../schemaPath/traverseSchema.js';
|
|
2
2
|
import { toSiblingAnnotations } from '../schema/siblings.js';
|
|
3
3
|
import { OasBase } from '../../types/OasBase.js';
|
|
4
|
+
import { DanglingRefError } from '../ref/DanglingRefError.js';
|
|
4
5
|
/**
|
|
5
6
|
* Represents a union type schema in the OpenAPI Specification.
|
|
6
7
|
*
|
|
@@ -283,7 +284,8 @@ export class OasUnion extends OasBase {
|
|
|
283
284
|
* an object that is not nullable, that requires the discriminator's
|
|
284
285
|
* property, and that allows only string literals for it. No value may
|
|
285
286
|
* belong to two members. A member that is a union, or anything other than
|
|
286
|
-
* an object, makes it `false
|
|
287
|
+
* an object, makes it `false`, as does a reference on the way that names
|
|
288
|
+
* nothing in the document (kept when `system.danglingRefs` lists `schema`).
|
|
287
289
|
*
|
|
288
290
|
* It reads the schemas only. A generator may refuse more: one that leaves
|
|
289
291
|
* out `readOnly` properties cannot discriminate on one.
|
|
@@ -294,17 +296,26 @@ export class OasUnion extends OasBase {
|
|
|
294
296
|
return false;
|
|
295
297
|
}
|
|
296
298
|
const seen = new Set();
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
values
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
299
|
+
try {
|
|
300
|
+
return this.members.every(member => {
|
|
301
|
+
const values = toDiscriminatorValues(member, key);
|
|
302
|
+
return (values !== undefined &&
|
|
303
|
+
values.every(value => {
|
|
304
|
+
if (seen.has(value)) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
seen.add(value);
|
|
308
|
+
return true;
|
|
309
|
+
}));
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
// A dangling reference: the schema it stands for is not known.
|
|
314
|
+
if (DanglingRefError.is(error)) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
throw error;
|
|
318
|
+
}
|
|
308
319
|
}
|
|
309
320
|
toJsonSchema(options) {
|
|
310
321
|
return {
|
|
@@ -58,6 +58,12 @@ export type ToOperationsWithValidSecurityArgs<Item extends SecuredItem> = {
|
|
|
58
58
|
* failed to parse or is not defined, or that has no `security` and
|
|
59
59
|
* inherits an unusable root. Each dropped item logs one `INVALID_OPERATION`
|
|
60
60
|
* error on its own trail. `security: []` inherits nothing and is kept.
|
|
61
|
+
*
|
|
62
|
+
* A kept item depends on each scheme its security names (its own, else the
|
|
63
|
+
* root's), as it would on a `$ref` to it, so it is registered as a consumer
|
|
64
|
+
* of the scheme. When the parser removes the scheme later (one that is a
|
|
65
|
+
* `$ref` that names nothing), the item goes with it, and so is never kept
|
|
66
|
+
* without its auth.
|
|
61
67
|
*/
|
|
62
68
|
export declare const toOperationsWithValidSecurity: <Item extends SecuredItem>({ items, check }: ToOperationsWithValidSecurityArgs<Item>) => Item[];
|
|
63
69
|
export {};
|
|
@@ -107,6 +107,12 @@ export const toSecurityCheck = ({ parse, declared, parsed, stackTrail, context }
|
|
|
107
107
|
* failed to parse or is not defined, or that has no `security` and
|
|
108
108
|
* inherits an unusable root. Each dropped item logs one `INVALID_OPERATION`
|
|
109
109
|
* error on its own trail. `security: []` inherits nothing and is kept.
|
|
110
|
+
*
|
|
111
|
+
* A kept item depends on each scheme its security names (its own, else the
|
|
112
|
+
* root's), as it would on a `$ref` to it, so it is registered as a consumer
|
|
113
|
+
* of the scheme. When the parser removes the scheme later (one that is a
|
|
114
|
+
* `$ref` that names nothing), the item goes with it, and so is never kept
|
|
115
|
+
* without its auth.
|
|
110
116
|
*/
|
|
111
117
|
export const toOperationsWithValidSecurity = ({ items, check }) => {
|
|
112
118
|
return items.filter(item => {
|
|
@@ -114,6 +120,8 @@ export const toOperationsWithValidSecurity = ({ items, check }) => {
|
|
|
114
120
|
? toSchemeProblems(item.security, check).map(capitalize).join('. ') || undefined
|
|
115
121
|
: check.rootProblem;
|
|
116
122
|
if (message === undefined) {
|
|
123
|
+
const names = (item.security ?? check.rootSecurity ?? []).flatMap(({ requirement }) => Object.keys(requirement));
|
|
124
|
+
new Set(names).forEach(name => check.context.registerRef(item.stackTrail.clone(), `#/components/securitySchemes/${name}`));
|
|
117
125
|
return true;
|
|
118
126
|
}
|
|
119
127
|
check.context.logIssueNoKey({
|
|
@@ -2,6 +2,7 @@ import { decomposeUnion } from './decompose-union.js';
|
|
|
2
2
|
import { mergeSchemasOrRefs } from './merge.js';
|
|
3
3
|
import { crossProduct } from './cross-product.js';
|
|
4
4
|
import { isRef } from '../../../helpers/refFns.js';
|
|
5
|
+
import { DanglingRefError } from '../../../oas/ref/DanglingRefError.js';
|
|
5
6
|
export const mergeUnion = ({ schema, getRef, system, groupType }) => {
|
|
6
7
|
const { beforeExcluded, decomposed, afterExcluded } = decomposeUnion({ schema, groupType });
|
|
7
8
|
// if (decomposed.length === 1) {
|
|
@@ -49,13 +50,20 @@ export const toOwnSystem = (system) => ({
|
|
|
49
50
|
* ({@link toOwnSystem}). Inside an outer first pass under `latestWins`, a
|
|
50
51
|
* cross product with no pair that merges fails its pair: the outer pass then
|
|
51
52
|
* leaves the pair out, or lets the later member win.
|
|
53
|
+
*
|
|
54
|
+
* A `$ref` that names nothing is not a contradiction, so its pair is not left
|
|
55
|
+
* out for it: the `DanglingRefError` is thrown on, and `system.danglingRefs`
|
|
56
|
+
* decides (`allOfMember`).
|
|
52
57
|
*/
|
|
53
58
|
const mergePairs = ({ pairs, merge, system }) => {
|
|
54
59
|
const mergeEach = (pairSystem) => pairs.flatMap(pair => {
|
|
55
60
|
try {
|
|
56
61
|
return [merge(pair, pairSystem)];
|
|
57
62
|
}
|
|
58
|
-
catch (
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (DanglingRefError.is(error)) {
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
59
67
|
return [];
|
|
60
68
|
}
|
|
61
69
|
});
|
|
@@ -117,13 +117,14 @@ export const toMediaTypeItemV3 = ({ mediaTypeItem, mediaType, stackTrail, contex
|
|
|
117
117
|
mediaType,
|
|
118
118
|
encoding,
|
|
119
119
|
schema: stackTrail.trace('schema', st => toOptionalSchemaV3({ schema, stackTrail: st, context })),
|
|
120
|
-
examples
|
|
120
|
+
// toExamplesV3 traces `examples` (or `example`) itself.
|
|
121
|
+
examples: toExamplesV3({
|
|
121
122
|
example,
|
|
122
123
|
examples,
|
|
123
124
|
exampleKey: mediaType,
|
|
124
|
-
stackTrail
|
|
125
|
+
stackTrail,
|
|
125
126
|
context
|
|
126
|
-
})
|
|
127
|
+
}),
|
|
127
128
|
extensionFields
|
|
128
129
|
};
|
|
129
130
|
return context.withStackTrail(stackTrail, () => new OasMediaType(fields, context));
|
|
@@ -101,13 +101,14 @@ const toParameterV3 = ({ parameter, stackTrail, context }) => {
|
|
|
101
101
|
allowEmptyValue,
|
|
102
102
|
allowReserved,
|
|
103
103
|
schema: stackTrail.trace('schema', st => toOptionalSchemaV3({ schema, stackTrail: st, context })),
|
|
104
|
-
examples
|
|
104
|
+
// toExamplesV3 traces `examples` (or `example`) itself.
|
|
105
|
+
examples: toExamplesV3({
|
|
105
106
|
examples,
|
|
106
107
|
example,
|
|
107
108
|
exampleKey: `${name}-${parsedLocation}`,
|
|
108
|
-
stackTrail
|
|
109
|
+
stackTrail,
|
|
109
110
|
context
|
|
110
|
-
})
|
|
111
|
+
}),
|
|
111
112
|
content: stackTrail.trace('content', st => toOptionalMediaTypeItemsV3({ content, stackTrail: st, context })),
|
|
112
113
|
extensionFields
|
|
113
114
|
};
|
|
@@ -5,6 +5,9 @@ export const toRefV31 = ({ ref, refType, stackTrail, context, nullable, readOnly
|
|
|
5
5
|
if (!isEmpty(skipped)) {
|
|
6
6
|
context.logSkippedFields({ skipped, parent: ref, parentType: 'ref', stackTrail });
|
|
7
7
|
}
|
|
8
|
-
context.
|
|
9
|
-
|
|
8
|
+
const oasRef = context.withStackTrail(stackTrail, () => new OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
|
|
9
|
+
// The parse context checks each ref once the document is parsed, and
|
|
10
|
+
// removes what holds a ref that names nothing (`system.danglingRefs`).
|
|
11
|
+
context.registerOasRef(stackTrail.clone(), oasRef);
|
|
12
|
+
return oasRef;
|
|
10
13
|
};
|
|
@@ -177,7 +177,7 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
177
177
|
const system = toAllOfSystem(context);
|
|
178
178
|
const merged = mergeIntersection({
|
|
179
179
|
schema,
|
|
180
|
-
getRef: toGetRef(context.documentObject),
|
|
180
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
181
181
|
system
|
|
182
182
|
});
|
|
183
183
|
logAllOfContradictions({ system, stackTrail, parent: schema, context });
|
|
@@ -191,7 +191,9 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
191
191
|
const merged = mergeUnion({
|
|
192
192
|
// A null member makes the union nullable: `oneOf: [$ref, null]` is a nullable reference.
|
|
193
193
|
schema: withoutNullMembers(schema, 'oneOf'),
|
|
194
|
-
|
|
194
|
+
// A member's own `allOf`, or a member merged with the keywords beside
|
|
195
|
+
// the `oneOf`, is merged here, so it follows `allOfMember` too.
|
|
196
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
195
197
|
system,
|
|
196
198
|
groupType: 'oneOf'
|
|
197
199
|
});
|
|
@@ -259,7 +261,8 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
259
261
|
const system = toAllOfSystem(context);
|
|
260
262
|
const merged = mergeUnion({
|
|
261
263
|
schema: withoutNullMembers(schema, 'anyOf'),
|
|
262
|
-
|
|
264
|
+
// As for `oneOf`: what is merged here follows `allOfMember`.
|
|
265
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
263
266
|
system,
|
|
264
267
|
groupType: 'anyOf'
|
|
265
268
|
});
|
|
@@ -2,6 +2,7 @@ import { decomposeUnion, isUnionLevelKey } from './decompose-union.js';
|
|
|
2
2
|
import { mergeSchemasOrRefs } from './merge.js';
|
|
3
3
|
import { crossProduct } from './cross-product.js';
|
|
4
4
|
import { isRef } from '../../../helpers/refFns.js';
|
|
5
|
+
import { DanglingRefError } from '../../../oas/ref/DanglingRefError.js';
|
|
5
6
|
export const mergeUnion = ({ schema, getRef, normalize, system, groupType }) => {
|
|
6
7
|
const { beforeExcluded, decomposed, afterExcluded } = decomposeUnion({ schema, groupType });
|
|
7
8
|
// if (decomposed.length === 1) {
|
|
@@ -56,13 +57,20 @@ export const toOwnSystem = (system) => ({
|
|
|
56
57
|
* ({@link toOwnSystem}). Inside an outer first pass under `latestWins`, a
|
|
57
58
|
* cross product with no pair that merges fails its pair: the outer pass then
|
|
58
59
|
* leaves the pair out, or lets the later member win.
|
|
60
|
+
*
|
|
61
|
+
* A `$ref` that names nothing is not a contradiction, so its pair is not left
|
|
62
|
+
* out for it: the `DanglingRefError` is thrown on, and `system.danglingRefs`
|
|
63
|
+
* decides (`allOfMember`).
|
|
59
64
|
*/
|
|
60
65
|
const mergePairs = ({ pairs, merge, system }) => {
|
|
61
66
|
const mergeEach = (pairSystem) => pairs.flatMap(pair => {
|
|
62
67
|
try {
|
|
63
68
|
return [merge(pair, pairSystem)];
|
|
64
69
|
}
|
|
65
|
-
catch (
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (DanglingRefError.is(error)) {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
66
74
|
return [];
|
|
67
75
|
}
|
|
68
76
|
});
|
|
@@ -114,8 +122,9 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, system, gr
|
|
|
114
122
|
* to a `oneOf` with `minLength` beside it. Union-level keys (`nullable`,
|
|
115
123
|
* `description`, …) are not merged in, and a `$ref` member is returned
|
|
116
124
|
* unchanged. A member that conflicts with the
|
|
117
|
-
* siblings is dropped, as in the cross product
|
|
118
|
-
*
|
|
125
|
+
* siblings is dropped, as in the cross product, but not one with a `$ref`
|
|
126
|
+
* that names nothing: that is no conflict. A schema with no group is its own
|
|
127
|
+
* only member and is returned unchanged.
|
|
119
128
|
*/
|
|
120
129
|
const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupType }) => {
|
|
121
130
|
if (!Array.isArray(schema[groupType])) {
|
|
@@ -135,7 +144,10 @@ const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupTy
|
|
|
135
144
|
try {
|
|
136
145
|
return mergeSchemasOrRefs({ first: siblingSchema, second: member, getRef, normalize, system });
|
|
137
146
|
}
|
|
138
|
-
catch (
|
|
147
|
+
catch (error) {
|
|
148
|
+
if (DanglingRefError.is(error)) {
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
139
151
|
return undefined;
|
|
140
152
|
}
|
|
141
153
|
})
|
|
@@ -117,13 +117,14 @@ export const toMediaTypeItemV3 = ({ mediaTypeItem, mediaType, stackTrail, contex
|
|
|
117
117
|
mediaType,
|
|
118
118
|
encoding,
|
|
119
119
|
schema: stackTrail.trace('schema', st => toOptionalSchemaV3({ schema, stackTrail: st, context })),
|
|
120
|
-
examples
|
|
120
|
+
// toExamplesV3 traces `examples` (or `example`) itself.
|
|
121
|
+
examples: toExamplesV3({
|
|
121
122
|
example,
|
|
122
123
|
examples,
|
|
123
124
|
exampleKey: mediaType,
|
|
124
|
-
stackTrail
|
|
125
|
+
stackTrail,
|
|
125
126
|
context
|
|
126
|
-
})
|
|
127
|
+
}),
|
|
127
128
|
extensionFields
|
|
128
129
|
};
|
|
129
130
|
return context.withStackTrail(stackTrail, () => new OasMediaType(fields, context));
|
|
@@ -101,13 +101,14 @@ const toParameterV3 = ({ parameter, stackTrail, context }) => {
|
|
|
101
101
|
allowEmptyValue,
|
|
102
102
|
allowReserved,
|
|
103
103
|
schema: stackTrail.trace('schema', st => toOptionalSchemaV3({ schema, stackTrail: st, context })),
|
|
104
|
-
examples
|
|
104
|
+
// toExamplesV3 traces `examples` (or `example`) itself.
|
|
105
|
+
examples: toExamplesV3({
|
|
105
106
|
examples,
|
|
106
107
|
example,
|
|
107
108
|
exampleKey: `${name}-${parsedLocation}`,
|
|
108
|
-
stackTrail
|
|
109
|
+
stackTrail,
|
|
109
110
|
context
|
|
110
|
-
})
|
|
111
|
+
}),
|
|
111
112
|
content: stackTrail.trace('content', st => toOptionalMediaTypeItemsV3({ content, stackTrail: st, context })),
|
|
112
113
|
extensionFields
|
|
113
114
|
};
|
|
@@ -5,6 +5,9 @@ export const toRefV31 = ({ ref, refType, stackTrail, context, nullable, readOnly
|
|
|
5
5
|
if (!isEmpty(skipped)) {
|
|
6
6
|
context.logSkippedFields({ skipped, parent: ref, parentType: 'ref', stackTrail });
|
|
7
7
|
}
|
|
8
|
-
context.
|
|
9
|
-
|
|
8
|
+
const oasRef = context.withStackTrail(stackTrail, () => new OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
|
|
9
|
+
// The parse context checks each ref once the document is parsed, and
|
|
10
|
+
// removes what holds a ref that names nothing (`system.danglingRefs`).
|
|
11
|
+
context.registerOasRef(stackTrail.clone(), oasRef);
|
|
12
|
+
return oasRef;
|
|
10
13
|
};
|
|
@@ -229,7 +229,7 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
229
229
|
const system = toAllOfSystem(context);
|
|
230
230
|
const merged = mergeIntersection({
|
|
231
231
|
schema: wrapper,
|
|
232
|
-
getRef: toGetRef(context.documentObject),
|
|
232
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
233
233
|
normalize: normalizeMergeMember,
|
|
234
234
|
system
|
|
235
235
|
});
|
|
@@ -261,7 +261,9 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
261
261
|
const system = toAllOfSystem(context);
|
|
262
262
|
const merged = mergeUnion({
|
|
263
263
|
schema: withoutNull,
|
|
264
|
-
|
|
264
|
+
// A member's own `allOf`, or a member merged with the keywords beside
|
|
265
|
+
// the `oneOf`, is merged here, so it follows `allOfMember` too.
|
|
266
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
265
267
|
normalize: normalizeMergeMember,
|
|
266
268
|
system,
|
|
267
269
|
groupType: 'oneOf'
|
|
@@ -342,7 +344,8 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
342
344
|
const system = toAllOfSystem(context);
|
|
343
345
|
const merged = mergeUnion({
|
|
344
346
|
schema: withoutNull,
|
|
345
|
-
|
|
347
|
+
// As for `oneOf`: what is merged here follows `allOfMember`.
|
|
348
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
346
349
|
normalize: normalizeMergeMember,
|
|
347
350
|
system,
|
|
348
351
|
groupType: 'anyOf'
|
|
@@ -2,6 +2,7 @@ import { decomposeUnion, isUnionLevelKey } from './decompose-union.js';
|
|
|
2
2
|
import { mergeSchemasOrRefs } from './merge.js';
|
|
3
3
|
import { crossProduct } from './cross-product.js';
|
|
4
4
|
import { isRef } from '../../../helpers/refFns.js';
|
|
5
|
+
import { DanglingRefError } from '../../../oas/ref/DanglingRefError.js';
|
|
5
6
|
export const mergeUnion = ({ schema, getRef, normalize, system, groupType }) => {
|
|
6
7
|
const { beforeExcluded, decomposed, afterExcluded } = decomposeUnion({ schema, groupType });
|
|
7
8
|
// if (decomposed.length === 1) {
|
|
@@ -56,13 +57,20 @@ export const toOwnSystem = (system) => ({
|
|
|
56
57
|
* ({@link toOwnSystem}). Inside an outer first pass under `latestWins`, a
|
|
57
58
|
* cross product with no pair that merges fails its pair: the outer pass then
|
|
58
59
|
* leaves the pair out, or lets the later member win.
|
|
60
|
+
*
|
|
61
|
+
* A `$ref` that names nothing is not a contradiction, so its pair is not left
|
|
62
|
+
* out for it: the `DanglingRefError` is thrown on, and `system.danglingRefs`
|
|
63
|
+
* decides (`allOfMember`).
|
|
59
64
|
*/
|
|
60
65
|
const mergePairs = ({ pairs, merge, system }) => {
|
|
61
66
|
const mergeEach = (pairSystem) => pairs.flatMap(pair => {
|
|
62
67
|
try {
|
|
63
68
|
return [merge(pair, pairSystem)];
|
|
64
69
|
}
|
|
65
|
-
catch (
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (DanglingRefError.is(error)) {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
66
74
|
return [];
|
|
67
75
|
}
|
|
68
76
|
});
|
|
@@ -114,8 +122,9 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, system, gr
|
|
|
114
122
|
* to a `oneOf` with `minLength` beside it. Union-level keys (`nullable`,
|
|
115
123
|
* `description`, …) are not merged in, and a `$ref` member is returned
|
|
116
124
|
* unchanged. A member that conflicts with the
|
|
117
|
-
* siblings is dropped, as in the cross product
|
|
118
|
-
*
|
|
125
|
+
* siblings is dropped, as in the cross product, but not one with a `$ref`
|
|
126
|
+
* that names nothing: that is no conflict. A schema with no group is its own
|
|
127
|
+
* only member and is returned unchanged.
|
|
119
128
|
*/
|
|
120
129
|
const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupType }) => {
|
|
121
130
|
if (!Array.isArray(schema[groupType])) {
|
|
@@ -135,7 +144,10 @@ const withGroupSiblings = ({ schema, members, getRef, normalize, system, groupTy
|
|
|
135
144
|
try {
|
|
136
145
|
return mergeSchemasOrRefs({ first: siblingSchema, second: member, getRef, normalize, system });
|
|
137
146
|
}
|
|
138
|
-
catch (
|
|
147
|
+
catch (error) {
|
|
148
|
+
if (DanglingRefError.is(error)) {
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
139
151
|
return undefined;
|
|
140
152
|
}
|
|
141
153
|
})
|
|
@@ -132,13 +132,14 @@ export const toMediaTypeItemV3 = ({ mediaTypeItem, mediaType, stackTrail, contex
|
|
|
132
132
|
itemEncoding,
|
|
133
133
|
schema: stackTrail.trace('schema', st => toOptionalSchemaV3({ schema, stackTrail: st, context })),
|
|
134
134
|
itemSchema: stackTrail.trace('itemSchema', st => toOptionalSchemaV3({ schema: itemSchema, stackTrail: st, context })),
|
|
135
|
-
examples
|
|
135
|
+
// toExamplesV3 traces `examples` (or `example`) itself.
|
|
136
|
+
examples: toExamplesV3({
|
|
136
137
|
example,
|
|
137
138
|
examples,
|
|
138
139
|
exampleKey: mediaType,
|
|
139
|
-
stackTrail
|
|
140
|
+
stackTrail,
|
|
140
141
|
context
|
|
141
|
-
})
|
|
142
|
+
}),
|
|
142
143
|
extensionFields
|
|
143
144
|
};
|
|
144
145
|
return context.withStackTrail(stackTrail, () => new OasMediaType(fields, context));
|
|
@@ -118,13 +118,14 @@ const toParameterV3 = ({ parameter, stackTrail, context }) => {
|
|
|
118
118
|
allowEmptyValue,
|
|
119
119
|
allowReserved,
|
|
120
120
|
schema: stackTrail.trace('schema', st => toOptionalSchemaV3({ schema, stackTrail: st, context })),
|
|
121
|
-
examples
|
|
121
|
+
// toExamplesV3 traces `examples` (or `example`) itself.
|
|
122
|
+
examples: toExamplesV3({
|
|
122
123
|
examples,
|
|
123
124
|
example,
|
|
124
125
|
exampleKey: `${name}-${parsedLocation}`,
|
|
125
|
-
stackTrail
|
|
126
|
+
stackTrail,
|
|
126
127
|
context
|
|
127
|
-
})
|
|
128
|
+
}),
|
|
128
129
|
content: stackTrail.trace('content', st => toOptionalMediaTypeItemsV3({ content, stackTrail: st, context })),
|
|
129
130
|
extensionFields
|
|
130
131
|
};
|
|
@@ -5,6 +5,9 @@ export const toRefV31 = ({ ref, refType, stackTrail, context, nullable, readOnly
|
|
|
5
5
|
if (!isEmpty(skipped)) {
|
|
6
6
|
context.logSkippedFields({ skipped, parent: ref, parentType: 'ref', stackTrail });
|
|
7
7
|
}
|
|
8
|
-
context.
|
|
9
|
-
|
|
8
|
+
const oasRef = context.withStackTrail(stackTrail, () => new OasRef({ ...siblings, refType, $ref, nullable, readOnly, writeOnly }, context));
|
|
9
|
+
// The parse context checks each ref once the document is parsed, and
|
|
10
|
+
// removes what holds a ref that names nothing (`system.danglingRefs`).
|
|
11
|
+
context.registerOasRef(stackTrail.clone(), oasRef);
|
|
12
|
+
return oasRef;
|
|
10
13
|
};
|
|
@@ -229,7 +229,7 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
229
229
|
const system = toAllOfSystem(context);
|
|
230
230
|
const merged = mergeIntersection({
|
|
231
231
|
schema: wrapper,
|
|
232
|
-
getRef: toGetRef(context.documentObject),
|
|
232
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
233
233
|
normalize: normalizeMergeMember,
|
|
234
234
|
system
|
|
235
235
|
});
|
|
@@ -261,7 +261,9 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
261
261
|
const system = toAllOfSystem(context);
|
|
262
262
|
const merged = mergeUnion({
|
|
263
263
|
schema: withoutNull,
|
|
264
|
-
|
|
264
|
+
// A member's own `allOf`, or a member merged with the keywords beside
|
|
265
|
+
// the `oneOf`, is merged here, so it follows `allOfMember` too.
|
|
266
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
265
267
|
normalize: normalizeMergeMember,
|
|
266
268
|
system,
|
|
267
269
|
groupType: 'oneOf'
|
|
@@ -342,7 +344,8 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
342
344
|
const system = toAllOfSystem(context);
|
|
343
345
|
const merged = mergeUnion({
|
|
344
346
|
schema: withoutNull,
|
|
345
|
-
|
|
347
|
+
// As for `oneOf`: what is merged here follows `allOfMember`.
|
|
348
|
+
getRef: toGetRef(context.documentObject, $ref => context.toDanglingAllOfMember({ $ref, stackTrail })),
|
|
346
349
|
normalize: normalizeMergeMember,
|
|
347
350
|
system,
|
|
348
351
|
groupType: 'anyOf'
|
package/esm/types/Settings.d.ts
CHANGED
|
@@ -162,6 +162,15 @@ export declare const includeModels: v.GenericSchema<IncludeModels>;
|
|
|
162
162
|
* {@link AnchorsSettings} for the consumer-facing fields.
|
|
163
163
|
*/
|
|
164
164
|
export declare const anchorsSettings: v.GenericSchema<AnchorsSettings>;
|
|
165
|
+
/**
|
|
166
|
+
* What `client.json#settings.system.danglingRefs` can list: each kind of
|
|
167
|
+
* `$ref`, and `allOfMember`, a `$ref` that core merges at parse: a member of
|
|
168
|
+
* an `allOf`, also one inside a `oneOf` or `anyOf` member, or a `oneOf` or
|
|
169
|
+
* `anyOf` member merged with the keywords beside the union.
|
|
170
|
+
*/
|
|
171
|
+
export declare const danglingRefKinds: readonly ["schema", "parameter", "requestBody", "response", "header", "securityScheme", "example", "link", "allOfMember"];
|
|
172
|
+
/** A kind of `$ref` that `system.danglingRefs` can let name nothing. */
|
|
173
|
+
export type DanglingRefKind = (typeof danglingRefKinds)[number];
|
|
165
174
|
/** Valibot schema for {@link SystemSettings}: `client.json#settings.system`. */
|
|
166
175
|
export declare const systemSettings: v.GenericSchema<SystemSettings>;
|
|
167
176
|
export declare const clientSettings: v.GenericSchema<ClientSettings>;
|
|
@@ -449,6 +458,18 @@ export type SystemSettings = {
|
|
|
449
458
|
* keywords and without the `anyOf`, with one warning for each schema.
|
|
450
459
|
*/
|
|
451
460
|
anyOf?: 'union' | 'unknown';
|
|
461
|
+
/**
|
|
462
|
+
* The kinds of `$ref` that may name nothing in the document. A dangling
|
|
463
|
+
* `$ref` of a listed kind stays, with a warning, and resolving it throws
|
|
464
|
+
* `DanglingRefError` for the generator to handle. A dangling `$ref` of any
|
|
465
|
+
* other kind is a parse error, and what holds it is removed, with what uses
|
|
466
|
+
* that. `allOfMember` is core's own choice, made at parse: a `$ref` that
|
|
467
|
+
* core merges (a member of an `allOf`, also inside a `oneOf` or `anyOf`
|
|
468
|
+
* member, or a union member merged with the keywords beside the union) is
|
|
469
|
+
* left out of the merge, so its properties are not in the model. The
|
|
470
|
+
* default, `[]`, lists none.
|
|
471
|
+
*/
|
|
472
|
+
danglingRefs?: DanglingRefKind[];
|
|
452
473
|
};
|
|
453
474
|
export type ClientSettings = {
|
|
454
475
|
/** Base output path for generated files */
|