@skmtc/core 0.45.0 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/anchors/attribute.js +3 -2
- package/esm/context/CoreContext.d.ts +1 -1
- package/esm/context/CoreContext.js +1 -1
- package/esm/context/GenerateContext.d.ts +13 -10
- package/esm/context/GenerateContext.js +36 -25
- package/esm/context/ParseContext.d.ts +3 -0
- package/esm/context/ParseContext.js +151 -39
- package/esm/context/generateTypes.d.ts +7 -6
- package/esm/dsl/GeneratorKeys.d.ts +21 -15
- package/esm/dsl/GeneratorKeys.js +16 -11
- package/esm/dsl/Inserted.d.ts +1 -1
- package/esm/dsl/Inserted.js +1 -1
- package/esm/dsl/model/ModelDriver.d.ts +6 -5
- package/esm/dsl/model/ModelDriver.js +22 -20
- package/esm/dsl/model/toModelEntry.d.ts +18 -18
- package/esm/dsl/model/toModelEntry.js +9 -9
- package/esm/dsl/model/toModelProjectionBase.d.ts +17 -16
- package/esm/dsl/model/toModelProjectionBase.js +12 -8
- package/esm/dsl/model/types.d.ts +26 -17
- package/esm/dsl/operation/gql/toGqlOperationProjectionBase.d.ts +2 -2
- package/esm/dsl/operation/gql/toGqlOperationProjectionBase.js +2 -2
- package/esm/dsl/operation/oas/toOasOperationProjectionBase.d.ts +2 -2
- package/esm/dsl/operation/oas/toOasOperationProjectionBase.js +2 -2
- package/esm/dsl/webhook/toWebhookProjectionBase.d.ts +2 -2
- package/esm/dsl/webhook/toWebhookProjectionBase.js +2 -2
- package/esm/helpers/collateExamples.js +35 -17
- package/esm/helpers/refFns.d.ts +108 -43
- package/esm/helpers/refFns.js +166 -57
- package/esm/mod.d.ts +1 -0
- package/esm/mod.js +1 -0
- package/esm/oas/document/Document.d.ts +6 -0
- package/esm/oas/document/Document.js +44 -2
- package/esm/oas/intersection/Intersection.d.ts +122 -0
- package/esm/oas/intersection/Intersection.js +266 -0
- package/esm/oas/mediaType/jsonMediaType.d.ts +1 -1
- package/esm/oas/mediaType/jsonMediaType.js +3 -3
- package/esm/oas/ref/DanglingRefError.d.ts +5 -2
- package/esm/oas/ref/DanglingRefError.js +4 -3
- package/esm/oas/ref/Ref.d.ts +23 -12
- package/esm/oas/ref/Ref.js +68 -72
- package/esm/oas/requestBody/RequestBody.d.ts +1 -1
- package/esm/oas/requestBody/RequestBody.js +1 -1
- package/esm/oas/response/Response.d.ts +1 -1
- package/esm/oas/response/Response.js +1 -1
- package/esm/oas/schema/Schema.d.ts +4 -2
- package/esm/oas/schema/siblings.d.ts +10 -2
- package/esm/oas/schema/siblings.js +87 -22
- package/esm/oas/schemaPath/traverseSchema.d.ts +2 -0
- package/esm/oas/schemaPath/traverseSchema.js +51 -5
- package/esm/parse/allOfSystem.d.ts +3 -0
- package/esm/parse/allOfSystem.js +2 -1
- package/esm/parse/keptIntersection.d.ts +53 -0
- package/esm/parse/keptIntersection.js +155 -0
- package/esm/parse/keptRefs.d.ts +23 -0
- package/esm/parse/keptRefs.js +59 -0
- package/esm/parse/toCyclicRefs.d.ts +8 -0
- package/esm/parse/toCyclicRefs.js +126 -0
- package/esm/parse/toDeclaredTypes.d.ts +20 -0
- package/esm/parse/toDeclaredTypes.js +43 -0
- package/esm/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
- package/esm/parse/v3-0/_merge-all-of/merge-union.js +7 -1
- package/esm/parse/v3-0/_merge-all-of/merge.js +13 -0
- package/esm/parse/v3-0/_merge-all-of/types.d.ts +3 -0
- package/esm/parse/v3-0/intersection/toIntersection.d.ts +12 -0
- package/esm/parse/v3-0/intersection/toIntersection.js +61 -0
- package/esm/parse/v3-0/ref/toRefTargetV3.d.ts +23 -0
- package/esm/parse/v3-0/ref/toRefTargetV3.js +70 -0
- package/esm/parse/v3-0/schema/toSchemasV3.js +13 -1
- package/esm/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
- package/esm/parse/v3-1/_merge-all-of/merge-union.js +7 -1
- package/esm/parse/v3-1/_merge-all-of/merge.js +13 -0
- package/esm/parse/v3-1/_merge-all-of/types.d.ts +3 -0
- package/esm/parse/v3-1/intersection/toIntersection.d.ts +12 -0
- package/esm/parse/v3-1/intersection/toIntersection.js +61 -0
- package/esm/parse/v3-1/ref/toRefTargetV3.d.ts +23 -0
- package/esm/parse/v3-1/ref/toRefTargetV3.js +70 -0
- package/esm/parse/v3-1/schema/toSchemasV3.js +15 -2
- package/esm/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
- package/esm/parse/v3-2/_merge-all-of/merge-union.js +7 -1
- package/esm/parse/v3-2/_merge-all-of/merge.js +13 -0
- package/esm/parse/v3-2/_merge-all-of/types.d.ts +3 -0
- package/esm/parse/v3-2/intersection/toIntersection.d.ts +12 -0
- package/esm/parse/v3-2/intersection/toIntersection.js +61 -0
- package/esm/parse/v3-2/ref/toRefTargetV3.d.ts +23 -0
- package/esm/parse/v3-2/ref/toRefTargetV3.js +70 -0
- package/esm/parse/v3-2/schema/toSchemasV3.js +15 -2
- package/esm/types/GeneratorType.d.ts +1 -1
- package/esm/types/ProjectionOptions.d.ts +1 -1
- package/esm/types/ProjectionOptions.js +1 -1
- package/esm/types/RefName.d.ts +3 -3
- package/esm/types/Settings.d.ts +9 -0
- package/esm/types/Settings.js +2 -1
- package/esm/types/TypeSystem.d.ts +20 -5
- package/package.json +1 -1
- package/script/anchors/attribute.js +3 -2
- package/script/context/CoreContext.d.ts +1 -1
- package/script/context/CoreContext.js +1 -1
- package/script/context/GenerateContext.d.ts +13 -10
- package/script/context/GenerateContext.js +36 -25
- package/script/context/ParseContext.d.ts +3 -0
- package/script/context/ParseContext.js +150 -38
- package/script/context/generateTypes.d.ts +7 -6
- package/script/dsl/GeneratorKeys.d.ts +21 -15
- package/script/dsl/GeneratorKeys.js +16 -11
- package/script/dsl/Inserted.d.ts +1 -1
- package/script/dsl/Inserted.js +1 -1
- package/script/dsl/model/ModelDriver.d.ts +6 -5
- package/script/dsl/model/ModelDriver.js +22 -20
- package/script/dsl/model/toModelEntry.d.ts +18 -18
- package/script/dsl/model/toModelEntry.js +9 -9
- package/script/dsl/model/toModelProjectionBase.d.ts +17 -16
- package/script/dsl/model/toModelProjectionBase.js +12 -8
- package/script/dsl/model/types.d.ts +26 -17
- package/script/dsl/operation/gql/toGqlOperationProjectionBase.d.ts +2 -2
- package/script/dsl/operation/gql/toGqlOperationProjectionBase.js +2 -2
- package/script/dsl/operation/oas/toOasOperationProjectionBase.d.ts +2 -2
- package/script/dsl/operation/oas/toOasOperationProjectionBase.js +2 -2
- package/script/dsl/webhook/toWebhookProjectionBase.d.ts +2 -2
- package/script/dsl/webhook/toWebhookProjectionBase.js +2 -2
- package/script/helpers/collateExamples.js +35 -17
- package/script/helpers/refFns.d.ts +108 -43
- package/script/helpers/refFns.js +180 -59
- package/script/mod.d.ts +1 -0
- package/script/mod.js +1 -0
- package/script/oas/document/Document.d.ts +6 -0
- package/script/oas/document/Document.js +44 -2
- package/script/oas/intersection/Intersection.d.ts +122 -0
- package/script/oas/intersection/Intersection.js +270 -0
- package/script/oas/mediaType/jsonMediaType.d.ts +1 -1
- package/script/oas/mediaType/jsonMediaType.js +3 -3
- package/script/oas/ref/DanglingRefError.d.ts +5 -2
- package/script/oas/ref/DanglingRefError.js +4 -3
- package/script/oas/ref/Ref.d.ts +23 -12
- package/script/oas/ref/Ref.js +67 -71
- package/script/oas/requestBody/RequestBody.d.ts +1 -1
- package/script/oas/requestBody/RequestBody.js +1 -1
- package/script/oas/response/Response.d.ts +1 -1
- package/script/oas/response/Response.js +1 -1
- package/script/oas/schema/Schema.d.ts +4 -2
- package/script/oas/schema/siblings.d.ts +10 -2
- package/script/oas/schema/siblings.js +89 -23
- package/script/oas/schemaPath/traverseSchema.d.ts +2 -0
- package/script/oas/schemaPath/traverseSchema.js +50 -4
- package/script/parse/allOfSystem.d.ts +3 -0
- package/script/parse/allOfSystem.js +2 -1
- package/script/parse/keptIntersection.d.ts +53 -0
- package/script/parse/keptIntersection.js +164 -0
- package/script/parse/keptRefs.d.ts +23 -0
- package/script/parse/keptRefs.js +63 -0
- package/script/parse/toCyclicRefs.d.ts +8 -0
- package/script/parse/toCyclicRefs.js +130 -0
- package/script/parse/toDeclaredTypes.d.ts +20 -0
- package/script/parse/toDeclaredTypes.js +48 -0
- package/script/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
- package/script/parse/v3-0/_merge-all-of/merge-union.js +7 -1
- package/script/parse/v3-0/_merge-all-of/merge.js +13 -0
- package/script/parse/v3-0/_merge-all-of/types.d.ts +3 -0
- package/script/parse/v3-0/intersection/toIntersection.d.ts +12 -0
- package/script/parse/v3-0/intersection/toIntersection.js +65 -0
- package/script/parse/v3-0/ref/toRefTargetV3.d.ts +23 -0
- package/script/parse/v3-0/ref/toRefTargetV3.js +74 -0
- package/script/parse/v3-0/schema/toSchemasV3.js +13 -1
- package/script/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
- package/script/parse/v3-1/_merge-all-of/merge-union.js +7 -1
- package/script/parse/v3-1/_merge-all-of/merge.js +13 -0
- package/script/parse/v3-1/_merge-all-of/types.d.ts +3 -0
- package/script/parse/v3-1/intersection/toIntersection.d.ts +12 -0
- package/script/parse/v3-1/intersection/toIntersection.js +65 -0
- package/script/parse/v3-1/ref/toRefTargetV3.d.ts +23 -0
- package/script/parse/v3-1/ref/toRefTargetV3.js +74 -0
- package/script/parse/v3-1/schema/toSchemasV3.js +15 -2
- package/script/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
- package/script/parse/v3-2/_merge-all-of/merge-union.js +7 -1
- package/script/parse/v3-2/_merge-all-of/merge.js +13 -0
- package/script/parse/v3-2/_merge-all-of/types.d.ts +3 -0
- package/script/parse/v3-2/intersection/toIntersection.d.ts +12 -0
- package/script/parse/v3-2/intersection/toIntersection.js +65 -0
- package/script/parse/v3-2/ref/toRefTargetV3.d.ts +23 -0
- package/script/parse/v3-2/ref/toRefTargetV3.js +74 -0
- package/script/parse/v3-2/schema/toSchemasV3.js +15 -2
- package/script/types/GeneratorType.d.ts +1 -1
- package/script/types/ProjectionOptions.d.ts +1 -1
- package/script/types/ProjectionOptions.js +1 -1
- package/script/types/RefName.d.ts +3 -3
- package/script/types/Settings.d.ts +9 -0
- package/script/types/Settings.js +2 -1
- package/script/types/TypeSystem.d.ts +20 -5
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ParseContextType } from '../../../context/parseTypes.js';
|
|
2
|
+
import type { StackTrail } from '../../../context/StackTrail.js';
|
|
3
|
+
import type { ComponentsEntry } from '../../../oas/components/Components.js';
|
|
4
|
+
import type { OasRefData } from '../../../oas/ref/ref-types.js';
|
|
5
|
+
/** What {@link toRefTargetV3} parses. */
|
|
6
|
+
export type ToRefTargetArgs = {
|
|
7
|
+
/** The kind of component the ref is written for. */
|
|
8
|
+
refType: OasRefData['refType'];
|
|
9
|
+
/** The last segment of the target's pointer, unescaped: the key it parses under. */
|
|
10
|
+
name: string;
|
|
11
|
+
/** The raw value at the pointer. */
|
|
12
|
+
value: Record<string, unknown>;
|
|
13
|
+
/** The trail of the target's parent, so the target parses at its own position. */
|
|
14
|
+
stackTrail: StackTrail;
|
|
15
|
+
context: ParseContextType;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Parses the target of a ref that does not point at a component
|
|
19
|
+
* (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
|
|
20
|
+
* at its own position, with the parser that components of its kind use. Gives
|
|
21
|
+
* `undefined` when the target does not parse (the parser has logged why).
|
|
22
|
+
*/
|
|
23
|
+
export declare const toRefTargetV3: ({ refType, name, value, stackTrail, context }: ToRefTargetArgs) => ComponentsEntry | undefined;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isPlainObject } from '../../isPlainObject.js';
|
|
2
|
+
import { toOptionalSchemasV3 } from '../schema/toSchemasV3.js';
|
|
3
|
+
import { toOptionalParametersV3 } from '../parameter/toParameterV3.js';
|
|
4
|
+
import { toOptionalResponsesV3 } from '../response/toResponseV3.js';
|
|
5
|
+
import { toRequestBodiesV3 } from '../requestBody/toRequestBodiesV3.js';
|
|
6
|
+
import { toComponentHeadersV3 } from '../header/toHeadersV3.js';
|
|
7
|
+
import { toLinksV3 } from '../link/toLinksV3.js';
|
|
8
|
+
import { toExampleV3 } from '../example/toExamplesV3.js';
|
|
9
|
+
import { toSecuritySchemesV3 } from '../securitySchemes/toSecuritySchemes.js';
|
|
10
|
+
import { tryParseAt } from '../../../context/tryParseAt.js';
|
|
11
|
+
/**
|
|
12
|
+
* The raw value read as the OpenAPI type its parser takes, as the components
|
|
13
|
+
* parser reads `documentObject.components`: the parser checks what it reads.
|
|
14
|
+
*/
|
|
15
|
+
const isRawEntry = (value) => isPlainObject(value);
|
|
16
|
+
/**
|
|
17
|
+
* Parses the target of a ref that does not point at a component
|
|
18
|
+
* (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
|
|
19
|
+
* at its own position, with the parser that components of its kind use. Gives
|
|
20
|
+
* `undefined` when the target does not parse (the parser has logged why).
|
|
21
|
+
*/
|
|
22
|
+
export const toRefTargetV3 = ({ refType, name, value, stackTrail, context }) => {
|
|
23
|
+
switch (refType) {
|
|
24
|
+
case 'schema':
|
|
25
|
+
return isRawEntry(value)
|
|
26
|
+
? toOptionalSchemasV3({ schemas: { [name]: value }, stackTrail, context })?.[name]
|
|
27
|
+
: undefined;
|
|
28
|
+
case 'parameter':
|
|
29
|
+
return isRawEntry(value)
|
|
30
|
+
? toOptionalParametersV3({ parameters: { [name]: value }, stackTrail, context })?.[name]
|
|
31
|
+
: undefined;
|
|
32
|
+
case 'response':
|
|
33
|
+
return isRawEntry(value)
|
|
34
|
+
? toOptionalResponsesV3({ responses: { [name]: value }, stackTrail, context })?.[name]
|
|
35
|
+
: undefined;
|
|
36
|
+
case 'requestBody':
|
|
37
|
+
return isRawEntry(value)
|
|
38
|
+
? toRequestBodiesV3({ requestBodies: { [name]: value }, stackTrail, context })?.[name]
|
|
39
|
+
: undefined;
|
|
40
|
+
case 'header':
|
|
41
|
+
return isRawEntry(value)
|
|
42
|
+
? toComponentHeadersV3({ headers: { [name]: value }, stackTrail, context })?.[name]
|
|
43
|
+
: undefined;
|
|
44
|
+
case 'link':
|
|
45
|
+
return isRawEntry(value)
|
|
46
|
+
? toLinksV3({ links: { [name]: value }, stackTrail, context })?.[name]
|
|
47
|
+
: undefined;
|
|
48
|
+
case 'example':
|
|
49
|
+
// No components parser reads a single example by key, so the target
|
|
50
|
+
// is parsed as `toExamplesV3` parses each entry, under `tryParseAt`.
|
|
51
|
+
return isRawEntry(value)
|
|
52
|
+
? tryParseAt({
|
|
53
|
+
stackTrail,
|
|
54
|
+
key: name,
|
|
55
|
+
context,
|
|
56
|
+
type: 'INVALID_EXAMPLE',
|
|
57
|
+
parent: value,
|
|
58
|
+
fn: st => toExampleV3({ example: value, stackTrail: st, context })
|
|
59
|
+
})
|
|
60
|
+
: undefined;
|
|
61
|
+
case 'securityScheme':
|
|
62
|
+
return isRawEntry(value)
|
|
63
|
+
? toSecuritySchemesV3({ securitySchemes: { [name]: value }, stackTrail, context })?.[name]
|
|
64
|
+
: undefined;
|
|
65
|
+
default: {
|
|
66
|
+
const _exhaustive = refType;
|
|
67
|
+
throw new Error(`Unhandled ref type: ${_exhaustive}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
@@ -10,6 +10,8 @@ import { toBoolean } from '../boolean/toBoolean.js';
|
|
|
10
10
|
import { toString } from '../string/toString.js';
|
|
11
11
|
import { toUnknown } from '../unknown/toUnknown.js';
|
|
12
12
|
import { toUnion } from '../union/toUnion.js';
|
|
13
|
+
import { toIntersection } from '../intersection/toIntersection.js';
|
|
14
|
+
import { toIntersectionParts, toSiteIntersection } from '../../keptIntersection.js';
|
|
13
15
|
import { toGetRef } from '../../../helpers/refFns.js';
|
|
14
16
|
import { toSiblingKeywords } from '../../toSiblingKeywords.js';
|
|
15
17
|
import { logAllOfContradictions, toAllOfSystem } from '../../allOfSystem.js';
|
|
@@ -182,7 +184,17 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
182
184
|
});
|
|
183
185
|
logAllOfContradictions({ system, stackTrail, parent: schema, context });
|
|
184
186
|
logMergedUnknownAnyOf({ system, stackTrail, parent: schema, context });
|
|
185
|
-
|
|
187
|
+
const parts = system.keptRefs && toIntersectionParts(merged, system.keptRefs);
|
|
188
|
+
if (parts === undefined) {
|
|
189
|
+
return toSchemaV3({ schema: merged, stackTrail: st, context });
|
|
190
|
+
}
|
|
191
|
+
const { own, members: kept } = toSiteIntersection(parts);
|
|
192
|
+
const [only] = kept;
|
|
193
|
+
if (kept.length > 1 || only === undefined) {
|
|
194
|
+
return toIntersection({ value: own, members: kept, stackTrail: st, context });
|
|
195
|
+
}
|
|
196
|
+
// An intersection of one ref is that ref with the keywords beside it.
|
|
197
|
+
return toSchemaV3({ schema: isEmpty(own) ? only : { ...own, allOf: [only] }, stackTrail: st, context });
|
|
186
198
|
});
|
|
187
199
|
}
|
|
188
200
|
if ('oneOf' in schema && Array.isArray(schema.oneOf)) {
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { decomposeIntersection } from './decompose-intersection.js';
|
|
2
2
|
import { mergeSchemasOrRefs } from './merge.js';
|
|
3
|
+
import { isEmpty } from '../../../helpers/isEmpty.js';
|
|
4
|
+
import { splitOwnKeywords, withOwnKeywords } from '../../keptIntersection.js';
|
|
3
5
|
export const mergeIntersection = ({ schema, getRef, normalize, system }) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
// Where a ref stays a ref, the wrapper's own keywords stay beside the `allOf`.
|
|
7
|
+
const { own, rest } = system.keptRefs?.holds(schema) ? splitOwnKeywords(schema) : { own: {}, rest: schema };
|
|
8
|
+
const decomposed = decomposeIntersection({ schema: rest });
|
|
9
|
+
if (decomposed.length === 1 && isEmpty(own)) {
|
|
6
10
|
return decomposed[0];
|
|
7
11
|
}
|
|
8
12
|
const dereffed = decomposed.map(decomposed => {
|
|
9
|
-
return '$ref' in decomposed ? getRef(decomposed) : decomposed;
|
|
13
|
+
return '$ref' in decomposed && !system.keptRefs?.has(decomposed) ? getRef(decomposed) : decomposed;
|
|
10
14
|
});
|
|
11
15
|
const result = dereffed.reduce((acc, decomposed) => {
|
|
12
16
|
const merged = mergeSchemasOrRefs({ first: acc, second: decomposed, getRef, normalize, system });
|
|
13
17
|
return merged;
|
|
14
18
|
}, {});
|
|
15
|
-
return result;
|
|
19
|
+
return withOwnKeywords(result, own);
|
|
16
20
|
};
|
|
@@ -101,7 +101,13 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, system, gr
|
|
|
101
101
|
secondGroup.length !== secondMembers.length;
|
|
102
102
|
const mergedGroup = mergePairs({
|
|
103
103
|
pairs: crossProduct(withGroupSiblings({ schema: first, members: firstGroup, getRef, normalize, system, groupType }), withGroupSiblings({ schema: second, members: secondGroup, getRef, normalize, system, groupType })),
|
|
104
|
-
merge: ([firstItem, secondItem], pairSystem) =>
|
|
104
|
+
merge: ([firstItem, secondItem], pairSystem) => {
|
|
105
|
+
// A kept ref is not read into the merge, so its type is compared here.
|
|
106
|
+
if (pairSystem.allOf === 'strict' && system.keptRefs?.contradict(firstItem, secondItem)) {
|
|
107
|
+
throw new Error('Cannot merge schemas: a member has another type than the reference');
|
|
108
|
+
}
|
|
109
|
+
return mergeSchemasOrRefs({ first: firstItem, second: secondItem, getRef, normalize, system: pairSystem });
|
|
110
|
+
},
|
|
105
111
|
system
|
|
106
112
|
});
|
|
107
113
|
// A nested group is spread into this one below; its `nullable` carries over.
|
|
@@ -17,6 +17,7 @@ import { mergeIntersection } from './merge-intersection.js';
|
|
|
17
17
|
import { collapseNullOnlyUnion, mergeCrossProduct, toOwnSystem } from './merge-union.js';
|
|
18
18
|
import { isEmpty } from '../../../helpers/isEmpty.js';
|
|
19
19
|
import { isNullOnly, mergeNullOnly } from './nullable-merge.js';
|
|
20
|
+
import { mergeKept, toIntersectionParts } from '../../keptIntersection.js';
|
|
20
21
|
import { withoutUnknownAnyOf } from '../../anyOfSystem.js';
|
|
21
22
|
export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, getRef, normalize, system }) => {
|
|
22
23
|
const first = isRef(inputFirst)
|
|
@@ -25,6 +26,18 @@ export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, get
|
|
|
25
26
|
const second = isRef(inputSecond)
|
|
26
27
|
? inputSecond
|
|
27
28
|
: collapseNullOnlyUnion(normalize(inputSecond), normalize);
|
|
29
|
+
const { keptRefs } = system;
|
|
30
|
+
if (keptRefs && (toIntersectionParts(first, keptRefs) || toIntersectionParts(second, keptRefs))) {
|
|
31
|
+
return mergeKept({
|
|
32
|
+
first,
|
|
33
|
+
second,
|
|
34
|
+
keptRefs,
|
|
35
|
+
merge: (a, b) => mergeSchemasOrRefs({ first: a, second: b, getRef, normalize, system }),
|
|
36
|
+
mergeAllOf: schema => mergeIntersection({ schema, getRef, normalize, system: toOwnSystem(system) }),
|
|
37
|
+
readRef: ref => normalize(getRef(ref)),
|
|
38
|
+
normalize
|
|
39
|
+
});
|
|
40
|
+
}
|
|
28
41
|
if (containsRef(first, second)) {
|
|
29
42
|
return mergeWithRef({ first, second, getRef, normalize, system });
|
|
30
43
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { OpenAPIV3 } from 'openapi-types';
|
|
2
2
|
import * as v from 'valibot';
|
|
3
|
+
import type { KeptRefs } from '../../keptRefs.js';
|
|
3
4
|
export type GetRefFn = (ref: OpenAPIV3.ReferenceObject) => OpenAPIV3.SchemaObject;
|
|
4
5
|
/**
|
|
5
6
|
* Maps a schema the merge reads (a member, a nested property or item schema,
|
|
@@ -28,6 +29,8 @@ export type MergeSystem = {
|
|
|
28
29
|
anyOf?: 'union' | 'unknown';
|
|
29
30
|
/** The keywords the merge read as any value: `anyOf` under `system.anyOf: unknown`. */
|
|
30
31
|
unknown?: Set<string>;
|
|
32
|
+
/** The refs that stay refs (`system.intersections`). Left out: every ref is followed. */
|
|
33
|
+
keptRefs?: KeptRefs;
|
|
31
34
|
};
|
|
32
35
|
export type OneOfObject = {
|
|
33
36
|
oneOf?: (OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject)[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { OpenAPIV3 } from 'openapi-types';
|
|
2
|
+
import type { ParseContextType } from '../../../context/parseTypes.js';
|
|
3
|
+
import type { StackTrail } from '../../../context/StackTrail.js';
|
|
4
|
+
import { OasIntersection } from '../../../oas/intersection/Intersection.js';
|
|
5
|
+
export type ToIntersectionArgs = {
|
|
6
|
+
/** The intersection's own keywords, without `allOf`. */
|
|
7
|
+
value: OpenAPIV3.SchemaObject;
|
|
8
|
+
members: (OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject)[];
|
|
9
|
+
stackTrail: StackTrail;
|
|
10
|
+
context: ParseContextType;
|
|
11
|
+
};
|
|
12
|
+
export declare const toIntersection: ({ value, members, stackTrail, context }: ToIntersectionArgs) => OasIntersection;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { OasIntersection } from '../../../oas/intersection/Intersection.js';
|
|
2
|
+
import { isRef } from '../../../helpers/refFns.js';
|
|
3
|
+
import { hasShapeKeyword } from '../../anyOfSystem.js';
|
|
4
|
+
import { withKeywordType } from '../../keptIntersection.js';
|
|
5
|
+
import { toSchemaV3 } from '../schema/toSchemasV3.js';
|
|
6
|
+
import { toUnknown } from '../unknown/toUnknown.js';
|
|
7
|
+
import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
|
|
8
|
+
import { parseNullable } from '../_helpers/parseNullable.js';
|
|
9
|
+
import { parseExample } from '../_helpers/parseExample.js';
|
|
10
|
+
import { parseDefault } from '../_helpers/parseDefault.js';
|
|
11
|
+
import { toExternalDocs } from '../externalDocs/toExternalDocs.js';
|
|
12
|
+
export const toIntersection = ({ value, members, stackTrail, context }) => {
|
|
13
|
+
const { nullable, value: valueWithoutNullable } = parseNullable({ value, context, stackTrail });
|
|
14
|
+
const { title, description, externalDocs, readOnly, writeOnly, deprecated, example: unparsedExample, default: unparsedDefaultValue, ...skipped } = valueWithoutNullable;
|
|
15
|
+
const example = parseExample({
|
|
16
|
+
value: unparsedExample,
|
|
17
|
+
context,
|
|
18
|
+
parent: value,
|
|
19
|
+
nullable,
|
|
20
|
+
check: isPresent,
|
|
21
|
+
toMessage: item => `Removed invalid example. Expected a non-null value, got: ${item}`,
|
|
22
|
+
stackTrail
|
|
23
|
+
});
|
|
24
|
+
const defaultValue = parseDefault({
|
|
25
|
+
value: unparsedDefaultValue,
|
|
26
|
+
context,
|
|
27
|
+
parent: value,
|
|
28
|
+
nullable,
|
|
29
|
+
check: isPresent,
|
|
30
|
+
toMessage: item => `Removed invalid default. Expected a non-null value, got: ${item}`,
|
|
31
|
+
stackTrail
|
|
32
|
+
});
|
|
33
|
+
const extensionFields = toSpecificationExtensionsV3({
|
|
34
|
+
skipped,
|
|
35
|
+
parent: value,
|
|
36
|
+
context,
|
|
37
|
+
stackTrail,
|
|
38
|
+
parentType: 'schema:allOf'
|
|
39
|
+
});
|
|
40
|
+
// A member with no shape of its own (`{ description }`) is any value: the
|
|
41
|
+
// parser must not guess a type from its `example`.
|
|
42
|
+
const membersParsed = members.map(withKeywordType).map((member, index) => stackTrail.trace(`${index}`, st => isRef(member) || hasShapeKeyword(member) || 'anyOf' in member || 'not' in member
|
|
43
|
+
? toSchemaV3({ schema: member, stackTrail: st, context })
|
|
44
|
+
: toUnknown({ value: member, stackTrail: st, context })));
|
|
45
|
+
return context.withStackTrail(stackTrail, () => new OasIntersection({
|
|
46
|
+
title,
|
|
47
|
+
description,
|
|
48
|
+
externalDocs: stackTrail.trace('externalDocs', st => toExternalDocs({ externalDocs, stackTrail: st, context })),
|
|
49
|
+
nullable,
|
|
50
|
+
readOnly,
|
|
51
|
+
writeOnly,
|
|
52
|
+
deprecated,
|
|
53
|
+
default: defaultValue,
|
|
54
|
+
members: membersParsed,
|
|
55
|
+
example,
|
|
56
|
+
extensionFields
|
|
57
|
+
}, context));
|
|
58
|
+
};
|
|
59
|
+
// A value matches every member, so without resolving them the most we can
|
|
60
|
+
// check is that a present default/example is not `null` on a non-nullable schema.
|
|
61
|
+
const isPresent = (value) => value !== null && value !== undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ParseContextType } from '../../../context/parseTypes.js';
|
|
2
|
+
import type { StackTrail } from '../../../context/StackTrail.js';
|
|
3
|
+
import type { ComponentsEntry } from '../../../oas/components/Components.js';
|
|
4
|
+
import type { OasRefData } from '../../../oas/ref/ref-types.js';
|
|
5
|
+
/** What {@link toRefTargetV3} parses. */
|
|
6
|
+
export type ToRefTargetArgs = {
|
|
7
|
+
/** The kind of component the ref is written for. */
|
|
8
|
+
refType: OasRefData['refType'];
|
|
9
|
+
/** The last segment of the target's pointer, unescaped: the key it parses under. */
|
|
10
|
+
name: string;
|
|
11
|
+
/** The raw value at the pointer. */
|
|
12
|
+
value: Record<string, unknown>;
|
|
13
|
+
/** The trail of the target's parent, so the target parses at its own position. */
|
|
14
|
+
stackTrail: StackTrail;
|
|
15
|
+
context: ParseContextType;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Parses the target of a ref that does not point at a component
|
|
19
|
+
* (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
|
|
20
|
+
* at its own position, with the parser that components of its kind use. Gives
|
|
21
|
+
* `undefined` when the target does not parse (the parser has logged why).
|
|
22
|
+
*/
|
|
23
|
+
export declare const toRefTargetV3: ({ refType, name, value, stackTrail, context }: ToRefTargetArgs) => ComponentsEntry | undefined;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isPlainObject } from '../../isPlainObject.js';
|
|
2
|
+
import { toOptionalSchemasV3 } from '../schema/toSchemasV3.js';
|
|
3
|
+
import { toOptionalParametersV3 } from '../parameter/toParameterV3.js';
|
|
4
|
+
import { toOptionalResponsesV3 } from '../response/toResponseV3.js';
|
|
5
|
+
import { toRequestBodiesV3 } from '../requestBody/toRequestBodiesV3.js';
|
|
6
|
+
import { toComponentHeadersV3 } from '../header/toHeadersV3.js';
|
|
7
|
+
import { toLinksV3 } from '../link/toLinksV3.js';
|
|
8
|
+
import { toExampleV3 } from '../example/toExamplesV3.js';
|
|
9
|
+
import { toSecuritySchemesV3 } from '../securitySchemes/toSecuritySchemes.js';
|
|
10
|
+
import { tryParseAt } from '../../../context/tryParseAt.js';
|
|
11
|
+
/**
|
|
12
|
+
* The raw value read as the OpenAPI type its parser takes, as the components
|
|
13
|
+
* parser reads `documentObject.components`: the parser checks what it reads.
|
|
14
|
+
*/
|
|
15
|
+
const isRawEntry = (value) => isPlainObject(value);
|
|
16
|
+
/**
|
|
17
|
+
* Parses the target of a ref that does not point at a component
|
|
18
|
+
* (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
|
|
19
|
+
* at its own position, with the parser that components of its kind use. Gives
|
|
20
|
+
* `undefined` when the target does not parse (the parser has logged why).
|
|
21
|
+
*/
|
|
22
|
+
export const toRefTargetV3 = ({ refType, name, value, stackTrail, context }) => {
|
|
23
|
+
switch (refType) {
|
|
24
|
+
case 'schema':
|
|
25
|
+
return isRawEntry(value)
|
|
26
|
+
? toOptionalSchemasV3({ schemas: { [name]: value }, stackTrail, context })?.[name]
|
|
27
|
+
: undefined;
|
|
28
|
+
case 'parameter':
|
|
29
|
+
return isRawEntry(value)
|
|
30
|
+
? toOptionalParametersV3({ parameters: { [name]: value }, stackTrail, context })?.[name]
|
|
31
|
+
: undefined;
|
|
32
|
+
case 'response':
|
|
33
|
+
return isRawEntry(value)
|
|
34
|
+
? toOptionalResponsesV3({ responses: { [name]: value }, stackTrail, context })?.[name]
|
|
35
|
+
: undefined;
|
|
36
|
+
case 'requestBody':
|
|
37
|
+
return isRawEntry(value)
|
|
38
|
+
? toRequestBodiesV3({ requestBodies: { [name]: value }, stackTrail, context })?.[name]
|
|
39
|
+
: undefined;
|
|
40
|
+
case 'header':
|
|
41
|
+
return isRawEntry(value)
|
|
42
|
+
? toComponentHeadersV3({ headers: { [name]: value }, stackTrail, context })?.[name]
|
|
43
|
+
: undefined;
|
|
44
|
+
case 'link':
|
|
45
|
+
return isRawEntry(value)
|
|
46
|
+
? toLinksV3({ links: { [name]: value }, stackTrail, context })?.[name]
|
|
47
|
+
: undefined;
|
|
48
|
+
case 'example':
|
|
49
|
+
// No components parser reads a single example by key, so the target
|
|
50
|
+
// is parsed as `toExamplesV3` parses each entry, under `tryParseAt`.
|
|
51
|
+
return isRawEntry(value)
|
|
52
|
+
? tryParseAt({
|
|
53
|
+
stackTrail,
|
|
54
|
+
key: name,
|
|
55
|
+
context,
|
|
56
|
+
type: 'INVALID_EXAMPLE',
|
|
57
|
+
parent: value,
|
|
58
|
+
fn: st => toExampleV3({ example: value, stackTrail: st, context })
|
|
59
|
+
})
|
|
60
|
+
: undefined;
|
|
61
|
+
case 'securityScheme':
|
|
62
|
+
return isRawEntry(value)
|
|
63
|
+
? toSecuritySchemesV3({ securitySchemes: { [name]: value }, stackTrail, context })?.[name]
|
|
64
|
+
: undefined;
|
|
65
|
+
default: {
|
|
66
|
+
const _exhaustive = refType;
|
|
67
|
+
throw new Error(`Unhandled ref type: ${_exhaustive}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
@@ -10,6 +10,8 @@ import { toBoolean } from '../boolean/toBoolean.js';
|
|
|
10
10
|
import { toString } from '../string/toString.js';
|
|
11
11
|
import { toUnknown } from '../unknown/toUnknown.js';
|
|
12
12
|
import { toUnion } from '../union/toUnion.js';
|
|
13
|
+
import { toIntersection } from '../intersection/toIntersection.js';
|
|
14
|
+
import { toIntersectionParts, toSiteIntersection } from '../../keptIntersection.js';
|
|
13
15
|
import { toGetRef } from '../../../helpers/refFns.js';
|
|
14
16
|
import { toSiblingKeywords } from '../../toSiblingKeywords.js';
|
|
15
17
|
import { logAllOfContradictions, toAllOfSystem } from '../../allOfSystem.js';
|
|
@@ -132,8 +134,9 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
132
134
|
});
|
|
133
135
|
}
|
|
134
136
|
const members = Array.isArray(allOf) ? allOf : [];
|
|
137
|
+
const bareRef = context.keptRefs?.copy(schema, { $ref }) ?? { $ref };
|
|
135
138
|
return Object.keys(keywords).length > 0
|
|
136
|
-
? toSchemaV3({ schema: { ...keywords, allOf: [
|
|
139
|
+
? toSchemaV3({ schema: { ...keywords, allOf: [bareRef, ...members] }, stackTrail, context })
|
|
137
140
|
: toRefV31({ ref: schema, refType: 'schema', stackTrail, context });
|
|
138
141
|
}
|
|
139
142
|
// OpenAPI `not` has no faithful TypeScript representation: a generated type
|
|
@@ -235,7 +238,17 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
|
|
|
235
238
|
});
|
|
236
239
|
logAllOfContradictions({ system, stackTrail, parent: schema, context });
|
|
237
240
|
logMergedUnknownAnyOf({ system, stackTrail, parent: schema, context });
|
|
238
|
-
|
|
241
|
+
const parts = system.keptRefs && toIntersectionParts(merged, system.keptRefs);
|
|
242
|
+
if (parts === undefined) {
|
|
243
|
+
return toSchemaV3({ schema: merged, stackTrail: st, context });
|
|
244
|
+
}
|
|
245
|
+
const { own, members: kept } = toSiteIntersection(parts);
|
|
246
|
+
const [only] = kept;
|
|
247
|
+
if (kept.length > 1 || only === undefined) {
|
|
248
|
+
return toIntersection({ value: own, members: kept, stackTrail: st, context });
|
|
249
|
+
}
|
|
250
|
+
// An intersection of one ref is that ref with the keywords beside it.
|
|
251
|
+
return toSchemaV3({ schema: isEmpty(own) ? only : { ...own, allOf: [only] }, stackTrail: st, context });
|
|
239
252
|
});
|
|
240
253
|
}
|
|
241
254
|
// A `oneOf`/`anyOf` of only null members reads as nullable, not as an
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { decomposeIntersection } from './decompose-intersection.js';
|
|
2
2
|
import { mergeSchemasOrRefs } from './merge.js';
|
|
3
|
+
import { isEmpty } from '../../../helpers/isEmpty.js';
|
|
4
|
+
import { splitOwnKeywords, withOwnKeywords } from '../../keptIntersection.js';
|
|
3
5
|
export const mergeIntersection = ({ schema, getRef, normalize, system }) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
// Where a ref stays a ref, the wrapper's own keywords stay beside the `allOf`.
|
|
7
|
+
const { own, rest } = system.keptRefs?.holds(schema) ? splitOwnKeywords(schema) : { own: {}, rest: schema };
|
|
8
|
+
const decomposed = decomposeIntersection({ schema: rest });
|
|
9
|
+
if (decomposed.length === 1 && isEmpty(own)) {
|
|
6
10
|
return decomposed[0];
|
|
7
11
|
}
|
|
8
12
|
const dereffed = decomposed.map(decomposed => {
|
|
9
|
-
return '$ref' in decomposed ? getRef(decomposed) : decomposed;
|
|
13
|
+
return '$ref' in decomposed && !system.keptRefs?.has(decomposed) ? getRef(decomposed) : decomposed;
|
|
10
14
|
});
|
|
11
15
|
const result = dereffed.reduce((acc, decomposed) => {
|
|
12
16
|
const merged = mergeSchemasOrRefs({ first: acc, second: decomposed, getRef, normalize, system });
|
|
13
17
|
return merged;
|
|
14
18
|
}, {});
|
|
15
|
-
return result;
|
|
19
|
+
return withOwnKeywords(result, own);
|
|
16
20
|
};
|
|
@@ -101,7 +101,13 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, system, gr
|
|
|
101
101
|
secondGroup.length !== secondMembers.length;
|
|
102
102
|
const mergedGroup = mergePairs({
|
|
103
103
|
pairs: crossProduct(withGroupSiblings({ schema: first, members: firstGroup, getRef, normalize, system, groupType }), withGroupSiblings({ schema: second, members: secondGroup, getRef, normalize, system, groupType })),
|
|
104
|
-
merge: ([firstItem, secondItem], pairSystem) =>
|
|
104
|
+
merge: ([firstItem, secondItem], pairSystem) => {
|
|
105
|
+
// A kept ref is not read into the merge, so its type is compared here.
|
|
106
|
+
if (pairSystem.allOf === 'strict' && system.keptRefs?.contradict(firstItem, secondItem)) {
|
|
107
|
+
throw new Error('Cannot merge schemas: a member has another type than the reference');
|
|
108
|
+
}
|
|
109
|
+
return mergeSchemasOrRefs({ first: firstItem, second: secondItem, getRef, normalize, system: pairSystem });
|
|
110
|
+
},
|
|
105
111
|
system
|
|
106
112
|
});
|
|
107
113
|
// A nested group is spread into this one below; its `nullable` carries over.
|
|
@@ -17,6 +17,7 @@ import { mergeIntersection } from './merge-intersection.js';
|
|
|
17
17
|
import { collapseNullOnlyUnion, mergeCrossProduct, toOwnSystem } from './merge-union.js';
|
|
18
18
|
import { isEmpty } from '../../../helpers/isEmpty.js';
|
|
19
19
|
import { isNullOnly, mergeNullOnly } from './nullable-merge.js';
|
|
20
|
+
import { mergeKept, toIntersectionParts } from '../../keptIntersection.js';
|
|
20
21
|
import { withoutUnknownAnyOf } from '../../anyOfSystem.js';
|
|
21
22
|
export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, getRef, normalize, system }) => {
|
|
22
23
|
const first = isRef(inputFirst)
|
|
@@ -25,6 +26,18 @@ export const mergeSchemasOrRefs = ({ first: inputFirst, second: inputSecond, get
|
|
|
25
26
|
const second = isRef(inputSecond)
|
|
26
27
|
? inputSecond
|
|
27
28
|
: collapseNullOnlyUnion(normalize(inputSecond), normalize);
|
|
29
|
+
const { keptRefs } = system;
|
|
30
|
+
if (keptRefs && (toIntersectionParts(first, keptRefs) || toIntersectionParts(second, keptRefs))) {
|
|
31
|
+
return mergeKept({
|
|
32
|
+
first,
|
|
33
|
+
second,
|
|
34
|
+
keptRefs,
|
|
35
|
+
merge: (a, b) => mergeSchemasOrRefs({ first: a, second: b, getRef, normalize, system }),
|
|
36
|
+
mergeAllOf: schema => mergeIntersection({ schema, getRef, normalize, system: toOwnSystem(system) }),
|
|
37
|
+
readRef: ref => normalize(getRef(ref)),
|
|
38
|
+
normalize
|
|
39
|
+
});
|
|
40
|
+
}
|
|
28
41
|
if (containsRef(first, second)) {
|
|
29
42
|
return mergeWithRef({ first, second, getRef, normalize, system });
|
|
30
43
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { OpenAPIV3 } from 'openapi-types';
|
|
2
2
|
import * as v from 'valibot';
|
|
3
|
+
import type { KeptRefs } from '../../keptRefs.js';
|
|
3
4
|
export type GetRefFn = (ref: OpenAPIV3.ReferenceObject) => OpenAPIV3.SchemaObject;
|
|
4
5
|
/**
|
|
5
6
|
* Maps a schema the merge reads (a member, a nested property or item schema,
|
|
@@ -28,6 +29,8 @@ export type MergeSystem = {
|
|
|
28
29
|
anyOf?: 'union' | 'unknown';
|
|
29
30
|
/** The keywords the merge read as any value: `anyOf` under `system.anyOf: unknown`. */
|
|
30
31
|
unknown?: Set<string>;
|
|
32
|
+
/** The refs that stay refs (`system.intersections`). Left out: every ref is followed. */
|
|
33
|
+
keptRefs?: KeptRefs;
|
|
31
34
|
};
|
|
32
35
|
export type OneOfObject = {
|
|
33
36
|
oneOf?: (OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject)[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { OpenAPIV3 } from 'openapi-types';
|
|
2
|
+
import type { ParseContextType } from '../../../context/parseTypes.js';
|
|
3
|
+
import type { StackTrail } from '../../../context/StackTrail.js';
|
|
4
|
+
import { OasIntersection } from '../../../oas/intersection/Intersection.js';
|
|
5
|
+
export type ToIntersectionArgs = {
|
|
6
|
+
/** The intersection's own keywords, without `allOf`. */
|
|
7
|
+
value: OpenAPIV3.SchemaObject;
|
|
8
|
+
members: (OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject)[];
|
|
9
|
+
stackTrail: StackTrail;
|
|
10
|
+
context: ParseContextType;
|
|
11
|
+
};
|
|
12
|
+
export declare const toIntersection: ({ value, members, stackTrail, context }: ToIntersectionArgs) => OasIntersection;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { OasIntersection } from '../../../oas/intersection/Intersection.js';
|
|
2
|
+
import { isRef } from '../../../helpers/refFns.js';
|
|
3
|
+
import { hasShapeKeyword } from '../../anyOfSystem.js';
|
|
4
|
+
import { withKeywordType } from '../../keptIntersection.js';
|
|
5
|
+
import { toSchemaV3 } from '../schema/toSchemasV3.js';
|
|
6
|
+
import { toUnknown } from '../unknown/toUnknown.js';
|
|
7
|
+
import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
|
|
8
|
+
import { parseNullable } from '../_helpers/parseNullable.js';
|
|
9
|
+
import { parseExample } from '../_helpers/parseExample.js';
|
|
10
|
+
import { parseDefault } from '../_helpers/parseDefault.js';
|
|
11
|
+
import { toExternalDocs } from '../externalDocs/toExternalDocs.js';
|
|
12
|
+
export const toIntersection = ({ value, members, stackTrail, context }) => {
|
|
13
|
+
const { nullable, value: valueWithoutNullable } = parseNullable({ value, context, stackTrail });
|
|
14
|
+
const { title, description, externalDocs, readOnly, writeOnly, deprecated, example: unparsedExample, default: unparsedDefaultValue, ...skipped } = valueWithoutNullable;
|
|
15
|
+
const example = parseExample({
|
|
16
|
+
value: unparsedExample,
|
|
17
|
+
context,
|
|
18
|
+
parent: value,
|
|
19
|
+
nullable,
|
|
20
|
+
check: isPresent,
|
|
21
|
+
toMessage: item => `Removed invalid example. Expected a non-null value, got: ${item}`,
|
|
22
|
+
stackTrail
|
|
23
|
+
});
|
|
24
|
+
const defaultValue = parseDefault({
|
|
25
|
+
value: unparsedDefaultValue,
|
|
26
|
+
context,
|
|
27
|
+
parent: value,
|
|
28
|
+
nullable,
|
|
29
|
+
check: isPresent,
|
|
30
|
+
toMessage: item => `Removed invalid default. Expected a non-null value, got: ${item}`,
|
|
31
|
+
stackTrail
|
|
32
|
+
});
|
|
33
|
+
const extensionFields = toSpecificationExtensionsV3({
|
|
34
|
+
skipped,
|
|
35
|
+
parent: value,
|
|
36
|
+
context,
|
|
37
|
+
stackTrail,
|
|
38
|
+
parentType: 'schema:allOf'
|
|
39
|
+
});
|
|
40
|
+
// A member with no shape of its own (`{ description }`) is any value: the
|
|
41
|
+
// parser must not guess a type from its `example`.
|
|
42
|
+
const membersParsed = members.map(withKeywordType).map((member, index) => stackTrail.trace(`${index}`, st => isRef(member) || hasShapeKeyword(member) || 'anyOf' in member || 'not' in member
|
|
43
|
+
? toSchemaV3({ schema: member, stackTrail: st, context })
|
|
44
|
+
: toUnknown({ value: member, stackTrail: st, context })));
|
|
45
|
+
return context.withStackTrail(stackTrail, () => new OasIntersection({
|
|
46
|
+
title,
|
|
47
|
+
description,
|
|
48
|
+
externalDocs: stackTrail.trace('externalDocs', st => toExternalDocs({ externalDocs, stackTrail: st, context })),
|
|
49
|
+
nullable,
|
|
50
|
+
readOnly,
|
|
51
|
+
writeOnly,
|
|
52
|
+
deprecated,
|
|
53
|
+
default: defaultValue,
|
|
54
|
+
members: membersParsed,
|
|
55
|
+
example,
|
|
56
|
+
extensionFields
|
|
57
|
+
}, context));
|
|
58
|
+
};
|
|
59
|
+
// A value matches every member, so without resolving them the most we can
|
|
60
|
+
// check is that a present default/example is not `null` on a non-nullable schema.
|
|
61
|
+
const isPresent = (value) => value !== null && value !== undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ParseContextType } from '../../../context/parseTypes.js';
|
|
2
|
+
import type { StackTrail } from '../../../context/StackTrail.js';
|
|
3
|
+
import type { ComponentsEntry } from '../../../oas/components/Components.js';
|
|
4
|
+
import type { OasRefData } from '../../../oas/ref/ref-types.js';
|
|
5
|
+
/** What {@link toRefTargetV3} parses. */
|
|
6
|
+
export type ToRefTargetArgs = {
|
|
7
|
+
/** The kind of component the ref is written for. */
|
|
8
|
+
refType: OasRefData['refType'];
|
|
9
|
+
/** The last segment of the target's pointer, unescaped: the key it parses under. */
|
|
10
|
+
name: string;
|
|
11
|
+
/** The raw value at the pointer. */
|
|
12
|
+
value: Record<string, unknown>;
|
|
13
|
+
/** The trail of the target's parent, so the target parses at its own position. */
|
|
14
|
+
stackTrail: StackTrail;
|
|
15
|
+
context: ParseContextType;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Parses the target of a ref that does not point at a component
|
|
19
|
+
* (`#/components/schemas/Pet/properties/tag`, `#/paths/~1pets/get/parameters/0`)
|
|
20
|
+
* at its own position, with the parser that components of its kind use. Gives
|
|
21
|
+
* `undefined` when the target does not parse (the parser has logged why).
|
|
22
|
+
*/
|
|
23
|
+
export declare const toRefTargetV3: ({ refType, name, value, stackTrail, context }: ToRefTargetArgs) => ComponentsEntry | undefined;
|