-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:elastic/kibana into version-apm-apis
- Loading branch information
Showing
36 changed files
with
1,029 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...e-saved-objects-base-server-internal/src/model_version/backward_conversion_schema.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* 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 { schema } from '@kbn/config-schema'; | ||
import { convertModelVersionBackwardConversionSchema } from './backward_conversion_schema'; | ||
import type { | ||
SavedObjectUnsanitizedDoc, | ||
SavedObjectModelVersionForwardCompatibilityFn, | ||
} from '@kbn/core-saved-objects-server'; | ||
|
||
describe('convertModelVersionBackwardConversionSchema', () => { | ||
const createDoc = ( | ||
parts: Partial<SavedObjectUnsanitizedDoc<any>> | ||
): SavedObjectUnsanitizedDoc<any> => ({ | ||
id: 'id', | ||
type: 'type', | ||
attributes: {}, | ||
...parts, | ||
}); | ||
|
||
describe('using functions', () => { | ||
it('converts the schema', () => { | ||
const conversionSchema: jest.MockedFunction<SavedObjectModelVersionForwardCompatibilityFn> = | ||
jest.fn(); | ||
conversionSchema.mockImplementation((attrs) => attrs); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(conversionSchema).toHaveBeenCalledTimes(1); | ||
expect(conversionSchema).toHaveBeenCalledWith({ foo: 'bar' }); | ||
expect(output).toEqual(doc); | ||
}); | ||
|
||
it('returns the document with the updated properties', () => { | ||
const conversionSchema: jest.MockedFunction< | ||
SavedObjectModelVersionForwardCompatibilityFn<any, any> | ||
> = jest.fn(); | ||
conversionSchema.mockImplementation((attrs) => ({ foo: attrs.foo })); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar', hello: 'dolly' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(output).toEqual({ | ||
...doc, | ||
attributes: { foo: 'bar' }, | ||
}); | ||
}); | ||
|
||
it('throws if the function throws', () => { | ||
const conversionSchema: jest.MockedFunction< | ||
SavedObjectModelVersionForwardCompatibilityFn<any, any> | ||
> = jest.fn(); | ||
conversionSchema.mockImplementation(() => { | ||
throw new Error('dang'); | ||
}); | ||
|
||
const doc = createDoc({}); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
expect(() => converted(doc)).toThrowErrorMatchingInlineSnapshot(`"dang"`); | ||
}); | ||
}); | ||
|
||
describe('using config-schemas', () => { | ||
it('converts the schema', () => { | ||
const conversionSchema = schema.object( | ||
{ | ||
foo: schema.maybe(schema.string()), | ||
}, | ||
{ unknowns: 'ignore' } | ||
); | ||
const validateSpy = jest.spyOn(conversionSchema, 'validate'); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(validateSpy).toHaveBeenCalledTimes(1); | ||
expect(validateSpy).toHaveBeenCalledWith({ foo: 'bar' }, {}); | ||
expect(output).toEqual(doc); | ||
}); | ||
|
||
it('returns the document with the updated properties', () => { | ||
const conversionSchema = schema.object( | ||
{ | ||
foo: schema.maybe(schema.string()), | ||
}, | ||
{ unknowns: 'ignore' } | ||
); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar', hello: 'dolly' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(output).toEqual({ | ||
...doc, | ||
attributes: { | ||
foo: 'bar', | ||
}, | ||
}); | ||
}); | ||
|
||
it('throws if the validation throws', () => { | ||
const conversionSchema = schema.object( | ||
{ | ||
foo: schema.maybe(schema.string()), | ||
}, | ||
{ unknowns: 'forbid' } | ||
); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar', hello: 'dolly' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
expect(() => converted(doc)).toThrowErrorMatchingInlineSnapshot( | ||
`"[hello]: definition for this key is missing"` | ||
); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
...s/core-saved-objects-base-server-internal/src/model_version/backward_conversion_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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 { isConfigSchema, type ObjectType } from '@kbn/config-schema'; | ||
import type { | ||
SavedObjectUnsanitizedDoc, | ||
SavedObjectModelVersionForwardCompatibilitySchema, | ||
} from '@kbn/core-saved-objects-server'; | ||
|
||
function isObjectType( | ||
schema: SavedObjectModelVersionForwardCompatibilitySchema | ||
): schema is ObjectType { | ||
return isConfigSchema(schema); | ||
} | ||
|
||
export type ConvertedSchema = (doc: SavedObjectUnsanitizedDoc) => SavedObjectUnsanitizedDoc; | ||
|
||
export const convertModelVersionBackwardConversionSchema = ( | ||
schema: SavedObjectModelVersionForwardCompatibilitySchema | ||
): ConvertedSchema => { | ||
if (isObjectType(schema)) { | ||
return (doc) => { | ||
const attrs = schema.validate(doc.attributes, {}); | ||
return { | ||
...doc, | ||
attributes: attrs, | ||
}; | ||
}; | ||
} else { | ||
return (doc) => { | ||
const attrs = schema(doc.attributes); | ||
return { | ||
...doc, | ||
attributes: attrs, | ||
}; | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.