Skip to content

Commit

Permalink
Fix null fallbackPropertyName when spec is missing property name. fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Jul 12, 2024
1 parent a3f1577 commit 100f9e6
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/utils/schema-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export function getTypeInfo(parameter, options = { includeNulls: false, enableEx

export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampleIds) {
const example = Array.isArray(schemaObj.examples) ? schemaObj.examples[0] : Object.values(schemaObj.examples || {})[0]?.value ?? schemaObj.example;
if (skipExampleIds && typeof example === 'string' && fallbackPropertyName.match(/id$/i)) { return ''; }
const propertyName = fallbackPropertyName || 'string';
if (skipExampleIds && typeof example === 'string' && propertyName.match(/id$/i)) { return ''; }
if (typeof example !== 'undefined') { return example; }

if (schemaObj.default) { return schemaObj.default; }
Expand Down Expand Up @@ -142,14 +143,14 @@ export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampl
}
if (typeValue.match(/^boolean/g)) { return false; }
if (typeValue.match(/^null/g)) { return null; }
if (skipExampleIds && typeValue.match(/^string/g) && fallbackPropertyName.match(/id$/i)) { return ''; }
if (skipExampleIds && typeValue.match(/^string/g) && propertyName.match(/id$/i)) { return ''; }
if (typeValue.match(/^string/g)) {
if (schemaObj.pattern) {
const examplePattern = schemaObj.pattern.replace(/[+*](?![^\][]*[\]])/g, '{8}').replace(/\{\d*,(\d+)?\}/g, '{8}');
try {
return new RandExp(examplePattern).gen() || fallbackPropertyName || 'string';
return new RandExp(examplePattern).gen() || propertyName;
} catch (error) {
return fallbackPropertyName || 'string';
return propertyName;
}
}
if (schemaObj.format) {
Expand Down Expand Up @@ -185,7 +186,7 @@ export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampl
return schemaObj.format;
}
} else {
return fallbackPropertyName || 'string';
return propertyName;
}
}
// If type cannot be determined
Expand Down

0 comments on commit 100f9e6

Please sign in to comment.