Skip to content

Commit

Permalink
Reapply "[Fleet] Expand subfields of nested objects when generating t…
Browse files Browse the repository at this point in the history
…emplate (#191730)" (#191897) (#192246)

This change was released at the end in 8.15.1, so let's keep it the
branch.
    
There are definitions of nested objects whose fields are defined as
subfields, like this:
```
  - name: a
    type: nested
    fields:
    - name: b
      type: keyword
```
This should generate a template with the subfields as subproperties:
```
      "properties": {
        ...
        "a": {
          "type": "nested",
          "properties": {
            "b": {
              "ignore_above": 1024,
              "type": "keyword",
            },
          },
        },
      },
```
This change adds support for this. Without it the nested object is
empty, without subfields, what is unexpected.

See elastic/package-spec#784 for more context.

This change was originally reverted in 8.15 in  #191897

Release notes were manually added in elastic/ingest-docs#1292
  • Loading branch information
jsoriano authored Sep 6, 2024
1 parent 78f8baf commit fdbaff1
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 9 deletions.
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

0 comments on commit fdbaff1

Please sign in to comment.