fix(linkageRules): fix an exception when the condition contains a association field (#5037)

This commit is contained in:
Zeke Zhang 2024-08-11 13:23:56 +08:00 committed by GitHub
parent c42a2040e2
commit d6c2cfe602
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 3 deletions

View File

@ -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]

View File

@ -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)) {

View File

@ -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,

View File

@ -44,6 +44,8 @@ export interface VariablesContextType {
options?: {
/** 第一次请求时,需要包含的关系字段 */
appends?: string[];
/** do not request when the association field is empty */
doNotRequest?: boolean;
},
) => Promise<any>;
/**