fix(collection-manager): infinite recursion (#1608)

* fix(collection-manager): infinite recursion

* fix: ts error
This commit is contained in:
Dunqing 2023-03-29 17:06:33 +08:00 committed by GitHub
parent 92d0eab0cc
commit e6326460b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,17 +88,26 @@ export const useCollectionManager = () => {
type: string | string[] = 'string', type: string | string[] = 'string',
opts?: { opts?: {
cached?: Record<string, any>; cached?: Record<string, any>;
collectionNames?: string[];
/** /**
* true * true
* Array<string> * Array<string>
*/ */
association?: boolean | string[]; association?: boolean | string[];
/**
* Max depth of recursion
*/
maxDepth?: number;
}, },
) => { ) => {
const { association = false, cached = {} } = opts || {}; const { association = false, cached = {}, collectionNames = [collectionName], maxDepth = 1 } = opts || {};
if (collectionNames.length - 1 > maxDepth) {
return;
}
// 防止无限循环
if (cached[collectionName]) { if (cached[collectionName]) {
// avoid infinite recursion
return _.cloneDeep(cached[collectionName]); return _.cloneDeep(cached[collectionName]);
} }
@ -122,7 +131,13 @@ export const useCollectionManager = () => {
...field, ...field,
}; };
if (association && field.target) { if (association && field.target) {
result.children = getCollectionFieldsOptions(field.target, type, { ...opts, cached }); result.children = collectionNames.includes(field.target)
? []
: getCollectionFieldsOptions(field.target, type, {
...opts,
cached,
collectionNames: [...collectionNames, field.target],
});
if (!result.children?.length) { if (!result.children?.length) {
return null; return null;
} }