@scalar/oas-utils 0.1.16 → 0.1.17
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
CHANGED
|
@@ -21,5 +21,9 @@ export declare const getExampleFromSchema: (schema: Record<string, any>, options
|
|
|
21
21
|
* Dynamic values to add to the example.
|
|
22
22
|
*/
|
|
23
23
|
variables?: Record<string, any>;
|
|
24
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Whether to omit empty and optional properties.
|
|
26
|
+
*/
|
|
27
|
+
omitEmptyAndOptionalProperties?: boolean;
|
|
28
|
+
}, level?: number, parentSchema?: Record<string, any>, name?: string) => any;
|
|
25
29
|
//# sourceMappingURL=getExampleFromSchema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../src/getExampleFromSchema.ts"],"names":[],"mappings":"AAsCA;;GAEG;AACH,eAAO,MAAM,oBAAoB,WACvB,OAAO,MAAM,EAAE,GAAG,CAAC,YACjB;IACR;;;QAGI;IACJ,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;IACvB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,MAAM,EAAE,GAAG,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../src/getExampleFromSchema.ts"],"names":[],"mappings":"AAsCA;;GAEG;AACH,eAAO,MAAM,oBAAoB,WACvB,OAAO,MAAM,EAAE,GAAG,CAAC,YACjB;IACR;;;QAGI;IACJ,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;IACvB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,MAAM,EAAE,GAAG,CAAC,CAAA;IAC/B;;OAEG;IACH,8BAA8B,CAAC,EAAE,OAAO,CAAA;CACzC,UACM,MAAM,iBACE,OAAO,MAAM,EAAE,GAAG,CAAC,SAC3B,MAAM,KACZ,GAkSF,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getRequestBodyFromOperation.d.ts","sourceRoot":"","sources":["../src/getRequestBodyFromOperation.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEhE;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,oBAAoB,EAC/B,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"getRequestBodyFromOperation.d.ts","sourceRoot":"","sources":["../src/getRequestBodyFromOperation.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEhE;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,oBAAoB,EAC/B,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA6NrC"}
|
package/dist/index.js
CHANGED
|
@@ -142,7 +142,7 @@ function guessFromFormat(schema, fallback = "") {
|
|
|
142
142
|
};
|
|
143
143
|
return exampleValues[schema.format] ?? fallback;
|
|
144
144
|
}
|
|
145
|
-
const getExampleFromSchema = (schema, options, level = 0) => {
|
|
145
|
+
const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) => {
|
|
146
146
|
if (level > 5) {
|
|
147
147
|
return null;
|
|
148
148
|
}
|
|
@@ -174,17 +174,29 @@ const getExampleFromSchema = (schema, options, level = 0) => {
|
|
|
174
174
|
if (schema.enum !== void 0) {
|
|
175
175
|
return schema.enum[0];
|
|
176
176
|
}
|
|
177
|
+
const isObjectOrArray = schema.type === "object" || schema.type === "array";
|
|
178
|
+
if (!isObjectOrArray && options?.omitEmptyAndOptionalProperties === true) {
|
|
179
|
+
const isRequired = schema.required === true || parentSchema?.required === true || parentSchema?.required?.includes(name ?? schema.name);
|
|
180
|
+
if (!isRequired) {
|
|
181
|
+
return void 0;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
177
184
|
if (schema.type === "object" || schema.properties !== void 0) {
|
|
178
185
|
const response = {};
|
|
179
186
|
if (schema.properties !== void 0) {
|
|
180
|
-
Object.keys(schema.properties).forEach((
|
|
181
|
-
const property = schema.properties[
|
|
187
|
+
Object.keys(schema.properties).forEach((propertyName) => {
|
|
188
|
+
const property = schema.properties[propertyName];
|
|
182
189
|
const propertyXmlTagName = options?.xml ? property.xml?.name : void 0;
|
|
183
|
-
response[propertyXmlTagName ??
|
|
190
|
+
response[propertyXmlTagName ?? propertyName] = getExampleFromSchema(
|
|
184
191
|
property,
|
|
185
192
|
options,
|
|
186
|
-
level + 1
|
|
193
|
+
level + 1,
|
|
194
|
+
schema,
|
|
195
|
+
propertyName
|
|
187
196
|
);
|
|
197
|
+
if (typeof response[propertyXmlTagName ?? propertyName] === "undefined") {
|
|
198
|
+
delete response[propertyXmlTagName ?? propertyName];
|
|
199
|
+
}
|
|
188
200
|
});
|
|
189
201
|
}
|
|
190
202
|
if (schema.anyOf !== void 0) {
|
|
@@ -205,8 +217,8 @@ const getExampleFromSchema = (schema, options, level = 0) => {
|
|
|
205
217
|
Object.assign(
|
|
206
218
|
response,
|
|
207
219
|
...schema.allOf.map(
|
|
208
|
-
(item) => getExampleFromSchema(item, options, level + 1)
|
|
209
|
-
)
|
|
220
|
+
(item) => getExampleFromSchema(item, options, level + 1, schema)
|
|
221
|
+
).filter((item) => item !== void 0)
|
|
210
222
|
);
|
|
211
223
|
}
|
|
212
224
|
if (schema.additionalProperties !== void 0 && schema.additionalProperties !== false) {
|
|
@@ -228,13 +240,18 @@ const getExampleFromSchema = (schema, options, level = 0) => {
|
|
|
228
240
|
if (additionalSchema === null) {
|
|
229
241
|
return null;
|
|
230
242
|
}
|
|
243
|
+
const additionalProperties = getExampleFromSchema(
|
|
244
|
+
schema.additionalProperties,
|
|
245
|
+
{
|
|
246
|
+
...options,
|
|
247
|
+
// Let’s just add the additionalProperties, even if they are optional.
|
|
248
|
+
omitEmptyAndOptionalProperties: false
|
|
249
|
+
},
|
|
250
|
+
level + 1
|
|
251
|
+
);
|
|
231
252
|
return {
|
|
232
253
|
...response,
|
|
233
|
-
|
|
234
|
-
schema.additionalProperties,
|
|
235
|
-
options,
|
|
236
|
-
level + 1
|
|
237
|
-
)
|
|
254
|
+
...additionalProperties === void 0 ? {} : { "{{key}}": additionalProperties }
|
|
238
255
|
};
|
|
239
256
|
}
|
|
240
257
|
return response;
|
|
@@ -259,8 +276,8 @@ const getExampleFromSchema = (schema, options, level = 0) => {
|
|
|
259
276
|
schema.items[rule]
|
|
260
277
|
);
|
|
261
278
|
const exampleFromRule = schemas.map(
|
|
262
|
-
(item) => getExampleFromSchema(item, options, level + 1)
|
|
263
|
-
);
|
|
279
|
+
(item) => getExampleFromSchema(item, options, level + 1, schema)
|
|
280
|
+
).filter((item) => item !== void 0);
|
|
264
281
|
return wrapItems ? [{ [itemsXmlTagName]: exampleFromRule }] : exampleFromRule;
|
|
265
282
|
}
|
|
266
283
|
}
|
|
@@ -524,7 +541,10 @@ function getRequestBodyFromOperation(operation, selectedExampleKey) {
|
|
|
524
541
|
];
|
|
525
542
|
const example = requestBodyObject?.example ? requestBodyObject?.example : void 0;
|
|
526
543
|
if (mimeType === "application/json") {
|
|
527
|
-
const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
|
|
544
|
+
const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
|
|
545
|
+
mode: "write",
|
|
546
|
+
omitEmptyAndOptionalProperties: true
|
|
547
|
+
}) : null;
|
|
528
548
|
const body = example ?? exampleFromSchema;
|
|
529
549
|
return {
|
|
530
550
|
headers,
|
|
@@ -537,7 +557,8 @@ function getRequestBodyFromOperation(operation, selectedExampleKey) {
|
|
|
537
557
|
if (mimeType === "application/xml") {
|
|
538
558
|
const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
|
|
539
559
|
xml: true,
|
|
540
|
-
mode: "write"
|
|
560
|
+
mode: "write",
|
|
561
|
+
omitEmptyAndOptionalProperties: true
|
|
541
562
|
}) : null;
|
|
542
563
|
return {
|
|
543
564
|
headers,
|
|
@@ -559,7 +580,8 @@ function getRequestBodyFromOperation(operation, selectedExampleKey) {
|
|
|
559
580
|
if (mimeType === "text/plain") {
|
|
560
581
|
const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
|
|
561
582
|
xml: true,
|
|
562
|
-
mode: "write"
|
|
583
|
+
mode: "write",
|
|
584
|
+
omitEmptyAndOptionalProperties: true
|
|
563
585
|
}) : null;
|
|
564
586
|
return {
|
|
565
587
|
headers,
|