@skmtc/core 0.40.0 → 0.41.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 +9 -0
- package/esm/context/ParseContext.js +90 -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 +6 -2
- package/esm/helpers/refFns.js +24 -7
- package/esm/mod.d.ts +1 -0
- package/esm/mod.js +1 -0
- package/esm/oas/operation/Operation.d.ts +4 -1
- package/esm/oas/operation/Operation.js +6 -5
- package/esm/oas/operation/toEffectiveParameters.d.ts +8 -0
- package/esm/oas/operation/toEffectiveParameters.js +32 -3
- 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/oas/webhook/Webhook.js +2 -4
- package/esm/parse/securityRequirements.d.ts +6 -0
- package/esm/parse/securityRequirements.js +8 -0
- 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-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-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/types/Settings.d.ts +10 -0
- package/esm/types/Settings.js +2 -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 +9 -0
- package/script/context/ParseContext.js +90 -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 +6 -2
- package/script/helpers/refFns.js +24 -7
- package/script/mod.d.ts +1 -0
- package/script/mod.js +1 -0
- package/script/oas/operation/Operation.d.ts +4 -1
- package/script/oas/operation/Operation.js +5 -4
- package/script/oas/operation/toEffectiveParameters.d.ts +8 -0
- package/script/oas/operation/toEffectiveParameters.js +34 -4
- 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/oas/webhook/Webhook.js +1 -3
- package/script/parse/securityRequirements.d.ts +6 -0
- package/script/parse/securityRequirements.js +8 -0
- 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-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-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/types/Settings.d.ts +10 -0
- package/script/types/Settings.js +2 -1
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 under `client.json` `system.danglingRefs: keep`),
|
|
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 under `system.danglingRefs: keep`).
|
|
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 under `system.danglingRefs: keep`).
|
|
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 {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import "../../_dnt.polyfills.js";
|
|
2
2
|
import { toPrimaryResponseCode } from '../response/toPrimaryResponseCode.js';
|
|
3
3
|
import { OasBase } from '../../types/OasBase.js';
|
|
4
|
-
import { toEffectiveParameters } from '../operation/toEffectiveParameters.js';
|
|
4
|
+
import { toEffectiveParameters, toResolvedParameters } from '../operation/toEffectiveParameters.js';
|
|
5
5
|
/**
|
|
6
6
|
* Represents an OpenAPI 3.1 webhook — a third OAS subject, peer to
|
|
7
7
|
* {@link OasOperation} and the schema/model subjects.
|
|
@@ -173,9 +173,7 @@ export class OasWebhook extends OasBase {
|
|
|
173
173
|
* parameters carry the same shape, only their direction is inbound.
|
|
174
174
|
*/
|
|
175
175
|
toParams(filter) {
|
|
176
|
-
return toEffectiveParameters({ own: this.parameters, shared: this.pathItem?.parameters })
|
|
177
|
-
.map(param => param.resolve())
|
|
178
|
-
.filter(param => (filter?.length ? filter.includes(param.location) : true));
|
|
176
|
+
return toResolvedParameters(toEffectiveParameters({ own: this.parameters, shared: this.pathItem?.parameters })).filter(param => (filter?.length ? filter.includes(param.location) : true));
|
|
179
177
|
}
|
|
180
178
|
/**
|
|
181
179
|
* The status code of the primary acknowledgement response — the lowest 2xx
|
|
@@ -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({
|
|
@@ -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
|
};
|
|
@@ -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
|
};
|
|
@@ -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
|
};
|
package/esm/types/Settings.d.ts
CHANGED
|
@@ -449,6 +449,16 @@ export type SystemSettings = {
|
|
|
449
449
|
* keywords and without the `anyOf`, with one warning for each schema.
|
|
450
450
|
*/
|
|
451
451
|
anyOf?: 'union' | 'unknown';
|
|
452
|
+
/**
|
|
453
|
+
* `strict` (the default): a `$ref` that names nothing in the document is a
|
|
454
|
+
* parse error, and what holds it is removed, with what uses that. `keep`:
|
|
455
|
+
* the `$ref` stays, with a warning, and resolving it throws
|
|
456
|
+
* `DanglingRefError`, which a generator can catch to render something
|
|
457
|
+
* else. With either, a `$ref` to an example or a link is a warning and
|
|
458
|
+
* stays, and a `$ref` that core resolves while it parses (an `allOf`
|
|
459
|
+
* member that it merges) is an error, and what holds it is removed.
|
|
460
|
+
*/
|
|
461
|
+
danglingRefs?: 'strict' | 'keep';
|
|
452
462
|
};
|
|
453
463
|
export type ClientSettings = {
|
|
454
464
|
/** Base output path for generated files */
|
package/esm/types/Settings.js
CHANGED
|
@@ -174,7 +174,8 @@ export const systemSettings = v.object({
|
|
|
174
174
|
allOf: v.optional(v.picklist(['strict', 'latestWins'])),
|
|
175
175
|
responseContradictions: v.optional(v.picklist(['strict', 'unknown'])),
|
|
176
176
|
not: v.optional(v.picklist(['strict', 'unknown'])),
|
|
177
|
-
anyOf: v.optional(v.picklist(['union', 'unknown']))
|
|
177
|
+
anyOf: v.optional(v.picklist(['union', 'unknown'])),
|
|
178
|
+
danglingRefs: v.optional(v.picklist(['strict', 'keep']))
|
|
178
179
|
});
|
|
179
180
|
export const clientSettings = v.object({
|
|
180
181
|
basePath: v.optional(v.pipe(v.string(), v.check(basePath => !isAbsolutePath(basePath), 'basePath must be relative to the SKMTC root (the directory containing .skmtc/); ' +
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skmtc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.0",
|
|
4
4
|
"description": "Skmtc is a declarative code generation framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openapi",
|
|
@@ -35,6 +35,10 @@
|
|
|
35
35
|
"import": "./esm/dsl/CustomValue.js",
|
|
36
36
|
"require": "./script/dsl/CustomValue.js"
|
|
37
37
|
},
|
|
38
|
+
"./DanglingRefError": {
|
|
39
|
+
"import": "./esm/oas/ref/DanglingRefError.js",
|
|
40
|
+
"require": "./script/oas/ref/DanglingRefError.js"
|
|
41
|
+
},
|
|
38
42
|
"./DenoJson": {
|
|
39
43
|
"import": "./esm/types/DenoJson.js",
|
|
40
44
|
"require": "./script/types/DenoJson.js"
|
|
@@ -328,7 +328,7 @@ export declare class GenerateContext implements GenerateContextType {
|
|
|
328
328
|
* Perform one lookup of schema by `refName`.
|
|
329
329
|
* @param refName
|
|
330
330
|
* @returns Matching schema or ref
|
|
331
|
-
* @throws
|
|
331
|
+
* @throws {DanglingRefError} when no schema has that name
|
|
332
332
|
*/
|
|
333
333
|
resolveSchemaRefOnce(refName: RefName): OasSchema | OasRef<'schema'>;
|
|
334
334
|
/**
|
|
@@ -18,6 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
exports.toModelSource = exports.toGqlOperationSource = exports.toWebhookSource = exports.toOasOperationSource = exports.GenerateContext = void 0;
|
|
19
19
|
const normalizeWorkspacePath_js_1 = require("../helpers/normalizeWorkspacePath.js");
|
|
20
20
|
const applyGeneratedSuffix_js_1 = require("../helpers/applyGeneratedSuffix.js");
|
|
21
|
+
const DanglingRefError_js_1 = require("../oas/ref/DanglingRefError.js");
|
|
21
22
|
const Method_js_1 = require("../types/Method.js");
|
|
22
23
|
const OasOperationDriver_js_1 = require("../dsl/operation/oas/OasOperationDriver.js");
|
|
23
24
|
const WebhookDriver_js_1 = require("../dsl/webhook/WebhookDriver.js");
|
|
@@ -836,14 +837,14 @@ class GenerateContext {
|
|
|
836
837
|
* Perform one lookup of schema by `refName`.
|
|
837
838
|
* @param refName
|
|
838
839
|
* @returns Matching schema or ref
|
|
839
|
-
* @throws
|
|
840
|
+
* @throws {DanglingRefError} when no schema has that name
|
|
840
841
|
*/
|
|
841
842
|
resolveSchemaRefOnce(refName) {
|
|
842
843
|
switch (this.document.type) {
|
|
843
844
|
case 'oas': {
|
|
844
845
|
const component = this.document.value.components?.schemas?.[refName];
|
|
845
846
|
if (!component) {
|
|
846
|
-
throw new
|
|
847
|
+
throw new DanglingRefError_js_1.DanglingRefError(refName);
|
|
847
848
|
}
|
|
848
849
|
const target = component.resolveOnce();
|
|
849
850
|
// One step, with what the component adds next to its reference
|
|
@@ -856,7 +857,7 @@ class GenerateContext {
|
|
|
856
857
|
case 'gql': {
|
|
857
858
|
const schema = this.document.value.registry.schemas[refName];
|
|
858
859
|
if (!schema) {
|
|
859
|
-
throw new
|
|
860
|
+
throw new DanglingRefError_js_1.DanglingRefError(refName);
|
|
860
861
|
}
|
|
861
862
|
// GqlRegistry stores OasSchema | OasRef entries: one step, as for OAS.
|
|
862
863
|
return schema.resolveOnce();
|
|
@@ -28,6 +28,8 @@ import { StackTrail } from './StackTrail.js';
|
|
|
28
28
|
import type { SkmtcDocumentInput, SkmtcParsedDocument } from '../types/SkmtcDocument.js';
|
|
29
29
|
import type { GqlParseOptions, LogAtArgs, LogIssueArgs, LogIssueAtArgs, LogIssueNoKeyArgs, LogSkippedValuesArgs } from './parseTypes.js';
|
|
30
30
|
import type { ParseIssue } from './ParseIssue.js';
|
|
31
|
+
import type { OasRef } from '../oas/ref/Ref.js';
|
|
32
|
+
import type { OasRefData } from '../oas/ref/ref-types.js';
|
|
31
33
|
export type { ParseIssue, GqlIssueType } from './ParseIssue.js';
|
|
32
34
|
export type { GqlParseOptions, LogAtArgs, LogIssueArgs, LogIssueAtArgs, LogIssueNoKeyArgs, LogSkippedValuesArgs, ParseContextType, ParseErrorInput, ParseIssueInput, ParseWarningInput } from './parseTypes.js';
|
|
33
35
|
type OasProtocolState = {
|
|
@@ -157,6 +159,13 @@ export declare class ParseContext {
|
|
|
157
159
|
* would use type names or qualified field paths.
|
|
158
160
|
*/
|
|
159
161
|
registerRef(consumer: StackTrail, refKey: string): void;
|
|
162
|
+
/**
|
|
163
|
+
* Register an OAS `$ref` at its consumer site, as {@link registerRef}
|
|
164
|
+
* does, under the key {@link toRefKey} gives its `$ref`, and keep the ref
|
|
165
|
+
* with that site, so that once the document is parsed `#checkRefs` can
|
|
166
|
+
* resolve it there.
|
|
167
|
+
*/
|
|
168
|
+
registerOasRef<T extends OasRefData['refType']>(consumer: StackTrail, ref: OasRef<T>): void;
|
|
160
169
|
/**
|
|
161
170
|
* Register an error against a ref key — typically called when the
|
|
162
171
|
* target of a `$ref` (or type reference) fails to parse. `undefined`
|
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_rootFrames, _ParseContext_removeComponent, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_toRawComponentParameter, _ParseContext_logDependencyIssue, _ParseContext_toDocumentTrail;
|
|
13
|
+
var _ParseContext_instances, _ParseContext_refConsumers, _ParseContext_refErrors, _ParseContext_oasRefs, _ParseContext_rootFrames, _ParseContext_removeComponent, _ParseContext_removeConsumer, _ParseContext_removePathParameterConsumer, _ParseContext_toRawComponentParameter, _ParseContext_logDependencyIssue, _ParseContext_toDocumentTrail, _ParseContext_checkRefs, _ParseContext_logDanglingRef;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.ParseContext = void 0;
|
|
16
16
|
/**
|
|
@@ -44,6 +44,20 @@ const StackTrail_js_1 = require("./StackTrail.js");
|
|
|
44
44
|
const isPlainObject_js_1 = require("../parse/isPlainObject.js");
|
|
45
45
|
const pathParameters_js_1 = require("../parse/pathParameters.js");
|
|
46
46
|
const SerializedError_js_1 = require("../types/SerializedError.js");
|
|
47
|
+
const DanglingRefError_js_1 = require("../oas/ref/DanglingRefError.js");
|
|
48
|
+
const refFns_js_1 = require("../helpers/refFns.js");
|
|
49
|
+
/**
|
|
50
|
+
* The key a `$ref`'s consumers and errors are kept under. A
|
|
51
|
+
* `#/components/<bucket>/…` ref is keyed by the name it resolves (`toRefName`
|
|
52
|
+
* unescapes it), which is the key `StackTrail.toStackRef()` gives that
|
|
53
|
+
* component's own errors: `#/components/schemas/Foo%20Bar` is kept under
|
|
54
|
+
* `#/components/schemas/Foo Bar`. Any other `$ref` is its own key.
|
|
55
|
+
*/
|
|
56
|
+
const toRefKey = ($ref) => {
|
|
57
|
+
// The last segment is not empty, so `toRefName` does not throw.
|
|
58
|
+
const bucket = /^#\/components\/([^/]+)\/.*[^/]$/.exec($ref)?.[1];
|
|
59
|
+
return bucket === undefined ? $ref : `#/components/${bucket}/${(0, refFns_js_1.toRefName)($ref)}`;
|
|
60
|
+
};
|
|
47
61
|
/** The stored form of an issue: an error's `cause` as a `SerializedError`. */
|
|
48
62
|
const toParseIssue = (issue) => {
|
|
49
63
|
if (issue.level !== 'error') {
|
|
@@ -116,6 +130,10 @@ class ParseContext {
|
|
|
116
130
|
// use type names. The maps don't care about the encoding.
|
|
117
131
|
_ParseContext_refConsumers.set(this, new Map());
|
|
118
132
|
_ParseContext_refErrors.set(this, new Map()
|
|
133
|
+
/** Each OAS ref and the place it is written, which `#checkRefs` resolves once the document is parsed. */
|
|
134
|
+
);
|
|
135
|
+
/** Each OAS ref and the place it is written, which `#checkRefs` resolves once the document is parsed. */
|
|
136
|
+
_ParseContext_oasRefs.set(this, []
|
|
119
137
|
/**
|
|
120
138
|
* Frames of the trail `parse()` was called with (`[traceId, spanId,
|
|
121
139
|
* 'parse']` through `toArtifacts`). Every trail built during the walk
|
|
@@ -265,6 +283,7 @@ class ParseContext {
|
|
|
265
283
|
throw new Error(`Unhandled OAS dialect: ${JSON.stringify(_exhaustive)}`);
|
|
266
284
|
}
|
|
267
285
|
}
|
|
286
|
+
__classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_checkRefs).call(this);
|
|
268
287
|
this.removeErroredItems();
|
|
269
288
|
return { type: 'oas', value: oasState.oasDocument };
|
|
270
289
|
}
|
|
@@ -364,6 +383,16 @@ class ParseContext {
|
|
|
364
383
|
__classPrivateFieldGet(this, _ParseContext_refConsumers, "f").set(refKey, [consumer]);
|
|
365
384
|
}
|
|
366
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* Register an OAS `$ref` at its consumer site, as {@link registerRef}
|
|
388
|
+
* does, under the key {@link toRefKey} gives its `$ref`, and keep the ref
|
|
389
|
+
* with that site, so that once the document is parsed `#checkRefs` can
|
|
390
|
+
* resolve it there.
|
|
391
|
+
*/
|
|
392
|
+
registerOasRef(consumer, ref) {
|
|
393
|
+
this.registerRef(consumer, toRefKey(ref.$ref));
|
|
394
|
+
__classPrivateFieldGet(this, _ParseContext_oasRefs, "f").push({ consumer, ref });
|
|
395
|
+
}
|
|
367
396
|
/**
|
|
368
397
|
* Register an error against a ref key — typically called when the
|
|
369
398
|
* target of a `$ref` (or type reference) fails to parse. `undefined`
|
|
@@ -516,7 +545,7 @@ class ParseContext {
|
|
|
516
545
|
}
|
|
517
546
|
}
|
|
518
547
|
exports.ParseContext = ParseContext;
|
|
519
|
-
_ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMap(), _ParseContext_rootFrames = new WeakMap(), _ParseContext_instances = new WeakSet(), _ParseContext_removeComponent = function _ParseContext_removeComponent(refKey) {
|
|
548
|
+
_ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMap(), _ParseContext_oasRefs = new WeakMap(), _ParseContext_rootFrames = new WeakMap(), _ParseContext_instances = new WeakSet(), _ParseContext_removeComponent = function _ParseContext_removeComponent(refKey) {
|
|
520
549
|
if (this.protocol.type !== 'oas') {
|
|
521
550
|
return;
|
|
522
551
|
}
|
|
@@ -629,4 +658,63 @@ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMa
|
|
|
629
658
|
frames.length >= rootFrames.length &&
|
|
630
659
|
rootFrames.every((frame, index) => frames[index] === frame);
|
|
631
660
|
return startsWithRoot ? new StackTrail_js_1.StackTrail(frames.slice(rootFrames.length)) : stackTrail;
|
|
661
|
+
}, _ParseContext_checkRefs = function _ParseContext_checkRefs() {
|
|
662
|
+
__classPrivateFieldGet(this, _ParseContext_oasRefs, "f").forEach(({ consumer, ref }) => {
|
|
663
|
+
if (__classPrivateFieldGet(this, _ParseContext_refErrors, "f").has(toRefKey(ref.$ref))) {
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
try {
|
|
667
|
+
ref.resolveOnce();
|
|
668
|
+
}
|
|
669
|
+
catch (error) {
|
|
670
|
+
if (DanglingRefError_js_1.DanglingRefError.is(error)) {
|
|
671
|
+
__classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_logDanglingRef).call(this, { consumer, ref, error });
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
this.logIssueNoKey({
|
|
675
|
+
level: 'warning',
|
|
676
|
+
message: `${error instanceof Error ? error.message : String(error)}. It is kept, and resolving it throws.`,
|
|
677
|
+
parent: { $ref: ref.$ref },
|
|
678
|
+
stackTrail: consumer,
|
|
679
|
+
type: 'REF_TYPE_MISMATCH'
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
}, _ParseContext_logDanglingRef = function _ParseContext_logDanglingRef({ consumer, ref, error }) {
|
|
685
|
+
const exampleOrLink = ref.refType === 'example' || ref.refType === 'link';
|
|
686
|
+
if (exampleOrLink || this.system.danglingRefs === 'keep') {
|
|
687
|
+
this.logIssueNoKey({
|
|
688
|
+
level: 'warning',
|
|
689
|
+
message: exampleOrLink
|
|
690
|
+
? `${error.message}. It is kept, as a ref to an example or a link is in both system.danglingRefs modes, and resolving it throws DanglingRefError.`
|
|
691
|
+
: `${error.message}. It is kept (system.danglingRefs is keep), and resolving it throws DanglingRefError.`,
|
|
692
|
+
parent: { $ref: ref.$ref },
|
|
693
|
+
stackTrail: consumer,
|
|
694
|
+
type: 'DANGLING_REF'
|
|
695
|
+
});
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
// An error inside a component removes that component, and what uses it
|
|
699
|
+
// (`removeErroredItems`).
|
|
700
|
+
this.logIssueNoKey({
|
|
701
|
+
level: 'error',
|
|
702
|
+
message: `${error.message}, so what holds it is removed (system.danglingRefs is strict).`,
|
|
703
|
+
cause: error,
|
|
704
|
+
parent: { $ref: ref.$ref },
|
|
705
|
+
stackTrail: consumer,
|
|
706
|
+
type: 'DANGLING_REF'
|
|
707
|
+
});
|
|
708
|
+
// What holds the ref outside the components (an operation, a webhook, a
|
|
709
|
+
// path-level parameter) is removed here. Its own location has the
|
|
710
|
+
// DANGLING_REF already; an operation that a path-level parameter applies
|
|
711
|
+
// to gets an INVALID_DEPENDENCY_REF.
|
|
712
|
+
const documentTrail = __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_toDocumentTrail).call(this, consumer);
|
|
713
|
+
if (documentTrail.toStackRef() !== undefined) {
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
const location = consumer.toString();
|
|
717
|
+
__classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_removeConsumer).call(this, consumer, documentTrail)
|
|
718
|
+
.filter(removed => removed !== location)
|
|
719
|
+
.forEach(removed => __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_logDependencyIssue).call(this, { location: removed, refKey: ref.$ref, rootCause: { error, location } }));
|
|
632
720
|
};
|