Skip to content

Commit 4202cb4

Browse files
authored
[7.x] [Index Patterns] Fix return saved index pattern object (#101051) (#101134)
* [Index Patterns] Fix return saved index pattern object (#101051) * update docs
1 parent abaa442 commit 4202cb4

File tree

12 files changed

+143
-164
lines changed

12 files changed

+143
-164
lines changed

docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern.getaggregationrestrictions.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@
88

99
```typescript
1010
getAggregationRestrictions(): Record<string, Record<string, {
11-
agg?: string | undefined;
11+
agg?: string | undefined; /**
12+
* Get last saved saved object fields
13+
*/
1214
interval?: number | undefined;
1315
fixed_interval?: string | undefined;
1416
calendar_interval?: string | undefined;
15-
delay?: string | undefined; /**
17+
delay?: string | undefined;
18+
time_zone?: string | undefined; /**
1619
* Reset last saved saved object fields. used after saving
1720
*/
18-
time_zone?: string | undefined;
1921
}>> | undefined;
2022
```
2123
<b>Returns:</b>
2224

2325
`Record<string, Record<string, {
24-
agg?: string | undefined;
26+
agg?: string | undefined; /**
27+
* Get last saved saved object fields
28+
*/
2529
interval?: number | undefined;
2630
fixed_interval?: string | undefined;
2731
calendar_interval?: string | undefined;
28-
delay?: string | undefined; /**
32+
delay?: string | undefined;
33+
time_zone?: string | undefined; /**
2934
* Reset last saved saved object fields. used after saving
3035
*/
31-
time_zone?: string | undefined;
3236
}>> | undefined`
3337

docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern.getassavedobjectbody.md

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,9 @@ Returns index pattern as saved object body for saving
99
<b>Signature:</b>
1010

1111
```typescript
12-
getAsSavedObjectBody(): {
13-
fieldAttrs: string | undefined;
14-
title: string;
15-
timeFieldName: string | undefined;
16-
intervalName: string | undefined;
17-
sourceFilters: string | undefined;
18-
fields: string | undefined;
19-
fieldFormatMap: string | undefined;
20-
type: string | undefined;
21-
typeMeta: string | undefined;
22-
allowNoIndex: true | undefined;
23-
runtimeFieldMap: string | undefined;
24-
};
12+
getAsSavedObjectBody(): IndexPatternAttributes;
2513
```
2614
<b>Returns:</b>
2715

28-
`{
29-
fieldAttrs: string | undefined;
30-
title: string;
31-
timeFieldName: string | undefined;
32-
intervalName: string | undefined;
33-
sourceFilters: string | undefined;
34-
fields: string | undefined;
35-
fieldFormatMap: string | undefined;
36-
type: string | undefined;
37-
typeMeta: string | undefined;
38-
allowNoIndex: true | undefined;
39-
runtimeFieldMap: string | undefined;
40-
}`
16+
`IndexPatternAttributes`
4117

docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpattern.getaggregationrestrictions.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@
88

99
```typescript
1010
getAggregationRestrictions(): Record<string, Record<string, {
11-
agg?: string | undefined;
11+
agg?: string | undefined; /**
12+
* Get last saved saved object fields
13+
*/
1214
interval?: number | undefined;
1315
fixed_interval?: string | undefined;
1416
calendar_interval?: string | undefined;
15-
delay?: string | undefined; /**
17+
delay?: string | undefined;
18+
time_zone?: string | undefined; /**
1619
* Reset last saved saved object fields. used after saving
1720
*/
18-
time_zone?: string | undefined;
1921
}>> | undefined;
2022
```
2123
<b>Returns:</b>
2224

2325
`Record<string, Record<string, {
24-
agg?: string | undefined;
26+
agg?: string | undefined; /**
27+
* Get last saved saved object fields
28+
*/
2529
interval?: number | undefined;
2630
fixed_interval?: string | undefined;
2731
calendar_interval?: string | undefined;
28-
delay?: string | undefined; /**
32+
delay?: string | undefined;
33+
time_zone?: string | undefined; /**
2934
* Reset last saved saved object fields. used after saving
3035
*/
31-
time_zone?: string | undefined;
3236
}>> | undefined`
3337

docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpattern.getassavedobjectbody.md

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,9 @@ Returns index pattern as saved object body for saving
99
<b>Signature:</b>
1010

1111
```typescript
12-
getAsSavedObjectBody(): {
13-
fieldAttrs: string | undefined;
14-
title: string;
15-
timeFieldName: string | undefined;
16-
intervalName: string | undefined;
17-
sourceFilters: string | undefined;
18-
fields: string | undefined;
19-
fieldFormatMap: string | undefined;
20-
type: string | undefined;
21-
typeMeta: string | undefined;
22-
allowNoIndex: true | undefined;
23-
runtimeFieldMap: string | undefined;
24-
};
12+
getAsSavedObjectBody(): IndexPatternAttributes;
2513
```
2614
<b>Returns:</b>
2715

28-
`{
29-
fieldAttrs: string | undefined;
30-
title: string;
31-
timeFieldName: string | undefined;
32-
intervalName: string | undefined;
33-
sourceFilters: string | undefined;
34-
fields: string | undefined;
35-
fieldFormatMap: string | undefined;
36-
type: string | undefined;
37-
typeMeta: string | undefined;
38-
allowNoIndex: true | undefined;
39-
runtimeFieldMap: string | undefined;
40-
}`
16+
`IndexPatternAttributes`
4117

src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import _, { each, reject } from 'lodash';
10-
import { FieldAttrs, FieldAttrSet } from '../..';
10+
import { FieldAttrs, FieldAttrSet, IndexPatternAttributes } from '../..';
1111
import type { RuntimeField } from '../types';
1212
import { DuplicateField } from '../../../../kibana_utils/common';
1313

@@ -331,7 +331,7 @@ export class IndexPattern implements IIndexPattern {
331331
/**
332332
* Returns index pattern as saved object body for saving
333333
*/
334-
getAsSavedObjectBody() {
334+
getAsSavedObjectBody(): IndexPatternAttributes {
335335
const fieldFormatMap = _.isEmpty(this.fieldFormatMap)
336336
? undefined
337337
: JSON.stringify(this.fieldFormatMap);
@@ -344,12 +344,10 @@ export class IndexPattern implements IIndexPattern {
344344
timeFieldName: this.timeFieldName,
345345
intervalName: this.intervalName,
346346
sourceFilters: this.sourceFilters ? JSON.stringify(this.sourceFilters) : undefined,
347-
fields: this.fields
348-
? JSON.stringify(this.fields.filter((field) => field.scripted))
349-
: undefined,
347+
fields: JSON.stringify(this.fields?.filter((field) => field.scripted) ?? []),
350348
fieldFormatMap,
351-
type: this.type,
352-
typeMeta: this.typeMeta ? JSON.stringify(this.typeMeta) : undefined,
349+
type: this.type!,
350+
typeMeta: JSON.stringify(this.typeMeta ?? {}),
353351
allowNoIndex: this.allowNoIndex ? this.allowNoIndex : undefined,
354352
runtimeFieldMap: runtimeFieldMap ? JSON.stringify(runtimeFieldMap) : undefined,
355353
};

src/plugins/data/common/index_patterns/index_patterns/index_patterns.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,12 @@ describe('IndexPatterns', () => {
231231

232232
test('createAndSave', async () => {
233233
const title = 'kibana-*';
234-
indexPatterns.createSavedObject = jest.fn();
234+
235+
indexPatterns.createSavedObject = jest.fn(() =>
236+
Promise.resolve(({
237+
id: 'id',
238+
} as unknown) as IndexPattern)
239+
);
235240
indexPatterns.setDefault = jest.fn();
236241
await indexPatterns.createAndSave({ title });
237242
expect(indexPatterns.createSavedObject).toBeCalled();

src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ export class IndexPatternsService {
407407
throw new SavedObjectNotFound(savedObjectType, id, 'management/kibana/indexPatterns');
408408
}
409409

410+
return this.initFromSavedObject(savedObject);
411+
};
412+
413+
private initFromSavedObject = async (
414+
savedObject: SavedObject<IndexPatternAttributes>
415+
): Promise<IndexPattern> => {
410416
const spec = this.savedObjectToSpec(savedObject);
411417
const { title, type, typeMeta, runtimeFieldMap } = spec;
412418
spec.fieldAttrs = savedObject.attributes.fieldAttrs
@@ -416,7 +422,7 @@ export class IndexPatternsService {
416422
try {
417423
spec.fields = await this.refreshFieldSpecMap(
418424
spec.fields || {},
419-
id,
425+
savedObject.id,
420426
spec.title as string,
421427
{
422428
pattern: title as string,
@@ -427,6 +433,7 @@ export class IndexPatternsService {
427433
},
428434
spec.fieldAttrs
429435
);
436+
430437
// CREATE RUNTIME FIELDS
431438
for (const [key, value] of Object.entries(runtimeFieldMap || {})) {
432439
// do not create runtime field if mapped field exists
@@ -454,7 +461,7 @@ export class IndexPatternsService {
454461
this.onError(err, {
455462
title: i18n.translate('data.indexPatterns.fetchFieldErrorTitle', {
456463
defaultMessage: 'Error fetching fields for index pattern {title} (ID: {id})',
457-
values: { id, title },
464+
values: { id: savedObject.id, title },
458465
}),
459466
});
460467
}
@@ -548,9 +555,9 @@ export class IndexPatternsService {
548555

549556
async createAndSave(spec: IndexPatternSpec, override = false, skipFetchFields = false) {
550557
const indexPattern = await this.create(spec, skipFetchFields);
551-
await this.createSavedObject(indexPattern, override);
552-
await this.setDefault(indexPattern.id!);
553-
return indexPattern;
558+
const createdIndexPattern = await this.createSavedObject(indexPattern, override);
559+
await this.setDefault(createdIndexPattern.id!);
560+
return createdIndexPattern;
554561
}
555562

556563
/**
@@ -570,15 +577,20 @@ export class IndexPatternsService {
570577
}
571578

572579
const body = indexPattern.getAsSavedObjectBody();
573-
const response = await this.savedObjectsClient.create(savedObjectType, body, {
574-
id: indexPattern.id,
575-
});
576-
indexPattern.id = response.id;
577-
this.indexPatternCache.set(indexPattern.id, Promise.resolve(indexPattern));
580+
const response: SavedObject<IndexPatternAttributes> = (await this.savedObjectsClient.create(
581+
savedObjectType,
582+
body,
583+
{
584+
id: indexPattern.id,
585+
}
586+
)) as SavedObject<IndexPatternAttributes>;
587+
588+
const createdIndexPattern = await this.initFromSavedObject(response);
589+
this.indexPatternCache.set(createdIndexPattern.id!, Promise.resolve(createdIndexPattern));
578590
if (this.savedObjectsCache) {
579591
this.savedObjectsCache.push(response as SavedObject<IndexPatternSavedObjectAttrs>);
580592
}
581-
return indexPattern;
593+
return createdIndexPattern;
582594
}
583595

584596
/**

src/plugins/data/public/public.api.md

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,28 +1330,18 @@ export class IndexPattern implements IIndexPattern {
13301330
};
13311331
// (undocumented)
13321332
getAggregationRestrictions(): Record<string, Record<string, {
1333-
agg?: string | undefined;
1333+
agg?: string | undefined; /**
1334+
* Get last saved saved object fields
1335+
*/
13341336
interval?: number | undefined;
13351337
fixed_interval?: string | undefined;
13361338
calendar_interval?: string | undefined;
1337-
delay?: string | undefined; /**
1339+
delay?: string | undefined;
1340+
time_zone?: string | undefined; /**
13381341
* Reset last saved saved object fields. used after saving
13391342
*/
1340-
time_zone?: string | undefined;
13411343
}>> | undefined;
1342-
getAsSavedObjectBody(): {
1343-
fieldAttrs: string | undefined;
1344-
title: string;
1345-
timeFieldName: string | undefined;
1346-
intervalName: string | undefined;
1347-
sourceFilters: string | undefined;
1348-
fields: string | undefined;
1349-
fieldFormatMap: string | undefined;
1350-
type: string | undefined;
1351-
typeMeta: string | undefined;
1352-
allowNoIndex: true | undefined;
1353-
runtimeFieldMap: string | undefined;
1354-
};
1344+
getAsSavedObjectBody(): IndexPatternAttributes;
13551345
// (undocumented)
13561346
getComputedFields(): {
13571347
storedFields: string[];

src/plugins/data/server/index_patterns/routes/create_index_pattern.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import type { DataPluginStart, DataPluginStartDependencies } from '../../plugin'
1515

1616
const indexPatternSpecSchema = schema.object({
1717
title: schema.string(),
18-
19-
id: schema.maybe(schema.string()),
2018
version: schema.maybe(schema.string()),
19+
id: schema.maybe(schema.string()),
2120
type: schema.maybe(schema.string()),
2221
timeFieldName: schema.maybe(schema.string()),
2322
sourceFilters: schema.maybe(

src/plugins/data/server/server.api.md

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -772,28 +772,18 @@ export class IndexPattern implements IIndexPattern {
772772
};
773773
// (undocumented)
774774
getAggregationRestrictions(): Record<string, Record<string, {
775-
agg?: string | undefined;
775+
agg?: string | undefined; /**
776+
* Get last saved saved object fields
777+
*/
776778
interval?: number | undefined;
777779
fixed_interval?: string | undefined;
778780
calendar_interval?: string | undefined;
779-
delay?: string | undefined; /**
781+
delay?: string | undefined;
782+
time_zone?: string | undefined; /**
780783
* Reset last saved saved object fields. used after saving
781784
*/
782-
time_zone?: string | undefined;
783785
}>> | undefined;
784-
getAsSavedObjectBody(): {
785-
fieldAttrs: string | undefined;
786-
title: string;
787-
timeFieldName: string | undefined;
788-
intervalName: string | undefined;
789-
sourceFilters: string | undefined;
790-
fields: string | undefined;
791-
fieldFormatMap: string | undefined;
792-
type: string | undefined;
793-
typeMeta: string | undefined;
794-
allowNoIndex: true | undefined;
795-
runtimeFieldMap: string | undefined;
796-
};
786+
getAsSavedObjectBody(): IndexPatternAttributes;
797787
// (undocumented)
798788
getComputedFields(): {
799789
storedFields: string[];

0 commit comments

Comments
 (0)