Fix a bug where ES sends a string and migrations expect a boolean (#23313)

This commit is contained in:
Chris Davies 2018-09-20 11:54:54 -04:00 committed by GitHub
parent 63d0824991
commit aac10b0eb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View file

@ -63,7 +63,30 @@ describe('determineMigrationAction', () => {
doc: {
dynamic: 'strict',
properties: {
hello: { dynamic: true, goober: 'pea' },
hello: { dynamic: 'true', goober: 'pea' },
world: { baz: 'bing' },
},
},
};
expect(determineMigrationAction(actual, expected)).toEqual(MigrationAction.None);
});
test('requires no action if mappings differ only by equivalent coerced properties', () => {
const actual = {
doc: {
dynamic: 'strict',
properties: {
hello: { dynamic: 'false', baz: '2', foo: 'bar' },
world: { baz: 'bing' },
},
},
};
const expected = {
doc: {
dynamic: 'strict',
properties: {
hello: { dynamic: false, baz: 2, foo: 'bar' },
world: { baz: 'bing' },
},
},

View file

@ -74,7 +74,11 @@ function diffSubProperty(actual: any, expected: any): MigrationAction {
// We have a leaf property, so we do a comparison. A change (e.g. 'text' -> 'keyword')
// should result in a migration.
if (typeof actual !== 'object') {
return _.isEqual(actual, expected) ? MigrationAction.None : MigrationAction.Migrate;
// We perform a string comparison here, because Elasticsearch coerces some primitives
// to string (such as dynamic: true and dynamic: 'true'), so we report a mapping
// equivalency if the string comparison checks out. This does mean that {} === '[object Object]'
// by this logic, but that is an edge case which should not occur in mapping definitions.
return `${actual}` === `${expected}` ? MigrationAction.None : MigrationAction.Migrate;
}
// Recursively compare the sub properties
@ -87,5 +91,5 @@ function diffSubProperty(actual: any, expected: any): MigrationAction {
}
function isDynamic(prop: any) {
return prop.dynamic === true || prop.dynamic === 'true';
return prop && `${prop.dynamic}` === 'true';
}