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

[Index template] Fix editor should support mappings types #55804

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
0bae3df
Add type metadata to extract mappings definition
sebelga Jan 24, 2020
bfc5226
Add "include_type_name" parameter to server API req
sebelga Jan 24, 2020
892afa9
Return mappings inside type if one has been detected
sebelga Jan 24, 2020
7d5def9
Add "include_type_name" req param to client requests
sebelga Jan 24, 2020
92875e4
Move query param to "sendRequest" query object
sebelga Jan 24, 2020
de48af4
Forward "include_type_name" api call to ES request call
sebelga Jan 24, 2020
028bb28
Fix API integration test
sebelga Jan 24, 2020
ba056e1
Refactor client integration tests: call registerTestBed() from setup
sebelga Jan 28, 2020
001442c
Add strictness to mappings validator to not allow unknown parameters
sebelga Jan 28, 2020
a97722a
Add test to detect custom type with mappings configuration name
sebelga Jan 28, 2020
b9ad4ac
Use "dynamic" in test type detection instead of "_source"
sebelga Jan 28, 2020
1d2a508
Detect mappings type in LoadMappingsProvider
sebelga Jan 28, 2020
3493e7d
Add client integration tests for LoadMappignsProvider
sebelga Jan 28, 2020
0045d0d
Merge remote-tracking branch 'upstream/7.x' into fix/Index-template-e…
sebelga Jan 28, 2020
df98c23
Refactor: change helper func to "doMappingsHaveType"
sebelga Jan 28, 2020
9f281c2
Fix TS issues
sebelga Jan 28, 2020
0f5e083
Merge remote-tracking branch 'upstream/7.x' into fix/Index-template-e…
sebelga Jan 29, 2020
8fa68b5
Address CR changes
sebelga Jan 29, 2020
1cbaf4d
Remove comment about io-ts lib
sebelga Jan 29, 2020
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 @@ -11,3 +11,5 @@ export * from './mappings_editor';
export * from './components/load_mappings';

export { OnUpdateHandler, Types } from './mappings_state';

export { doesMappingsHasType } from './lib';
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ describe('extractMappingsDefinition', () => {
},
};

expect(extractMappingsDefinition(mappings)).toBe(mappings.type2);
expect(extractMappingsDefinition(mappings)).toEqual({
type: 'type2',
mappings: mappings.type2,
});
});

test('should detect that the mappings has one type and return its mapping definition', () => {
Expand All @@ -97,7 +100,10 @@ describe('extractMappingsDefinition', () => {
},
};

expect(extractMappingsDefinition(mappings)).toBe(mappings.myType);
expect(extractMappingsDefinition(mappings)).toEqual({
type: 'myType',
mappings: mappings.myType,
});
});

test('should detect that the mappings has one type at root level', () => {
Expand All @@ -123,6 +129,6 @@ describe('extractMappingsDefinition', () => {
},
};

expect(extractMappingsDefinition(mappings)).toBe(mappings);
expect(extractMappingsDefinition(mappings)).toEqual({ mappings });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {
mappingsConfigurationSchemaKeys,
} from './mappings_validator';

interface MappingsWithType {
type?: string;
mappings: GenericObject;
}

const ALLOWED_PARAMETERS = [...mappingsConfigurationSchemaKeys, 'dynamic_templates', 'properties'];

const isMappingDefinition = (obj: GenericObject): boolean => {
Expand All @@ -32,6 +37,29 @@ const isMappingDefinition = (obj: GenericObject): boolean => {
return isConfigurationValid && isPropertiesValid && isDynamicTemplatesValid;
};

const getMappingsDefinitionWithType = (mappings: GenericObject): MappingsWithType[] => {
if (isMappingDefinition(mappings)) {
// No need to go any further
return [{ mappings }];
}

// At this point there must be one or more type mappings
const typedMappings = Object.entries(mappings).reduce(
(acc: Array<{ type: string; mappings: GenericObject }>, [type, value]) => {
if (isMappingDefinition(value)) {
acc.push({ type, mappings: value as GenericObject });
}
return acc;
},
[]
);

return typedMappings;
};

export const doesMappingsHasType = (mappings: GenericObject = {}): boolean =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think the name doMappingsHaveType or even isTypedMappings would be more grammatically correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For sure 😊

getMappingsDefinitionWithType(mappings).filter(({ type }) => type !== undefined).length > 0;

/**
* 5.x index templates can be created with multiple types.
* e.g.
Expand Down Expand Up @@ -72,19 +100,10 @@ const isMappingDefinition = (obj: GenericObject): boolean => {
*
* @param mappings The mappings object to validate
*/
export const extractMappingsDefinition = (mappings: GenericObject = {}): GenericObject | null => {
if (isMappingDefinition(mappings)) {
// No need to go any further
return mappings;
}

// At this point there must be one or more type mappings
const typedMappings = Object.values(mappings).reduce((acc: GenericObject[], value) => {
if (isMappingDefinition(value)) {
acc.push(value as GenericObject);
}
return acc;
}, []);
export const extractMappingsDefinition = (
mappings: GenericObject = {}
): MappingsWithType | null => {
const typedMappings = getMappingsDefinitionWithType(mappings);

// If there are no typed mappings found this means that one of the type must did not pass
// the "isMappingDefinition()" validation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type TabName = 'fields' | 'advanced' | 'templates';
export const MappingsEditor = React.memo(({ onUpdate, defaultValue, indexSettings }: Props) => {
const [selectedTab, selectTab] = useState<TabName>('fields');

const { parsedDefaultValue, multipleMappingsDeclared } = useMemo(() => {
const { parsedDefaultValue, multipleMappingsDeclared, mappingsType } = useMemo(() => {
const mappingsDefinition = extractMappingsDefinition(defaultValue);

if (mappingsDefinition === null) {
Expand All @@ -48,7 +48,7 @@ export const MappingsEditor = React.memo(({ onUpdate, defaultValue, indexSetting
dynamic_date_formats,
properties = {},
dynamic_templates,
} = mappingsDefinition;
} = mappingsDefinition.mappings;

const parsed = {
configuration: {
Expand All @@ -66,7 +66,11 @@ export const MappingsEditor = React.memo(({ onUpdate, defaultValue, indexSetting
},
};

return { parsedDefaultValue: parsed, multipleMappingsDeclared: false };
return {
parsedDefaultValue: parsed,
multipleMappingsDeclared: false,
mappingsType: mappingsDefinition.type,
};
}, [defaultValue]);

useEffect(() => {
Expand Down Expand Up @@ -108,7 +112,11 @@ export const MappingsEditor = React.memo(({ onUpdate, defaultValue, indexSetting
<MultipleMappingsWarning />
) : (
<IndexSettingsProvider indexSettings={indexSettings}>
<MappingsState onUpdate={onUpdate} defaultValue={parsedDefaultValue!}>
<MappingsState
onUpdate={onUpdate}
defaultValue={parsedDefaultValue!}
mappingsType={mappingsType}
>
{({ state }) => {
const tabToContentMap = {
fields: <DocumentFields />,
Expand Down
Loading