@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.
Files changed (69) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/entities/cookie/cookie.d.ts +2 -17
  3. package/dist/entities/cookie/cookie.d.ts.map +1 -1
  4. package/dist/entities/environment/environment.d.ts +2 -14
  5. package/dist/entities/environment/environment.d.ts.map +1 -1
  6. package/dist/entities/shared/utility.d.ts +1 -1
  7. package/dist/entities/shared/utility.d.ts.map +1 -1
  8. package/dist/entities/spec/collection.d.ts +76 -501
  9. package/dist/entities/spec/collection.d.ts.map +1 -1
  10. package/dist/entities/spec/operation.d.ts +60 -290
  11. package/dist/entities/spec/operation.d.ts.map +1 -1
  12. package/dist/entities/spec/parameters.d.ts +33 -50
  13. package/dist/entities/spec/parameters.d.ts.map +1 -1
  14. package/dist/entities/spec/request-examples.d.ts +161 -998
  15. package/dist/entities/spec/request-examples.d.ts.map +1 -1
  16. package/dist/entities/spec/request-examples.js +11 -6
  17. package/dist/entities/spec/request-examples.js.map +2 -2
  18. package/dist/entities/spec/requests.d.ts +102 -595
  19. package/dist/entities/spec/requests.d.ts.map +1 -1
  20. package/dist/entities/spec/server.d.ts +12 -156
  21. package/dist/entities/spec/server.d.ts.map +1 -1
  22. package/dist/entities/spec/spec-objects.d.ts +37 -229
  23. package/dist/entities/spec/spec-objects.d.ts.map +1 -1
  24. package/dist/entities/spec/x-scalar-environments.d.ts +8 -56
  25. package/dist/entities/spec/x-scalar-environments.d.ts.map +1 -1
  26. package/dist/entities/spec/x-scalar-secrets.d.ts +2 -14
  27. package/dist/entities/spec/x-scalar-secrets.d.ts.map +1 -1
  28. package/dist/entities/workspace/workspace.d.ts +218 -98
  29. package/dist/entities/workspace/workspace.d.ts.map +1 -1
  30. package/dist/entities/workspace/workspace.js +4 -5
  31. package/dist/entities/workspace/workspace.js.map +2 -2
  32. package/dist/helpers/parse.js +1 -1
  33. package/dist/helpers/parse.js.map +2 -2
  34. package/dist/helpers/schema-model.d.ts +3 -3
  35. package/dist/helpers/schema-model.d.ts.map +1 -1
  36. package/dist/helpers/schema-model.js.map +1 -1
  37. package/dist/helpers/security/get-schemes.d.ts +27 -27
  38. package/dist/helpers/security/get-schemes.d.ts.map +1 -1
  39. package/dist/migrations/v-2.5.0/migration.d.ts.map +1 -1
  40. package/dist/migrations/v-2.5.0/migration.js.map +2 -2
  41. package/dist/spec-getters/get-example-from-schema.d.ts +26 -23
  42. package/dist/spec-getters/get-example-from-schema.d.ts.map +1 -1
  43. package/dist/spec-getters/get-example-from-schema.js +330 -174
  44. package/dist/spec-getters/get-example-from-schema.js.map +2 -2
  45. package/package.json +9 -9
  46. package/dist/helpers/operation-to-har/index.d.ts +0 -2
  47. package/dist/helpers/operation-to-har/index.d.ts.map +0 -1
  48. package/dist/helpers/operation-to-har/index.js +0 -5
  49. package/dist/helpers/operation-to-har/index.js.map +0 -7
  50. package/dist/helpers/operation-to-har/operation-to-har.d.ts +0 -49
  51. package/dist/helpers/operation-to-har/operation-to-har.d.ts.map +0 -1
  52. package/dist/helpers/operation-to-har/operation-to-har.js +0 -68
  53. package/dist/helpers/operation-to-har/operation-to-har.js.map +0 -7
  54. package/dist/helpers/operation-to-har/process-body.d.ts +0 -12
  55. package/dist/helpers/operation-to-har/process-body.d.ts.map +0 -1
  56. package/dist/helpers/operation-to-har/process-body.js +0 -80
  57. package/dist/helpers/operation-to-har/process-body.js.map +0 -7
  58. package/dist/helpers/operation-to-har/process-parameters.d.ts +0 -19
  59. package/dist/helpers/operation-to-har/process-parameters.d.ts.map +0 -1
  60. package/dist/helpers/operation-to-har/process-parameters.js +0 -261
  61. package/dist/helpers/operation-to-har/process-parameters.js.map +0 -7
  62. package/dist/helpers/operation-to-har/process-security-schemes.d.ts +0 -15
  63. package/dist/helpers/operation-to-har/process-security-schemes.d.ts.map +0 -1
  64. package/dist/helpers/operation-to-har/process-security-schemes.js +0 -62
  65. package/dist/helpers/operation-to-har/process-security-schemes.js.map +0 -7
  66. package/dist/helpers/operation-to-har/process-server-url.d.ts +0 -6
  67. package/dist/helpers/operation-to-har/process-server-url.d.ts.map +0 -1
  68. package/dist/helpers/operation-to-har/process-server-url.js +0 -27
  69. 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
