vbenjs / vue-vben-admin

A modern vue admin. It is based on Vue3, vite and TypeScript. It's fast!
https://vben.vvbin.cn/
MIT License
22.88k stars 6.24k forks source link

useDataSource merged params does not effect with undefined values #3891

Open doraemonxxx opened 1 month ago

doraemonxxx commented 1 month ago

⚠️ IMPORTANT ⚠️ Please check the following list before proceeding. If you ignore this issue template, your issue will be directly closed.

Describe the bug

In let params all parameters for fetching data will merge recursively using lodash merge. The issue is when you have an object that has a key with a value of undefined, that undefined will not merge.

When dynamically overriding the searchInfo with opt?.searchInfo, the searchInfo object's key with a defined value and opt?.searchInfo with a key value is undefined, resulting in unexpected behavior. This scenario is utilized in business logic where occasionally setting the searchInfo key to undefined is necessary to meet requirements.

Reference

https://github.com/vbenjs/vue-vben-admin/commit/59b309aa7e0049f0ade0b5ee932f146c5bdb8dfc

Reproduction and Expected Output

const customizer = (objValue: any, srcValue: any, key: any, obj: any): void => {
    // Check if the values are different and if the source value is undefined
    if (objValue !== srcValue && (typeof srcValue === 'undefined' || srcValue === undefined)) {
        // Set the object's key to the source value
        obj[key] = srcValue;
    }
};

// Original object with searchInfo
let obj1 = {
    level1: {
        level2: {
            value1: 1,
            value2: 'hello',
            value3: {
                test1: 'hello world',
                test2: 'hello world test2 obj1',
            },
        },
    },
    anotherKey: 'value',
};

// Object with opt?.searchInfo
let obj2 = {
    level1: {
        level2: {
            value1: undefined,
            value2: 'world',
            value3: {
                test1: 'hello world obj2',
                test2: undefined,
            },
        },
    },
    anotherKey: undefined,
};

console.log('params test', mergeWith({}, obj1, obj2, customizer));
{
    "level1": {
        "level2": {
            "value1": undefined,
            "value2": "world",
            "value3": {
                "test1": "hello world obj2",
                "test2": undefined
            }
        }
    },
    "anotherKey": undefined
}

System Info