From d6c2cfe6021ab7c13057477415aedbf21c24ad8e Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Sun, 11 Aug 2024 13:23:56 +0800 Subject: [PATCH] fix(linkageRules): fix an exception when the condition contains a association field (#5037) --- .../src/schema-component/common/utils/uitls.tsx | 4 +++- .../core/client/src/variables/VariablesProvider.tsx | 13 +++++++++++-- .../src/variables/__tests__/useVariables.test.tsx | 12 ++++++++++++ packages/core/client/src/variables/types.ts | 2 ++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/core/client/src/schema-component/common/utils/uitls.tsx b/packages/core/client/src/schema-component/common/utils/uitls.tsx index 2d5b93c776..2d6ddae0f1 100644 --- a/packages/core/client/src/schema-component/common/utils/uitls.tsx +++ b/packages/core/client/src/schema-component/common/utils/uitls.tsx @@ -100,7 +100,9 @@ export const conditionAnalyses = async ({ } const targetVariableName = targetFieldToVariableString(getTargetField(condition)); - const targetValue = variables.parseVariable(targetVariableName, localVariables); + const targetValue = variables.parseVariable(targetVariableName, localVariables, { + doNotRequest: true, + }); const parsingResult = isVariable(jsonlogic?.value) ? [variables.parseVariable(jsonlogic?.value, localVariables), targetValue] diff --git a/packages/core/client/src/variables/VariablesProvider.tsx b/packages/core/client/src/variables/VariablesProvider.tsx index bc765a42eb..93cd3be3fa 100644 --- a/packages/core/client/src/variables/VariablesProvider.tsx +++ b/packages/core/client/src/variables/VariablesProvider.tsx @@ -76,6 +76,8 @@ const VariablesProvider = ({ children }) => { options?: { /** 第一次请求时,需要包含的关系字段 */ appends?: string[]; + /** do not request when the association field is empty */ + doNotRequest?: boolean; }, ) => { const list = variablePath.split('.'); @@ -100,7 +102,7 @@ const VariablesProvider = ({ children }) => { const collectionPrimaryKey = getCollection(collectionName)?.getPrimaryKey(); if (Array.isArray(current)) { const result = current.map((item) => { - if (shouldToRequest(item?.[key]) && item?.[collectionPrimaryKey] != null) { + if (!options?.doNotRequest && shouldToRequest(item?.[key]) && item?.[collectionPrimaryKey] != null) { if (associationField?.target) { const url = `/${collectionName}/${ item[associationField.sourceKey || collectionPrimaryKey] @@ -128,7 +130,12 @@ const VariablesProvider = ({ children }) => { return item?.[key]; }); current = removeThroughCollectionFields(_.flatten(await Promise.all(result)), associationField); - } else if (shouldToRequest(current[key]) && current[collectionPrimaryKey] != null && associationField?.target) { + } else if ( + !options?.doNotRequest && + shouldToRequest(current[key]) && + current[collectionPrimaryKey] != null && + associationField?.target + ) { const url = `/${collectionName}/${ current[associationField.sourceKey || collectionPrimaryKey] }/${key}:${getAction(associationField.type)}`; @@ -228,6 +235,8 @@ const VariablesProvider = ({ children }) => { options?: { /** 第一次请求时,需要包含的关系字段 */ appends?: string[]; + /** do not request when the association field is empty */ + doNotRequest?: boolean; }, ) => { if (!isVariable(str)) { diff --git a/packages/core/client/src/variables/__tests__/useVariables.test.tsx b/packages/core/client/src/variables/__tests__/useVariables.test.tsx index 06186b5d63..69c016e740 100644 --- a/packages/core/client/src/variables/__tests__/useVariables.test.tsx +++ b/packages/core/client/src/variables/__tests__/useVariables.test.tsx @@ -303,6 +303,18 @@ describe('useVariables', () => { }); }); + it('set doNotRequest to true to ensure the result is empty', async () => { + const { result } = renderHook(() => useVariables(), { + wrapper: Providers, + }); + + await waitFor(async () => { + expect(await result.current.parseVariable('{{ $user.belongsToField }}', undefined, { doNotRequest: true })).toBe( + null, + ); + }); + }); + it('long variable path', async () => { const { result } = renderHook(() => useVariables(), { wrapper: Providers, diff --git a/packages/core/client/src/variables/types.ts b/packages/core/client/src/variables/types.ts index 49b8708896..7387a2806b 100644 --- a/packages/core/client/src/variables/types.ts +++ b/packages/core/client/src/variables/types.ts @@ -44,6 +44,8 @@ export interface VariablesContextType { options?: { /** 第一次请求时,需要包含的关系字段 */ appends?: string[]; + /** do not request when the association field is empty */ + doNotRequest?: boolean; }, ) => Promise; /**