Skip to content

Commit

Permalink
[nodejs client] fix hookup issues in src/core/server/saved_objects
Browse files Browse the repository at this point in the history
1.import Client from '@opensearch-project/opensearch/api/new' to hook
up the new types
2.add support functions. for example, add a support function for
converting a possibly not existant doc to an existant doc.
3.add @ts-expect-error to expected mismatches
4.fix undefined and mismatched types
5.fix unit tests in src/core/server/saved_objects

Issue Resolved: opensearch-project#1216
Partical Resolved: opensearch-project#837

Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Feb 7, 2022
1 parent decd7c1 commit a310437
Show file tree
Hide file tree
Showing 26 changed files with 227 additions and 226 deletions.
2 changes: 0 additions & 2 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ export {
SavedObjectsRepository,
SavedObjectsDeleteByNamespaceOptions,
SavedObjectsIncrementCounterOptions,
SavedObjectsComplexFieldMapping,
SavedObjectsCoreFieldMapping,
SavedObjectsFieldMapping,
SavedObjectsTypeMappingDefinition,
SavedObjectsMappingProperties,
Expand Down
2 changes: 0 additions & 2 deletions src/core/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ export {
} from './service/lib/repository';

export {
SavedObjectsCoreFieldMapping,
SavedObjectsComplexFieldMapping,
SavedObjectsFieldMapping,
SavedObjectsMappingProperties,
SavedObjectsTypeMappingDefinition,
Expand Down
2 changes: 0 additions & 2 deletions src/core/server/saved_objects/mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
*/
export { getTypes, getProperty, getRootProperties, getRootPropertiesObjects } from './lib';
export {
SavedObjectsComplexFieldMapping,
SavedObjectsCoreFieldMapping,
SavedObjectsTypeMappingDefinition,
SavedObjectsTypeMappingDefinitions,
SavedObjectsMappingProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const MAPPINGS = {
},
},
},
};
} as const;