- function guessFromFormat(schema, makeUpRandomData = false, fallback = "") {
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
- return makeUpRandomData ? genericExampleValues[schema.format ?? ""] ?? fallback : "";
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
- function cache(schema, result) {
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 getExampleFromSchema = (_schema, options, level = 0, parentSchema, name) => {
55
- const schema = getResolvedRef(_schema);
56
- if (!isDefined(schema)) {
57
- return void 0;
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
- if (resultCache.has(schema)) {
60
- return resultCache.get(schema);
80
+ const isContainer = "type" in schema && (schema.type === "object" || schema.type === "array") || isComposed(schema);
81
+ if (isContainer) {
82
+ return false;
61
83
  }
62
- if (level > MAX_LEVELS_DEEP) {
63
- return "[Max Depth Exceeded]";
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 makeUpRandomData = !!options?.emptyString;
66
- if (schema.deprecated) {
67
- return void 0;
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 (options?.mode === "write" && schema.readOnly || options?.mode === "read" && schema.writeOnly) {
70
- return void 0;
96
+ if (baseValue && typeof baseValue === "object" && newValue && typeof newValue === "object") {
97
+ return { ...baseValue, ...newValue };
71
98
  }
72
- if (schema["x-variable"]) {
73
- const value = options?.variables?.[schema["x-variable"]];
74
- if (value !== void 0) {
75
- if (schema.type === "number" || schema.type === "integer") {
76
- const numValue = Number.parseInt(value, 10);
77
- return cache(schema, numValue);
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
- return cache(schema, value);
122
+ }
123
+ if (level > 3 && propertyNames.length > MAX_PROPERTIES) {
124
+ response["..."] = "[Additional Properties Truncated]";
80
125
  }
81
126
  }
82
- if (Array.isArray(schema.examples) && schema.examples.length > 0) {
83
- return cache(schema, schema.examples[0]);
84
- }
85
- if (schema.example !== void 0) {
86
- return cache(schema, schema.example);
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.default !== void 0) {
89
- return cache(schema, schema.default);
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.const !== void 0) {
92
- return cache(schema, schema.const);
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 (Array.isArray(schema.enum) && schema.enum.length > 0) {
95
- return cache(schema, schema.enum[0]);
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
- const isObjectOrArray = schema.type === "object" || schema.type === "array" || !!schema.allOf?.at?.(0) || !!schema.anyOf?.at?.(0) || !!schema.oneOf?.at?.(0);
98
- if (!isObjectOrArray && options?.omitEmptyAndOptionalProperties === true) {
99
- const isRequired = schema.required === true || parentSchema?.required === true || parentSchema?.required?.includes(name ?? schema.title ?? "");
100
- if (!isRequired) {
101
- return void 0;
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 (schema.type === "object" || schema.properties !== void 0) {
105
- const response = {};
106
- let propertyCount = 0;
107
- if (schema.properties !== void 0) {
108
- const propertyNames = Object.keys(schema.properties);
109
- const maxProps = level > 3 ? MAX_PROPERTIES : propertyNames.length;
110
- for (const propertyName of propertyNames.slice(0, maxProps)) {
111
- const property = getResolvedRef(schema.properties[propertyName]);
112
- if (!property) {
113
- continue;
114
- }
115
- const propertyXmlTagName = options?.xml ? property?.xml?.name : void 0;
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
- if (schema.patternProperties !== void 0) {
127
- for (const pattern of Object.keys(schema.patternProperties)) {
128
- const property = getResolvedRef(schema.patternProperties[pattern]);
129
- if (!property) {
130
- continue;
131
- }
132
- response[pattern] = getExampleFromSchema(property, options, level + 1, schema, pattern);
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
- if (schema.anyOf?.[0]) {
150
- Object.assign(response, getExampleFromSchema(getResolvedRef(schema.anyOf[0]), options, level + 1));
151
- } else if (schema.oneOf?.[0] !== void 0) {
152
- Object.assign(response, getExampleFromSchema(getResolvedRef(schema.oneOf[0]), options, level + 1));
153
- } else if (schema.allOf?.[0]) {
154
- Object.assign(
155
- response,
156
- ...schema.allOf.filter(isDefined).map((item) => getExampleFromSchema(getResolvedRef(item), options, level + 1, schema))
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
- if (options?.xml && schema.xml?.name && level === 0) {
160
- const wrappedResponse = {};
161
- wrappedResponse[schema.xml.name] = response;
162
- return cache(schema, wrappedResponse);
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
- return cache(schema, response);
165
- }
166
- if (schema.type === "array" || schema.items !== void 0) {
167
- const items = getResolvedRef(schema.items);
168
- const itemsXmlTagName = items?.xml?.name;
169
- const wrapItems = !!(options?.xml && schema.xml?.wrapped && itemsXmlTagName);
170
- if (schema.example !== void 0) {
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
- if (items) {
174
- if (items.allOf) {
175
- const allOf = items.allOf.filter(isDefined);
176
- const firstItem = getResolvedRef(allOf[0]);
177
- if (firstItem?.type === "object") {
178
- const combined = { type: "object", allOf };
179
- const mergedExample = getExampleFromSchema(combined, options, level + 1, schema);
180
- return cache(schema, wrapItems ? [{ [itemsXmlTagName]: mergedExample }] : [mergedExample]);
181
- }
182
- const examples = allOf.map((item) => getExampleFromSchema(getResolvedRef(item), options, level + 1, schema)).filter(isDefined);
183
- return cache(schema, wrapItems ? examples.map((example) => ({ [itemsXmlTagName]: example })) : examples);
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
- return cache(schema, []);
203
- }
204
- const exampleValues = {
205
- string: guessFromFormat(schema, makeUpRandomData, options?.emptyString),
206
- boolean: true,
207
- integer: schema.minimum ?? 1,
208
- number: schema.minimum ?? 1,
209
- array: []
210
- };
211
- if (schema.type && !Array.isArray(schema.type) && exampleValues[schema.type] !== void 0) {
212
- return cache(schema, exampleValues[schema.type]);
213
- }
214
- const discriminateSchema = schema.oneOf || schema.anyOf;
215
- if (Array.isArray(discriminateSchema) && discriminateSchema.length > 0) {
216
- const firstNonNullItem = discriminateSchema.map((item) => getResolvedRef(item)).find((item) => item.type !== "null");
217
- if (firstNonNullItem) {
218
- const result = getExampleFromSchema(firstNonNullItem, options, level + 1);
219
- return cache(schema, result);
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(schema.type)) {
235
- if (schema.type.includes("null")) {
236
- return cache(schema, null);
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
- const exampleValue = exampleValues[schema.type[0] ?? ""];
239
- if (exampleValue !== void 0) {
240
- return cache(schema, exampleValue);
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
- return cache(schema, null);
398
+ seen.delete(targetValue);
399
+ return cache(_schema, null);
244
400
  };
245
401
  export {
246
402
  getExampleFromSchema