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

[savedObjects/mappings] limit valid type names #17511

Merged
merged 1 commit into from
Apr 9, 2018
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
5 changes: 5 additions & 0 deletions src/server/mappings/__snapshots__/index_mappings.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`server/mapping/index_mapping constructor includes the pluginId from the extension in the _ error message if defined 1`] = `"Property names _foo registered by plugin abc123 are not allowed to start with an underscore (_)"`;

exports[`server/mapping/index_mapping constructor throws if any of the new properties start with _ 1`] = `"Property names _foo are not allowed to start with an underscore (_)"`;
12 changes: 11 additions & 1 deletion src/server/mappings/index_mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export class IndexMappings {
mappingExtensions.forEach(({ properties, pluginId }) => {
const rootProperties = getRootProperties(this._dsl);


const conflicts = Object.keys(properties)
.filter(key => rootProperties.hasOwnProperty(key));

const illegal = Object.keys(properties)
.filter(key => key.startsWith('_'));

if (conflicts.length) {
const props = formatListAsProse(conflicts);
const owner = pluginId ? `registered by plugin ${pluginId} ` : '';
Expand All @@ -37,6 +39,14 @@ export class IndexMappings {
);
}

if (illegal.length) {
const props = formatListAsProse(illegal);
const owner = pluginId ? `registered by plugin ${pluginId} ` : '';
throw new Error(
`Property name${props.length > 1 ? 's' : ''} ${props} ${owner}are not allowed to start with an underscore (_)`
Copy link

Choose a reason for hiding this comment

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

Is there a reason there's no space between ${owner} and are? Also same thing on line 38.

Copy link

@rhoboat rhoboat Apr 4, 2018

Choose a reason for hiding this comment

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

Oh, lines 36 & 44 have the trailing space. Just feels wrong.

Copy link

Choose a reason for hiding this comment

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

Nevermind, this is fine.

);
}

this._setProperties({
...rootProperties,
...properties
Expand Down
35 changes: 35 additions & 0 deletions src/server/mappings/index_mappings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ describe('server/mapping/index_mapping', function () {
new IndexMappings(initialMapping, extensions);
}).toThrowError(/plugin abc123/);
});

it('throws if any of the new properties start with _', () => {
const initialMapping = {
root: { properties: { foo: 'bar' } }
};
const extensions = [
{
properties: {
_foo: 'bar'
}
}
];

expect(() => {
new IndexMappings(initialMapping, extensions);
}).toThrowErrorMatchingSnapshot();
});

it('includes the pluginId from the extension in the _ error message if defined', () => {
const initialMapping = {
root: { properties: { foo: 'bar' } }
};
const extensions = [
{
pluginId: 'abc123',
properties: {
_foo: 'bar'
}
}
];

expect(() => {
new IndexMappings(initialMapping, extensions);
}).toThrowErrorMatchingSnapshot();
});
});

describe('#getDsl()', () => {
Expand Down