function runTest(key: string | string[], mapping: IndexMapping | SavedObjectsFieldMapping) {
expect(typeof key === 'string' || Array.isArray(key)).toBeTruthy();
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/mappings/lib/get_property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
*/

import { toPath } from 'lodash';
import { SavedObjectsCoreFieldMapping, SavedObjectsFieldMapping, IndexMapping } from '../types';
import { SavedObjectsFieldMapping, IndexMapping } from '../types';

function getPropertyMappingFromObjectMapping(
mapping: IndexMapping | SavedObjectsFieldMapping,
path: string[]
): SavedObjectsFieldMapping | undefined {
const props =
(mapping && (mapping as IndexMapping).properties) ||
(mapping && (mapping as SavedObjectsCoreFieldMapping).fields);
(mapping && (mapping as SavedObjectsFieldMapping).fields);

if (!props) {
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test(`returns single object with properties`, () => {
properties: {},
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({
Expand All @@ -51,7 +51,7 @@ test(`returns single object with type === 'object'`, () => {
type: 'object',
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({
Expand All @@ -71,7 +71,7 @@ test(`returns two objects with properties`, () => {
properties: {},
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({
Expand All @@ -94,7 +94,7 @@ test(`returns two objects with type === 'object'`, () => {
type: 'object',
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({
Expand All @@ -114,7 +114,7 @@ test(`excludes objects without properties and type of keyword`, () => {
type: 'keyword',
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({});
Expand All @@ -130,7 +130,7 @@ test(`excludes two objects without properties and type of keyword`, () => {
type: 'keyword',
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({});
Expand All @@ -146,7 +146,7 @@ test(`includes one object with properties and excludes one object without proper
type: 'keyword',
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({
Expand All @@ -166,7 +166,7 @@ test(`includes one object with type === 'object' and excludes one object without
type: 'keyword',
},
},
};
} as const;

const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({
Expand All @@ -189,7 +189,7 @@ test('excludes references and migrationVersion which are part of the blacklist',
type: 'object',
},
},
};
} as const;
const result = getRootPropertiesObjects(mappings);
expect(result).toEqual({
foo: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/

import {
SavedObjectsComplexFieldMapping,
SavedObjectsFieldMapping,
IndexMapping,
SavedObjectsMappingProperties,
} from '../types';
Expand Down Expand Up @@ -55,7 +55,7 @@ export function getRootPropertiesObjects(mappings: IndexMapping) {
// we consider the existence of the properties or type of object to designate that this is an object datatype
if (
!omittedRootProps.includes(key) &&
((value as SavedObjectsComplexFieldMapping).properties || value.type === 'object')
((value as SavedObjectsFieldMapping).properties || value.type === 'object')
) {
acc[key] = value;
}
Expand Down
58 changes: 15 additions & 43 deletions src/core/server/saved_objects/mappings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* GitHub history for details.
*/

import type { opensearchtypes } from '@opensearch-project/opensearch/api/new';

/**
* Describe a saved object type mapping.
*
Expand Down Expand Up @@ -117,13 +119,22 @@ export interface SavedObjectsMappingProperties {
*
* @public
*/
export type SavedObjectsFieldMapping =
| SavedObjectsCoreFieldMapping
| SavedObjectsComplexFieldMapping;
export type SavedObjectsFieldMapping = opensearchtypes.MappingProperty & {
/**
* The dynamic property of the mapping, either `false` or `'strict'`. If
* unspecified `dynamic: 'strict'` will be inherited from the top-level
* index mappings.
*
* Note: To limit the number of mapping fields Saved Object types should
* *never* use `dynamic: true`.
*/
dynamic?: false | 'strict';
};


/** @internal */
export interface IndexMapping {
dynamic?: string;
dynamic?: boolean | 'strict';
properties: SavedObjectsMappingProperties;
_meta?: IndexMappingMeta;
}
Expand All @@ -135,42 +146,3 @@ export interface IndexMappingMeta {
// the md5 hash of that mapping's value when the index was created.
migrationMappingPropertyHashes?: { [k: string]: string };
}

/**
* See {@link SavedObjectsFieldMapping} for documentation.
*
* @public
*/
export interface SavedObjectsCoreFieldMapping {
type: string;
null_value?: number | boolean | string;
index?: boolean;
doc_values?: boolean;
fields?: {
[subfield: string]: {
type: string;
ignore_above?: number;
};
};
}

/**
* See {@link SavedObjectsFieldMapping} for documentation.
*
* @public
*/
export interface SavedObjectsComplexFieldMapping {
/**
* The dynamic property of the mapping, either `false` or `'strict'`. If
* unspecified `dynamic: 'strict'` will be inherited from the top-level
* index mappings.
*
* Note: To limit the number of mapping fields Saved Object types should
* *never* use `dynamic: true`.
*/
dynamic?: false | 'strict';
enabled?: boolean;
doc_values?: boolean;
type?: string;
properties: SavedObjectsMappingProperties;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ describe('buildActiveMappings', () => {
const properties = {
aaa: { type: 'text' },
bbb: { type: 'long' },
};
} as const;

expect(buildActiveMappings(properties)).toMatchSnapshot();
});

test('disallows duplicate mappings', () => {
const properties = { type: { type: 'long' } };
const properties = { type: { type: 'long' } } as const;

expect(() => buildActiveMappings(properties)).toThrow(/Cannot redefine core mapping \"type\"/);
});

test('disallows mappings with leading underscore', () => {
const properties = { _hm: { type: 'keyword' } };
const properties = { _hm: { type: 'keyword' } } as const;

expect(() => buildActiveMappings(properties)).toThrow(
/Invalid mapping \"_hm\"\. Mappings cannot start with _/
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('buildActiveMappings', () => {
aaa: { type: 'keyword', fields: { a: { type: 'keyword' }, b: { type: 'text' } } },
bbb: { fields: { b: { type: 'text' }, a: { type: 'keyword' } }, type: 'keyword' },
ccc: { fields: { b: { type: 'text' }, a: { type: 'text' } }, type: 'keyword' },
};
} as const;

const mappings = buildActiveMappings(properties);
const hashes = mappings._meta!.migrationMappingPropertyHashes!;
Expand Down Expand Up @@ -183,6 +183,7 @@ describe('diffMappings', () => {
_meta: {
migrationMappingPropertyHashes: { foo: 'bar' },
},
// @ts-expect-error
dynamic: 'abcde',
properties: {},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test('mappings without index pattern goes to default index', () => {
type1: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
Expand All @@ -67,7 +67,7 @@ test('mappings without index pattern goes to default index', () => {
type1: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
Expand All @@ -88,7 +88,7 @@ test(`mappings with custom index pattern doesn't go to default index`, () => {
type1: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
Expand All @@ -100,7 +100,7 @@ test(`mappings with custom index pattern doesn't go to default index`, () => {
type1: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
Expand All @@ -122,7 +122,7 @@ test('creating a script gets added to the index pattern', () => {
type1: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
Expand All @@ -135,7 +135,7 @@ test('creating a script gets added to the index pattern', () => {
type1: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
Expand Down Expand Up @@ -163,18 +163,18 @@ test('throws when two scripts are defined for an index pattern', () => {
type1: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
type2: {
properties: {
field1: {
type: 'string',
type: 'text',
},
},
},
};
} as const;
expect(() =>
createIndexMap({
opensearchDashboardsIndexName: defaultIndex,
Expand Down
12 changes: 3 additions & 9 deletions src/core/server/saved_objects/migrations/core/call_cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
* funcationality contained here.
*/

import { opensearchtypes } from '@opensearch-project/opensearch';
import { IndexMapping } from '../../mappings';

export interface CallCluster {
(path: 'bulk', opts: { body: object[] }): Promise<BulkResult>;
(path: 'count', opts: CountOpts): Promise<{ count: number; _shards: ShardsInfo }>;
(path: 'count', opts: CountOpts): Promise<{ count: number; _shards: opensearchtypes.ShardStatistics }>;
(path: 'clearScroll', opts: { scrollId: string }): Promise<any>;
(path: 'indices.create' | 'indices.delete', opts: IndexCreationOpts): Promise<any>;
(path: 'indices.exists', opts: IndexOpts): Promise<boolean>;
Expand Down Expand Up @@ -172,14 +173,7 @@ export interface SearchResults {
hits: RawDoc[];
};
_scroll_id?: string;
_shards: ShardsInfo;
}

export interface ShardsInfo {
total: number;
successful: number;
skipped: number;
failed: number;
_shards: opensearchtypes.ShardStatistics;
}

export interface ErrorResponse {
Expand Down
Loading

0 comments on commit a310437

Please sign in to comment.