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

Reapply "[Fleet] Expand subfields of nested objects when generating template (#191730)" (#191897) #192246

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,106 @@ describe('EPM template', () => {
expect(mappings).toEqual(expectedMapping);
});

it('tests processing nested field with subobject, nested field first', () => {
const nestedYaml = `
- name: a
type: nested
include_in_parent: true
- name: a.b
type: group
fields:
- name: c
type: keyword
`;
const expectedMapping = {
properties: {
a: {
include_in_parent: true,
type: 'nested',
properties: {
b: {
properties: {
c: {
ignore_above: 1024,
type: 'keyword',
},
},
},
},
},
},
};
const fields: Field[] = safeLoad(nestedYaml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(mappings).toEqual(expectedMapping);
});

it('tests processing nested field with subfields', () => {
const nestedYaml = `
- name: a
type: nested
include_in_parent: true
fields:
- name: b
type: keyword
`;
const expectedMapping = {
properties: {
a: {
include_in_parent: true,
type: 'nested',
properties: {
b: {
ignore_above: 1024,
type: 'keyword',
},
},
},
},
};
const fields: Field[] = safeLoad(nestedYaml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(mappings).toEqual(expectedMapping);
});

it('tests processing nested field with subobjects', () => {
const nestedYaml = `
- name: a
type: nested
include_in_parent: true
fields:
- name: b
type: group
fields:
- name: c
type: keyword
`;
const expectedMapping = {
properties: {
a: {
include_in_parent: true,
type: 'nested',
properties: {
b: {
properties: {
c: {
ignore_above: 1024,
type: 'keyword',
},
},
},
},
},
},
};
const fields: Field[] = safeLoad(nestedYaml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(mappings).toEqual(expectedMapping);
});

it('tests processing nested leaf field with properties', () => {
const nestedYaml = `
- name: a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,11 @@ function _generateMappings(
fieldProps.subobjects = mappings.subobjects;
}
break;
case 'nested':
case 'group-nested':
fieldProps = {
properties: _generateMappings(
fieldProps = { ...generateNestedProps(field), type: 'nested' };
if (field.fields) {
fieldProps.properties = _generateMappings(
field.fields!,
{
...ctx,
Expand All @@ -524,10 +526,8 @@ function _generateMappings(
: field.name,
},
isIndexModeTimeSeries
).properties,
...generateNestedProps(field),
type: 'nested',
};
).properties;
}
break;
case 'integer':
fieldProps.type = 'long';
Expand Down Expand Up @@ -564,9 +564,6 @@ function _generateMappings(
fieldProps.value = field.value;
}
break;
case 'nested':
fieldProps = { ...fieldProps, ...generateNestedProps(field), type: 'nested' };
break;
case 'array':
// this assumes array fields were validated in an earlier step
// adding an array field with no object_type would result in an error
Expand Down
31 changes: 31 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/fields/field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,37 @@ describe('processFields', () => {
expect(processFields(nested)).toEqual(nestedExpanded);
});

test('correctly handles properties of nested type fields with subfields', () => {
const nested = [
{
name: 'a',
type: 'nested',
dynamic: true,
fields: [
{
name: 'b',
type: 'keyword',
},
],
},
];

const nestedExpanded = [
{
name: 'a',
type: 'nested',
dynamic: true,
fields: [
{
name: 'b',
type: 'keyword',
},
],
},
];
expect(processFields(nested)).toEqual(nestedExpanded);
});

test('correctly handles properties of nested and object type fields together', () => {
const fields = [
{
Expand Down
Loading