Skip to content

Commit

Permalink
Fix issue with single value for metaFields advanced setting (#155811)
Browse files Browse the repository at this point in the history
## Summary

Resolves #94356.

Fixes an issue with the _fields_for_wildcard API when the value for
`metaFields` advanced setting is set to a single value (e.g. `_source`).

### Checklist

- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### Release note

Fixes an issue where having a single value for the advanced setting
`metaFields` would prevent refreshing a data view.
  • Loading branch information
lukasolson committed Apr 27, 2023
1 parent 654287b commit f1360f2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
52 changes: 52 additions & 0 deletions src/plugins/data_views/server/routes/fields_for.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { parseMetaFields } from './fields_for';

describe('_fields_for_wildcard', () => {
describe('parseMetaFields', () => {
it('should throw if receiving a string of comma-separated values', () => {
const value = '_source,_id';
expect(() => parseMetaFields(value)).toThrowErrorMatchingInlineSnapshot(
`"metaFields should be an array of field names, a JSON-stringified array of field names, or a single field name"`
);
});

it('should parse a stringified list of values', () => {
const value = JSON.stringify(['_source', '_id']);
const fields = parseMetaFields(value);
expect(fields).toMatchInlineSnapshot(`
Array [
"_source",
"_id",
]
`);
});

it('should wrap a single value in an array', () => {
const value = '_source';
const fields = parseMetaFields(value);
expect(fields).toMatchInlineSnapshot(`
Array [
"_source",
]
`);
});

it('should return the array if already an array', () => {
const value = ['_source', '_id'];
const fields = parseMetaFields(value);
expect(fields).toMatchInlineSnapshot(`
Array [
"_source",
"_id",
]
`);
});
});
});
26 changes: 18 additions & 8 deletions src/plugins/data_views/server/routes/fields_for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,32 @@ import {
import { IndexPatternsFetcher } from '../fetcher';
import type { DataViewsServerPluginStart, DataViewsServerPluginStartDependencies } from '../types';

const parseMetaFields = (metaFields: string | string[]) => {
let parsedFields: string[] = [];
if (typeof metaFields === 'string') {
parsedFields = JSON.parse(metaFields);
} else {
parsedFields = metaFields;
/**
* Accepts one of the following:
* 1. An array of field names
* 2. A JSON-stringified array of field names
* 3. A single field name (not comma-separated)
* @returns an array of field names
* @param metaFields
*/
export const parseMetaFields = (metaFields: string | string[]): string[] => {
if (Array.isArray(metaFields)) return metaFields;
try {
return JSON.parse(metaFields);
} catch (e) {
if (!metaFields.includes(',')) return [metaFields];
throw new Error(
'metaFields should be an array of field names, a JSON-stringified array of field names, or a single field name'
);
}
return parsedFields;
};

const path = '/api/index_patterns/_fields_for_wildcard';

type IBody = { index_filter?: estypes.QueryDslQueryContainer } | undefined;
interface IQuery {
pattern: string;
meta_fields: string[];
meta_fields: string | string[];
type?: string;
rollup_index?: string;
allow_no_index?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export default function ({ getService }) {
})
.expect(200));

it('accepts single meta_fields query param', () =>
supertest
.get('/api/index_patterns/_fields_for_wildcard')
.query({
pattern: '*',
meta_fields: ['_id'],
})
.expect(200));

it('rejects a comma-separated list of meta_fields', () =>
supertest
.get('/api/index_patterns/_fields_for_wildcard')
Expand Down

0 comments on commit f1360f2

Please sign in to comment.