Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent dynamic field overwrite when building dynamic row #401

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions milvus/utils/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ export const buildDynamicRow = (

// iterate through each key in the input data object
for (let key in originRow) {
if (key === dynamicFieldName) {
// ignore the dynamic field key for now
continue;
}
row[dynamicFieldName] = row[dynamicFieldName] || ({} as JSON); // initialize the dynamic field object

if (fieldMap.has(key)) {
Expand All @@ -467,6 +471,13 @@ export const buildDynamicRow = (
}
}

// do this last to prevent any non-deterministic behavior with loop order
if (originRow[dynamicFieldName]) {
// extend the dynamic field object with the input dynamic field object
row[dynamicFieldName] = row[dynamicFieldName] || ({} as JSON);
Object.assign(row[dynamicFieldName] as JSON, originRow[dynamicFieldName]);
}

Comment on lines +474 to +480
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the intended behaviour when the input data is something like:

{
    $meta: {
        key2: "value22",
    },
    key1: "value1",
    key2: "value2",
}

where key2 is a dynamic field not specified in the schema.

return row; // return the generated dynamic row object
};

Expand Down
65 changes: 65 additions & 0 deletions test/utils/Format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,71 @@ describe('utils/format', () => {
});
});

it('should return an object with dynamicField key when data contains keys not in fieldsDataMap and an empty dynamicField object', () => {
const dynamicField = 'dynamic';
const data = { key1: 'value1', key2: 'value2', [dynamicField]: {} };
const fieldsDataMap = new Map([
[
'key1',
{
name: 'key1',
type: 'VarChar',
data: [{ key1: 'value1' }],
} as _Field,
],
[
// this mirrors the dynamic field map in grpc.Data._insert method
dynamicField,
{
name: dynamicField,
type: 'JSON',
data: [],
} as _Field,
],
]);
const result = buildDynamicRow(data, fieldsDataMap, dynamicField, []);
expect(result).toEqual({
key1: 'value1',
[dynamicField]: { key2: 'value2' },
});
});

it('should return an object with dynamicField key when data contains keys not in fieldsDataMap and a non-empty dynamicField object', () => {
const dynamicField = 'dynamic';
const data = {
key1: 'value1',
key2: 'value2',
[dynamicField]: {
key2: 'value22',
key3: 'value3',
},
};
const fieldsDataMap = new Map([
[
'key1',
{
name: 'key1',
type: 'VarChar',
data: [{ key1: 'value1' }],
} as _Field,
],
[
// this mirrors the dynamic field map in grpc.Data._insert method
dynamicField,
{
name: dynamicField,
type: 'JSON',
data: [],
} as _Field,
],
]);
const result = buildDynamicRow(data, fieldsDataMap, dynamicField, []);
expect(result).toEqual({
key1: 'value1',
[dynamicField]: { key2: 'value22', key3: 'value3' },
});
});

it('should return an empty string if no credentials are provided', () => {
const authString = getAuthString({});
expect(authString).toEqual('');
Expand Down