Skip to content

Commit 100f9e6

Browse files
committed
Fix null fallbackPropertyName when spec is missing property name. fix #259.
1 parent a3f1577 commit 100f9e6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/utils/schema-utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ export function getTypeInfo(parameter, options = { includeNulls: false, enableEx
105105

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

111112
if (schemaObj.default) { return schemaObj.default; }
@@ -142,14 +143,14 @@ export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampl
142143
}
143144
if (typeValue.match(/^boolean/g)) { return false; }
144145
if (typeValue.match(/^null/g)) { return null; }
145-
if (skipExampleIds && typeValue.match(/^string/g) && fallbackPropertyName.match(/id$/i)) { return ''; }
146+
if (skipExampleIds && typeValue.match(/^string/g) && propertyName.match(/id$/i)) { return ''; }
146147
if (typeValue.match(/^string/g)) {
147148
if (schemaObj.pattern) {
148149
const examplePattern = schemaObj.pattern.replace(/[+*](?![^\][]*[\]])/g, '{8}').replace(/\{\d*,(\d+)?\}/g, '{8}');
149150
try {
150-
return new RandExp(examplePattern).gen() || fallbackPropertyName || 'string';
151+
return new RandExp(examplePattern).gen() || propertyName;
151152
} catch (error) {
152-
return fallbackPropertyName || 'string';
153+
return propertyName;
153154
}
154155
}
155156
if (schemaObj.format) {
@@ -185,7 +186,7 @@ export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampl
185186
return schemaObj.format;
186187
}
187188
} else {
188-
return fallbackPropertyName || 'string';
189+
return propertyName;
189190
}
190191
}
191192
// If type cannot be determined

0 commit comments

Comments
 (0)