From 466ecd21808f4298b68bf1919f02b28211e2acb1 Mon Sep 17 00:00:00 2001 From: j3rem1e Date: Sat, 12 Aug 2023 18:49:10 +0200 Subject: [PATCH] Improve examples generation - Generate "additionalProperties" examples. Support the vendor extension "x-additionalPropertiesName" - Use the default value if provided - Generate null instead of a (potentially) invalid string if the type is not found (same behavior here as redoc and others) --- src/utils/schema-utils.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/schema-utils.js b/src/utils/schema-utils.js index 93b16953..4e98b466 100644 --- a/src/utils/schema-utils.js +++ b/src/utils/schema-utils.js @@ -231,9 +231,12 @@ export function getSampleValueByType(schemaObj) { if (schemaObj.const) { return schemaObj.const; } + if (schemaObj.default) { + return schemaObj.default; + } const typeValue = Array.isArray(schemaObj.type) ? schemaObj.type[0] : schemaObj.type; if (!typeValue) { - return '?'; + return null; } if (typeValue.match(/^integer|^number/g)) { const multipleOf = Number.isNaN(Number(schemaObj.multipleOf)) ? undefined : Number(schemaObj.multipleOf); @@ -297,7 +300,7 @@ export function getSampleValueByType(schemaObj) { } } // If type cannot be determined - return '?'; + return null; } /* @@ -590,6 +593,11 @@ export function schemaToSampleObj(schema, config = { }) { } obj = mergePropertyExamples(obj, propertyName, schemaToSampleObj(schema.properties[propertyName], config)); } + if (typeof schema.additionalProperties === 'object') { + const propertyName = schema.additionalProperties['x-additionalPropertiesName'] || 'property'; + obj = mergePropertyExamples(obj, `${propertyName}1`, schemaToSampleObj(schema.additionalProperties, config)); + obj = mergePropertyExamples(obj, `${propertyName}2`, schemaToSampleObj(schema.additionalProperties, config)); + } } } else if (schema.type === 'array' || schema.items) { if (schema.items || schema.example) {