fix: generate array items defaults only when generating state examples

This commit is contained in:
xzdry 2022-09-13 10:09:21 +08:00
parent 8e59b852ec
commit 5c2e03d8f7
2 changed files with 7 additions and 4 deletions

View File

@ -323,7 +323,7 @@ export class ComponentModel implements IComponentModel {
private genStateExample() {
if (!this.spec) return [];
const componentStateSpec = this.spec.spec.state;
let _temp = generateDefaultValueFromSpec(componentStateSpec, true) as Record<
let _temp = generateDefaultValueFromSpec(componentStateSpec, true, true) as Record<
string,
any
>;
@ -336,7 +336,7 @@ export class ComponentModel implements IComponentModel {
_temp[key] = AnyTypePlaceholder;
}
} else {
_temp = merge(_temp, generateDefaultValueFromSpec(t.spec.spec.state, true));
_temp = merge(_temp, generateDefaultValueFromSpec(t.spec.spec.state, true, true));
}
});
this.stateExample = _temp;

View File

@ -72,7 +72,8 @@ function getObject(
export function generateDefaultValueFromSpec(
spec: JSONSchema7,
returnPlaceholderForAny = false
returnPlaceholderForAny = false,
genArrayItemDefaults = false
): JSONSchema7Type {
if (!spec.type) {
if ((spec.anyOf && spec.anyOf!.length > 0) || (spec.oneOf && spec.oneOf.length > 0)) {
@ -112,7 +113,9 @@ export function generateDefaultValueFromSpec(
? Array.isArray(spec.items)
? getArray(spec.items, returnPlaceholderForAny)
: isJSONSchema(spec.items)
? [generateDefaultValueFromSpec(spec.items, returnPlaceholderForAny)]
? genArrayItemDefaults
? [generateDefaultValueFromSpec(spec.items, returnPlaceholderForAny)]
: []
: null
: [];
case spec.type === 'number':