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

[EPM] Handle object type fields with properties in mappings #64745

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,28 @@ test('tests processing object field with dynamic set to strict', () => {
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldDynamicStrictMapping));
});

test('tests processing object field with property', () => {
const objectFieldWithPropertyLiteralYml = `
- name: a
type: object
- name: a.b
type: keyword
`;
const objectFieldWithPropertyMapping = {
properties: {
a: {
properties: {
b: {
ignore_above: 1024,
type: 'keyword',
},
},
},
},
};
const fields: Field[] = safeLoad(objectFieldWithPropertyLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldWithPropertyMapping));
});
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,35 @@ describe('processFields', () => {
JSON.stringify(mixedFieldsExpanded)
);
});

const objectFieldWithProperty = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Will it also work if you have a.b first and then a?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I've added a unit test for that.

{
name: 'a',
type: 'object',
dynamic: true,
},
{
name: 'a.b',
type: 'keyword',
},
];

const objectFieldWithPropertyExpanded = [
{
name: 'a',
type: 'group',
dynamic: true,
fields: [
{
name: 'b',
type: 'keyword',
},
],
},
];
test('correctly handles properties of object type fields', () => {
expect(JSON.stringify(processFields(objectFieldWithProperty))).toEqual(
JSON.stringify(objectFieldWithPropertyExpanded)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ function dedupFields(fields: Fields): Fields {
return f.name === field.name;
});
if (found) {
if (found.type === 'group' && field.type === 'group' && found.fields && field.fields) {
if (
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to have a comment in here that explains why this code is needed (to cover the a, a.b problem).

(found.type === 'group' || found.type === 'object') &&
field.type === 'group' &&
field.fields
) {
if (!found.fields) {
found.fields = [];
}
found.type = 'group';
found.fields = dedupFields(found.fields.concat(field.fields));
} else {
// only 'group' fields can be merged in this way
Expand Down