@scalar/oas-utils 0.4.33 → 0.5.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/CHANGELOG.md +25 -0
- package/dist/entities/cookie/cookie.d.ts +2 -17
- package/dist/entities/cookie/cookie.d.ts.map +1 -1
- package/dist/entities/environment/environment.d.ts +2 -14
- package/dist/entities/environment/environment.d.ts.map +1 -1
- package/dist/entities/shared/utility.d.ts +1 -1
- package/dist/entities/shared/utility.d.ts.map +1 -1
- package/dist/entities/spec/collection.d.ts +76 -501
- package/dist/entities/spec/collection.d.ts.map +1 -1
- package/dist/entities/spec/operation.d.ts +60 -290
- package/dist/entities/spec/operation.d.ts.map +1 -1
- package/dist/entities/spec/parameters.d.ts +33 -50
- package/dist/entities/spec/parameters.d.ts.map +1 -1
- package/dist/entities/spec/request-examples.d.ts +161 -998
- package/dist/entities/spec/request-examples.d.ts.map +1 -1
- package/dist/entities/spec/request-examples.js +11 -6
- package/dist/entities/spec/request-examples.js.map +2 -2
- package/dist/entities/spec/requests.d.ts +102 -595
- package/dist/entities/spec/requests.d.ts.map +1 -1
- package/dist/entities/spec/server.d.ts +12 -156
- package/dist/entities/spec/server.d.ts.map +1 -1
- package/dist/entities/spec/spec-objects.d.ts +37 -229
- package/dist/entities/spec/spec-objects.d.ts.map +1 -1
- package/dist/entities/spec/x-scalar-environments.d.ts +8 -56
- package/dist/entities/spec/x-scalar-environments.d.ts.map +1 -1
- package/dist/entities/spec/x-scalar-secrets.d.ts +2 -14
- package/dist/entities/spec/x-scalar-secrets.d.ts.map +1 -1
- package/dist/entities/workspace/workspace.d.ts +218 -98
- package/dist/entities/workspace/workspace.d.ts.map +1 -1
- package/dist/entities/workspace/workspace.js +4 -5
- package/dist/entities/workspace/workspace.js.map +2 -2
- package/dist/helpers/parse.js +1 -1
- package/dist/helpers/parse.js.map +2 -2
- package/dist/helpers/schema-model.d.ts +3 -3
- package/dist/helpers/schema-model.d.ts.map +1 -1
- package/dist/helpers/schema-model.js.map +1 -1
- package/dist/helpers/security/get-schemes.d.ts +27 -27
- package/dist/helpers/security/get-schemes.d.ts.map +1 -1
- package/dist/migrations/v-2.5.0/migration.d.ts.map +1 -1
- package/dist/migrations/v-2.5.0/migration.js.map +2 -2
- package/dist/spec-getters/get-example-from-schema.d.ts +26 -23
- package/dist/spec-getters/get-example-from-schema.d.ts.map +1 -1
- package/dist/spec-getters/get-example-from-schema.js +330 -174
- package/dist/spec-getters/get-example-from-schema.js.map +2 -2
- package/package.json +9 -9
- package/dist/helpers/operation-to-har/index.d.ts +0 -2
- package/dist/helpers/operation-to-har/index.d.ts.map +0 -1
- package/dist/helpers/operation-to-har/index.js +0 -5
- package/dist/helpers/operation-to-har/index.js.map +0 -7
- package/dist/helpers/operation-to-har/operation-to-har.d.ts +0 -49
- package/dist/helpers/operation-to-har/operation-to-har.d.ts.map +0 -1
- package/dist/helpers/operation-to-har/operation-to-har.js +0 -68
- package/dist/helpers/operation-to-har/operation-to-har.js.map +0 -7
- package/dist/helpers/operation-to-har/process-body.d.ts +0 -12
- package/dist/helpers/operation-to-har/process-body.d.ts.map +0 -1
- package/dist/helpers/operation-to-har/process-body.js +0 -80
- package/dist/helpers/operation-to-har/process-body.js.map +0 -7
- package/dist/helpers/operation-to-har/process-parameters.d.ts +0 -19
- package/dist/helpers/operation-to-har/process-parameters.d.ts.map +0 -1
- package/dist/helpers/operation-to-har/process-parameters.js +0 -261
- package/dist/helpers/operation-to-har/process-parameters.js.map +0 -7
- package/dist/helpers/operation-to-har/process-security-schemes.d.ts +0 -15
- package/dist/helpers/operation-to-har/process-security-schemes.d.ts.map +0 -1
- package/dist/helpers/operation-to-har/process-security-schemes.js +0 -62
- package/dist/helpers/operation-to-har/process-security-schemes.js.map +0 -7
- package/dist/helpers/operation-to-har/process-server-url.d.ts +0 -6
- package/dist/helpers/operation-to-har/process-server-url.d.ts.map +0 -1
- package/dist/helpers/operation-to-har/process-server-url.js +0 -27
- package/dist/helpers/operation-to-har/process-server-url.js.map +0 -7
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { isDefined } from "@scalar/helpers/array/is-defined";
|
|
2
|
+
import { getRaw } from "@scalar/json-magic/magic-proxy";
|
|
2
3
|
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
|
|
4
|
+
import { unpackOverridesProxy } from "@scalar/workspace-store/helpers/overrides-proxy";
|
|
3
5
|
const MAX_LEVELS_DEEP = 10;
|
|
4
6
|
const MAX_PROPERTIES = 10;
|
|
5
7
|
const DEFAULT_ADDITIONAL_PROPERTIES_NAME = "propertyName*";
|
|
@@ -7,9 +9,7 @@ const currentISOString = (/* @__PURE__ */ new Date()).toISOString();
|
|
|
7
9
|
const currentDateString = currentISOString.split("T")[0];
|
|
8
10
|
const currentTimeString = currentISOString.split("T")[1].split(".")[0];
|
|
9
11
|
const genericExampleValues = {
|
|
10
|
-
// 'date-time': '1970-01-01T00:00:00Z',
|
|
11
12
|
"date-time": currentISOString,
|
|
12
|
-
// 'date': '1970-01-01',
|
|
13
13
|
"date": currentDateString,
|
|
14
14
|
"email": "hello@example.com",
|
|
15
15
|
"hostname": "example.com",
|
|
@@ -28,7 +28,6 @@ const genericExampleValues = {
|
|
|
28
28
|
// https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
|
|
29
29
|
"relative-json-pointer": "1/nested/objects",
|
|
30
30
|
// full-time in https://tools.ietf.org/html/rfc3339#section-5.6
|
|
31
|
-
// 'time': '00:00:00Z',
|
|
32
31
|
"time": currentTimeString,
|
|
33
32
|
// either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
|
|
34
33
|
"uri-reference": "../folder",
|
|
@@ -37,210 +36,367 @@ const genericExampleValues = {
|
|
|
37
36
|
"uuid": "123e4567-e89b-12d3-a456-426614174000",
|
|
38
37
|
"object-id": "6592008029c8c3e4dc76256c"
|
|
39
38
|
};
|
|
40
|
-
|
|
41
|
-
if (schema.format === "binary") {
|
|
39
|
+
const guessFromFormat = (schema, makeUpRandomData = false, fallback = "") => {
|
|
40
|
+
if ("type" in schema && schema.type === "string" && "format" in schema && schema.format === "binary") {
|
|
42
41
|
return new File([""], "filename");
|
|
43
42
|
}
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
if (makeUpRandomData && "format" in schema && schema.format) {
|
|
44
|
+
return genericExampleValues[schema.format] ?? fallback;
|
|
45
|
+
}
|
|
46
|
+
return fallback;
|
|
47
|
+
};
|
|
46
48
|
const resultCache = /* @__PURE__ */ new WeakMap();
|
|
47
|
-
|
|
49
|
+
const requiredNamesCache = /* @__PURE__ */ new WeakMap();
|
|
50
|
+
const getRequiredNames = (parentSchema) => {
|
|
51
|
+
if (!parentSchema) {
|
|
52
|
+
return void 0;
|
|
53
|
+
}
|
|
54
|
+
const cached = requiredNamesCache.get(parentSchema);
|
|
55
|
+
if (cached) {
|
|
56
|
+
return cached;
|
|
57
|
+
}
|
|
58
|
+
if ("required" in parentSchema) {
|
|
59
|
+
const required = parentSchema.required;
|
|
60
|
+
if (Array.isArray(required) && required.length > 0) {
|
|
61
|
+
const set = new Set(required);
|
|
62
|
+
requiredNamesCache.set(parentSchema, set);
|
|
63
|
+
return set;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
};
|
|
68
|
+
const cache = (schema, result) => {
|
|
48
69
|
if (typeof result !== "object" || result === null) {
|
|
49
70
|
return result;
|
|
50
71
|
}
|
|
51
|
-
resultCache.set(schema, result);
|
|
72
|
+
resultCache.set(getRaw(unpackOverridesProxy(schema)), result);
|
|
52
73
|
return result;
|
|
53
|
-
}
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
return
|
|
74
|
+
};
|
|
75
|
+
const isComposed = (schema) => !!(schema.allOf || schema.oneOf || schema.anyOf);
|
|
76
|
+
const shouldOmitProperty = (schema, parentSchema, propertyName, options) => {
|
|
77
|
+
if (options?.omitEmptyAndOptionalProperties !== true) {
|
|
78
|
+
return false;
|
|
58
79
|
}
|
|
59
|
-
|
|
60
|
-
|
|
80
|
+
const isContainer = "type" in schema && (schema.type === "object" || schema.type === "array") || isComposed(schema);
|
|
81
|
+
if (isContainer) {
|
|
82
|
+
return false;
|
|
61
83
|
}
|
|
62
|
-
if (
|
|
63
|
-
return
|
|
84
|
+
if ("examples" in schema && Array.isArray(schema.examples) && schema.examples.length > 0 || "example" in schema && schema.example !== void 0 || "default" in schema && schema.default !== void 0 || "const" in schema && schema.const !== void 0 || "enum" in schema && Array.isArray(schema.enum) && schema.enum.length > 0) {
|
|
85
|
+
return false;
|
|
64
86
|
}
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
87
|
+
const name = propertyName ?? schema.title ?? "";
|
|
88
|
+
const requiredNames = getRequiredNames(parentSchema);
|
|
89
|
+
const isRequired = requiredNames ? requiredNames.has(name) : false;
|
|
90
|
+
return !isRequired;
|
|
91
|
+
};
|
|
92
|
+
const mergeExamples = (baseValue, newValue) => {
|
|
93
|
+
if (Array.isArray(baseValue) && Array.isArray(newValue)) {
|
|
94
|
+
return [...baseValue, ...newValue];
|
|
68
95
|
}
|
|
69
|
-
if (
|
|
70
|
-
return
|
|
96
|
+
if (baseValue && typeof baseValue === "object" && newValue && typeof newValue === "object") {
|
|
97
|
+
return { ...baseValue, ...newValue };
|
|
71
98
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
99
|
+
return newValue;
|
|
100
|
+
};
|
|
101
|
+
const handleObjectSchema = (schema, options, level, seen) => {
|
|
102
|
+
const response = {};
|
|
103
|
+
if ("properties" in schema && schema.properties) {
|
|
104
|
+
const propertyNames = Object.keys(schema.properties);
|
|
105
|
+
const limit = level > 3 ? Math.min(MAX_PROPERTIES, propertyNames.length) : propertyNames.length;
|
|
106
|
+
for (let i = 0; i < limit; i++) {
|
|
107
|
+
const propertyName = propertyNames[i];
|
|
108
|
+
const propertySchema = getResolvedRef(schema.properties[propertyName]);
|
|
109
|
+
if (!propertySchema) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const propertyXmlName = options?.xml && "xml" in propertySchema ? propertySchema.xml?.name : void 0;
|
|
113
|
+
const value = getExampleFromSchema(propertySchema, options, {
|
|
114
|
+
level: level + 1,
|
|
115
|
+
parentSchema: schema,
|
|
116
|
+
name: propertyName,
|
|
117
|
+
seen
|
|
118
|
+
});
|
|
119
|
+
if (typeof value !== "undefined") {
|
|
120
|
+
response[propertyXmlName ?? propertyName] = value;
|
|
78
121
|
}
|
|
79
|
-
|
|
122
|
+
}
|
|
123
|
+
if (level > 3 && propertyNames.length > MAX_PROPERTIES) {
|
|
124
|
+
response["..."] = "[Additional Properties Truncated]";
|
|
80
125
|
}
|
|
81
126
|
}
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
127
|
+
if ("patternProperties" in schema && schema.patternProperties) {
|
|
128
|
+
for (const pattern of Object.keys(schema.patternProperties)) {
|
|
129
|
+
const propertySchema = getResolvedRef(schema.patternProperties[pattern]);
|
|
130
|
+
if (!propertySchema) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
response[pattern] = getExampleFromSchema(propertySchema, options, {
|
|
134
|
+
level: level + 1,
|
|
135
|
+
parentSchema: schema,
|
|
136
|
+
name: pattern,
|
|
137
|
+
seen
|
|
138
|
+
});
|
|
139
|
+
}
|
|
87
140
|
}
|
|
88
|
-
if (schema.
|
|
89
|
-
|
|
141
|
+
if ("additionalProperties" in schema && schema.additionalProperties !== void 0) {
|
|
142
|
+
const additional = getResolvedRef(schema.additionalProperties);
|
|
143
|
+
const isAnyType = schema.additionalProperties === true || typeof schema.additionalProperties === "object" && Object.keys(schema.additionalProperties).length === 0;
|
|
144
|
+
const additionalName = typeof additional === "object" && "x-additionalPropertiesName" in additional && typeof additional["x-additionalPropertiesName"] === "string" && additional["x-additionalPropertiesName"].trim().length > 0 ? `${additional["x-additionalPropertiesName"].trim()}*` : DEFAULT_ADDITIONAL_PROPERTIES_NAME;
|
|
145
|
+
response[additionalName] = isAnyType ? "anything" : typeof additional === "object" ? getExampleFromSchema(additional, options, {
|
|
146
|
+
level: level + 1,
|
|
147
|
+
seen
|
|
148
|
+
}) : "anything";
|
|
90
149
|
}
|
|
91
|
-
if (schema.
|
|
92
|
-
|
|
150
|
+
if (schema.oneOf?.[0]) {
|
|
151
|
+
Object.assign(
|
|
152
|
+
response,
|
|
153
|
+
getExampleFromSchema(getResolvedRef(schema.oneOf[0]), options, {
|
|
154
|
+
level: level + 1,
|
|
155
|
+
seen
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
} else if (schema.anyOf?.[0]) {
|
|
159
|
+
Object.assign(
|
|
160
|
+
response,
|
|
161
|
+
getExampleFromSchema(getResolvedRef(schema.anyOf[0]), options, {
|
|
162
|
+
level: level + 1,
|
|
163
|
+
seen
|
|
164
|
+
})
|
|
165
|
+
);
|
|
166
|
+
} else if (Array.isArray(schema.allOf) && schema.allOf.length > 0) {
|
|
167
|
+
let merged = response;
|
|
168
|
+
for (const item of schema.allOf) {
|
|
169
|
+
const ex = getExampleFromSchema(getResolvedRef(item), options, {
|
|
170
|
+
level: level + 1,
|
|
171
|
+
parentSchema: schema,
|
|
172
|
+
seen
|
|
173
|
+
});
|
|
174
|
+
merged = mergeExamples(merged, ex);
|
|
175
|
+
}
|
|
176
|
+
if (merged && typeof merged === "object") {
|
|
177
|
+
Object.assign(response, merged);
|
|
178
|
+
}
|
|
93
179
|
}
|
|
94
|
-
if (
|
|
95
|
-
|
|
180
|
+
if (options?.xml && "xml" in schema && schema.xml?.name && level === 0) {
|
|
181
|
+
const wrapped = {};
|
|
182
|
+
wrapped[schema.xml.name] = response;
|
|
183
|
+
return cache(schema, wrapped);
|
|
96
184
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
185
|
+
return cache(schema, response);
|
|
186
|
+
};
|
|
187
|
+
const handleArraySchema = (schema, options, level, seen) => {
|
|
188
|
+
const items = "items" in schema ? getResolvedRef(schema.items) : void 0;
|
|
189
|
+
const itemsXmlTagName = items && typeof items === "object" && "xml" in items ? items.xml?.name : void 0;
|
|
190
|
+
const wrapItems = !!(options?.xml && "xml" in schema && schema.xml?.wrapped && itemsXmlTagName);
|
|
191
|
+
if (schema.example !== void 0) {
|
|
192
|
+
return cache(schema, wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example);
|
|
103
193
|
}
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const value = getExampleFromSchema(property, options, level + 1, schema, propertyName);
|
|
117
|
-
if (typeof value !== "undefined") {
|
|
118
|
-
response[propertyXmlTagName ?? propertyName] = value;
|
|
119
|
-
propertyCount++;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
if (level > 3 && propertyCount >= MAX_PROPERTIES && propertyNames.length > MAX_PROPERTIES) {
|
|
123
|
-
response["..."] = "[Additional Properties Truncated]";
|
|
194
|
+
if (items && typeof items === "object") {
|
|
195
|
+
if (Array.isArray(items.allOf) && items.allOf.length > 0) {
|
|
196
|
+
const allOf = items.allOf.filter(isDefined);
|
|
197
|
+
const first = getResolvedRef(allOf[0]);
|
|
198
|
+
if (first && typeof first === "object" && "type" in first && first.type === "object") {
|
|
199
|
+
const combined = { type: "object", allOf };
|
|
200
|
+
const merged = getExampleFromSchema(combined, options, {
|
|
201
|
+
level: level + 1,
|
|
202
|
+
parentSchema: schema,
|
|
203
|
+
seen
|
|
204
|
+
});
|
|
205
|
+
return cache(schema, wrapItems ? [{ [itemsXmlTagName]: merged }] : [merged]);
|
|
124
206
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (schema.additionalProperties !== void 0) {
|
|
136
|
-
const anyTypeIsValid = (
|
|
137
|
-
// true
|
|
138
|
-
schema.additionalProperties === true || // or an empty object {}
|
|
139
|
-
typeof schema.additionalProperties === "object" && !Object.keys(schema.additionalProperties).length
|
|
207
|
+
const examples = allOf.map(
|
|
208
|
+
(s) => getExampleFromSchema(getResolvedRef(s), options, {
|
|
209
|
+
level: level + 1,
|
|
210
|
+
parentSchema: schema,
|
|
211
|
+
seen
|
|
212
|
+
})
|
|
213
|
+
).filter(isDefined);
|
|
214
|
+
return cache(
|
|
215
|
+
schema,
|
|
216
|
+
wrapItems ? examples.map((e) => ({ [itemsXmlTagName]: e })) : examples
|
|
140
217
|
);
|
|
141
|
-
const additionalProperties = getResolvedRef(schema.additionalProperties);
|
|
142
|
-
const additionalPropertiesName = typeof additionalProperties === "object" && additionalProperties["x-additionalPropertiesName"] && typeof additionalProperties["x-additionalPropertiesName"] === "string" && additionalProperties["x-additionalPropertiesName"].trim().length > 0 ? `${additionalProperties["x-additionalPropertiesName"].trim()}*` : DEFAULT_ADDITIONAL_PROPERTIES_NAME;
|
|
143
|
-
if (anyTypeIsValid) {
|
|
144
|
-
response[additionalPropertiesName] = "anything";
|
|
145
|
-
} else if (typeof additionalProperties === "object") {
|
|
146
|
-
response[additionalPropertiesName] = getExampleFromSchema(additionalProperties, options, level + 1);
|
|
147
|
-
}
|
|
148
218
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
);
|
|
219
|
+
const union = items.anyOf || items.oneOf;
|
|
220
|
+
if (union && union.length > 0) {
|
|
221
|
+
const first = union[0];
|
|
222
|
+
const ex = getExampleFromSchema(getResolvedRef(first), options, {
|
|
223
|
+
level: level + 1,
|
|
224
|
+
parentSchema: schema,
|
|
225
|
+
seen
|
|
226
|
+
});
|
|
227
|
+
return cache(schema, wrapItems ? [{ [itemsXmlTagName]: ex }] : [ex]);
|
|
158
228
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
229
|
+
}
|
|
230
|
+
const isObject = items && typeof items === "object" && ("type" in items && items.type === "object" || "properties" in items);
|
|
231
|
+
const isArray = items && typeof items === "object" && ("type" in items && items.type === "array" || "items" in items);
|
|
232
|
+
if (items && typeof items === "object" && ("type" in items && items.type || isObject || isArray)) {
|
|
233
|
+
const ex = getExampleFromSchema(items, options, {
|
|
234
|
+
level: level + 1,
|
|
235
|
+
seen
|
|
236
|
+
});
|
|
237
|
+
return cache(schema, wrapItems ? [{ [itemsXmlTagName]: ex }] : [ex]);
|
|
238
|
+
}
|
|
239
|
+
return cache(schema, []);
|
|
240
|
+
};
|
|
241
|
+
const getPrimitiveValue = (schema, makeUpRandomData, emptyString) => {
|
|
242
|
+
if ("type" in schema && schema.type && !Array.isArray(schema.type)) {
|
|
243
|
+
switch (schema.type) {
|
|
244
|
+
case "string":
|
|
245
|
+
return guessFromFormat(schema, makeUpRandomData, emptyString ?? "");
|
|
246
|
+
case "boolean":
|
|
247
|
+
return true;
|
|
248
|
+
case "integer":
|
|
249
|
+
return "minimum" in schema && typeof schema.minimum === "number" ? schema.minimum : 1;
|
|
250
|
+
case "number":
|
|
251
|
+
return "minimum" in schema && typeof schema.minimum === "number" ? schema.minimum : 1;
|
|
252
|
+
case "array":
|
|
253
|
+
return [];
|
|
254
|
+
default:
|
|
255
|
+
return void 0;
|
|
163
256
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return cache(schema, wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example);
|
|
257
|
+
}
|
|
258
|
+
return void 0;
|
|
259
|
+
};
|
|
260
|
+
const getUnionPrimitiveValue = (schema, makeUpRandomData, emptyString) => {
|
|
261
|
+
if ("type" in schema && Array.isArray(schema.type)) {
|
|
262
|
+
if (schema.type.includes("null")) {
|
|
263
|
+
return null;
|
|
172
264
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
265
|
+
const first = schema.type[0];
|
|
266
|
+
if (first) {
|
|
267
|
+
switch (first) {
|
|
268
|
+
case "string":
|
|
269
|
+
return guessFromFormat(schema, makeUpRandomData, emptyString ?? "");
|
|
270
|
+
case "boolean":
|
|
271
|
+
return true;
|
|
272
|
+
case "integer":
|
|
273
|
+
return "minimum" in schema && typeof schema.minimum === "number" ? schema.minimum : 1;
|
|
274
|
+
case "number":
|
|
275
|
+
return "minimum" in schema && typeof schema.minimum === "number" ? schema.minimum : 1;
|
|
276
|
+
case "null":
|
|
277
|
+
return null;
|
|
278
|
+
default:
|
|
279
|
+
return void 0;
|
|
184
280
|
}
|
|
185
|
-
if (items.anyOf || items.oneOf) {
|
|
186
|
-
const ruleItems = items.anyOf || items.oneOf;
|
|
187
|
-
if (ruleItems && ruleItems.length > 0) {
|
|
188
|
-
const firstItem = ruleItems[0];
|
|
189
|
-
if (firstItem) {
|
|
190
|
-
const exampleFromRule = getExampleFromSchema(getResolvedRef(firstItem), options, level + 1, schema);
|
|
191
|
-
return cache(schema, wrapItems ? [{ [itemsXmlTagName]: exampleFromRule }] : [exampleFromRule]);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
const isObject = items?.type === "object" || items?.properties !== void 0;
|
|
197
|
-
const isArray = items?.type === "array" || items?.items !== void 0;
|
|
198
|
-
if (items?.type || isObject || isArray) {
|
|
199
|
-
const exampleFromSchema = getExampleFromSchema(items, options, level + 1);
|
|
200
|
-
return cache(schema, wrapItems ? [{ [itemsXmlTagName]: exampleFromSchema }] : [exampleFromSchema]);
|
|
201
281
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
282
|
+
}
|
|
283
|
+
return void 0;
|
|
284
|
+
};
|
|
285
|
+
const getExampleFromSchema = (schema, options, args) => {
|
|
286
|
+
const { level = 0, parentSchema, name, seen = /* @__PURE__ */ new WeakSet() } = args ?? {};
|
|
287
|
+
const _schema = getResolvedRef(schema);
|
|
288
|
+
if (!isDefined(_schema)) {
|
|
289
|
+
return void 0;
|
|
290
|
+
}
|
|
291
|
+
const targetValue = getRaw(unpackOverridesProxy(_schema));
|
|
292
|
+
if (seen.has(targetValue)) {
|
|
293
|
+
return "[Circular Reference]";
|
|
294
|
+
}
|
|
295
|
+
seen.add(targetValue);
|
|
296
|
+
if (resultCache.has(targetValue)) {
|
|
297
|
+
seen.delete(targetValue);
|
|
298
|
+
return resultCache.get(targetValue);
|
|
299
|
+
}
|
|
300
|
+
if (level > MAX_LEVELS_DEEP) {
|
|
301
|
+
seen.delete(targetValue);
|
|
302
|
+
return "[Max Depth Exceeded]";
|
|
303
|
+
}
|
|
304
|
+
const makeUpRandomData = !!options?.emptyString;
|
|
305
|
+
if (_schema.deprecated || options?.mode === "write" && _schema.readOnly || options?.mode === "read" && _schema.writeOnly || shouldOmitProperty(_schema, parentSchema, name, options)) {
|
|
306
|
+
seen.delete(targetValue);
|
|
307
|
+
return void 0;
|
|
308
|
+
}
|
|
309
|
+
if ("x-variable" in _schema && _schema["x-variable"]) {
|
|
310
|
+
const value = options?.variables?.[_schema["x-variable"]];
|
|
311
|
+
if (value !== void 0) {
|
|
312
|
+
if ("type" in _schema && (_schema.type === "number" || _schema.type === "integer")) {
|
|
313
|
+
seen.delete(targetValue);
|
|
314
|
+
return cache(_schema, Number(value));
|
|
315
|
+
}
|
|
316
|
+
seen.delete(targetValue);
|
|
317
|
+
return cache(_schema, value);
|
|
220
318
|
}
|
|
221
|
-
return cache(schema, null);
|
|
222
|
-
}
|
|
223
|
-
if (Array.isArray(schema.allOf)) {
|
|
224
|
-
let example = null;
|
|
225
|
-
schema.allOf.forEach((allOfItem) => {
|
|
226
|
-
const newExample = getExampleFromSchema(getResolvedRef(allOfItem), options, level + 1);
|
|
227
|
-
example = typeof newExample === "object" && typeof example === "object" ? {
|
|
228
|
-
...example ?? {},
|
|
229
|
-
...newExample
|
|
230
|
-
} : Array.isArray(newExample) && Array.isArray(example) ? [...example ?? {}, ...newExample] : newExample;
|
|
231
|
-
});
|
|
232
|
-
return cache(schema, example);
|
|
233
319
|
}
|
|
234
|
-
if (Array.isArray(
|
|
235
|
-
|
|
236
|
-
|
|
320
|
+
if (Array.isArray(_schema.examples) && _schema.examples.length > 0) {
|
|
321
|
+
seen.delete(targetValue);
|
|
322
|
+
return cache(_schema, _schema.examples[0]);
|
|
323
|
+
}
|
|
324
|
+
if (_schema.example !== void 0) {
|
|
325
|
+
seen.delete(targetValue);
|
|
326
|
+
return cache(_schema, _schema.example);
|
|
327
|
+
}
|
|
328
|
+
if (_schema.default !== void 0) {
|
|
329
|
+
seen.delete(targetValue);
|
|
330
|
+
return cache(_schema, _schema.default);
|
|
331
|
+
}
|
|
332
|
+
if (_schema.const !== void 0) {
|
|
333
|
+
seen.delete(targetValue);
|
|
334
|
+
return cache(_schema, _schema.const);
|
|
335
|
+
}
|
|
336
|
+
if (Array.isArray(_schema.enum) && _schema.enum.length > 0) {
|
|
337
|
+
seen.delete(targetValue);
|
|
338
|
+
return cache(_schema, _schema.enum[0]);
|
|
339
|
+
}
|
|
340
|
+
if ("properties" in _schema || "type" in _schema && _schema.type === "object") {
|
|
341
|
+
const result = handleObjectSchema(_schema, options, level, seen);
|
|
342
|
+
seen.delete(targetValue);
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
if ("type" in _schema && _schema.type === "array" || "items" in _schema) {
|
|
346
|
+
const result = handleArraySchema(_schema, options, level, seen);
|
|
347
|
+
seen.delete(targetValue);
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
const primitive = getPrimitiveValue(_schema, makeUpRandomData, options?.emptyString);
|
|
351
|
+
if (primitive !== void 0) {
|
|
352
|
+
seen.delete(targetValue);
|
|
353
|
+
return cache(_schema, primitive);
|
|
354
|
+
}
|
|
355
|
+
const discriminate = _schema.oneOf || _schema.anyOf;
|
|
356
|
+
if (Array.isArray(discriminate) && discriminate.length > 0) {
|
|
357
|
+
for (const item of discriminate) {
|
|
358
|
+
const resolved = getResolvedRef(item);
|
|
359
|
+
if (resolved && (!("type" in resolved) || resolved.type !== "null")) {
|
|
360
|
+
seen.delete(targetValue);
|
|
361
|
+
return cache(
|
|
362
|
+
_schema,
|
|
363
|
+
getExampleFromSchema(resolved, options, {
|
|
364
|
+
level: level + 1,
|
|
365
|
+
seen
|
|
366
|
+
})
|
|
367
|
+
);
|
|
368
|
+
}
|
|
237
369
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
370
|
+
seen.delete(targetValue);
|
|
371
|
+
return cache(_schema, null);
|
|
372
|
+
}
|
|
373
|
+
if (Array.isArray(_schema.allOf) && _schema.allOf.length > 0) {
|
|
374
|
+
let merged = void 0;
|
|
375
|
+
const items = _schema.allOf;
|
|
376
|
+
for (const item of items) {
|
|
377
|
+
const ex = getExampleFromSchema(item, options, {
|
|
378
|
+
level: level + 1,
|
|
379
|
+
parentSchema: _schema,
|
|
380
|
+
seen
|
|
381
|
+
});
|
|
382
|
+
if (merged === void 0) {
|
|
383
|
+
merged = ex;
|
|
384
|
+
} else if (merged && typeof merged === "object" && ex && typeof ex === "object") {
|
|
385
|
+
merged = mergeExamples(merged, ex);
|
|
386
|
+
} else if (ex !== void 0) {
|
|
387
|
+
merged = ex;
|
|
388
|
+
}
|
|
241
389
|
}
|
|
390
|
+
seen.delete(targetValue);
|
|
391
|
+
return cache(_schema, merged ?? null);
|
|
392
|
+
}
|
|
393
|
+
const unionPrimitive = getUnionPrimitiveValue(_schema, makeUpRandomData, options?.emptyString);
|
|
394
|
+
if (unionPrimitive !== void 0) {
|
|
395
|
+
seen.delete(targetValue);
|
|
396
|
+
return cache(_schema, unionPrimitive);
|
|
242
397
|
}
|
|
243
|
-
|
|
398
|
+
seen.delete(targetValue);
|
|
399
|
+
return cache(_schema, null);
|
|
244
400
|
};
|
|
245
401
|
export {
|
|
246
402
|
getExampleFromSchema
|