diff --git a/packages/shared/__tests__/spec.spec.ts b/packages/shared/__tests__/spec.spec.ts index 0efaacc3..3f57bc10 100644 --- a/packages/shared/__tests__/spec.spec.ts +++ b/packages/shared/__tests__/spec.spec.ts @@ -93,5 +93,13 @@ describe('generateDefaultValueFromSpec function', () => { it('can parse any type', () => { expect(generateDefaultValueFromSpec({})).toEqual(''); expect(generateDefaultValueFromSpec({}, true)).toEqual(AnyTypePlaceholder); + expect( + ( + generateDefaultValueFromSpec( + Type.Object({ foo: Type.Object({ bar: Type.Any() }) }), + true + ) as any + ).foo.bar + ).toEqual(AnyTypePlaceholder); }); }); diff --git a/packages/shared/src/utils/spec.ts b/packages/shared/src/utils/spec.ts index 84865571..18d26be0 100644 --- a/packages/shared/src/utils/spec.ts +++ b/packages/shared/src/utils/spec.ts @@ -23,20 +23,31 @@ export function StringUnion(values: [...T], options?: any) { ); } -function getArray(items: JSONSchema7Definition[]): JSONSchema7Type[] { +function getArray( + items: JSONSchema7Definition[], + returnPlaceholderForAny = false +): JSONSchema7Type[] { return items.map(item => - isJSONSchema(item) ? generateDefaultValueFromSpec(item) : null + isJSONSchema(item) + ? generateDefaultValueFromSpec(item, returnPlaceholderForAny) + : null ); } -function getObject(spec: JSONSchema7): JSONSchema7Object { +function getObject( + spec: JSONSchema7, + returnPlaceholderForAny = false +): JSONSchema7Object { const obj: JSONSchema7Object = {}; if (spec.allOf && spec.allOf.length > 0) { - return (getArray(spec.allOf) as JSONSchema7Object[]).reduce((prev, cur) => { - prev = Object.assign(prev, cur); - return prev; - }, obj); + return (getArray(spec.allOf, returnPlaceholderForAny) as JSONSchema7Object[]).reduce( + (prev, cur) => { + prev = Object.assign(prev, cur); + return prev; + }, + obj + ); } for (const key in spec.properties) { @@ -44,7 +55,7 @@ function getObject(spec: JSONSchema7): JSONSchema7Object { if (typeof subSpec === 'boolean') { obj[key] = null; } else if (subSpec) { - obj[key] = generateDefaultValueFromSpec(subSpec); + obj[key] = generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny); } } return obj; @@ -58,10 +69,11 @@ export function generateDefaultValueFromSpec( if ((spec.anyOf && spec.anyOf!.length > 0) || (spec.oneOf && spec.oneOf.length > 0)) { const subSpec = (spec.anyOf! || spec.oneOf)[0]; if (typeof subSpec === 'boolean') return null; - return generateDefaultValueFromSpec(subSpec); + return generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny); } // It is any type + console.log('any', returnPlaceholderForAny); if (returnPlaceholderForAny) { return AnyTypePlaceholder; } @@ -77,7 +89,7 @@ export function generateDefaultValueFromSpec( const subSpec = { type: spec.type[0], } as JSONSchema7; - return generateDefaultValueFromSpec(subSpec); + return generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny); } case spec.type === 'string': if (spec.enum && spec.enum.length > 0) { @@ -90,16 +102,16 @@ export function generateDefaultValueFromSpec( case spec.type === 'array': return spec.items ? Array.isArray(spec.items) - ? getArray(spec.items) + ? getArray(spec.items, returnPlaceholderForAny) : isJSONSchema(spec.items) - ? [generateDefaultValueFromSpec(spec.items)] + ? [generateDefaultValueFromSpec(spec.items, returnPlaceholderForAny)] : null : []; case spec.type === 'number': case spec.type === 'integer': return 0; case spec.type === 'object': - return getObject(spec); + return getObject(spec, returnPlaceholderForAny); case spec.type === 'null': return null; default: