[SOR] validation schema: use previous version#156665
[SOR] validation schema: use previous version#156665pgayvallet merged 4 commits intoelastic:mainfrom
Conversation
| private getTypeValidator(type: string): SavedObjectsTypeValidator { | ||
| if (!this.typeValidatorMap[type]) { | ||
| const savedObjectType = this._registry.getType(type); | ||
| this.typeValidatorMap[type] = new SavedObjectsTypeValidator({ | ||
| logger: this._logger.get('type-validator'), | ||
| type, | ||
| validationMap: savedObjectType!.schemas ?? {}, | ||
| defaultVersion: this._migrator.kibanaVersion, | ||
| }); | ||
| } | ||
| return this.typeValidatorMap[type]!; | ||
| } |
There was a problem hiding this comment.
Just some optimization: keep a map of the per-type validators, to reuse the same instance everytime.
| [K in keyof Required<SavedObjectSanitizedDoc>]: Type<SavedObjectSanitizedDoc[K]>; | ||
| }; | ||
|
|
||
| const baseSchema = schema.object<SavedObjectSanitizedDocSchema>({ |
There was a problem hiding this comment.
Another optimization: using a base schema and schema.extend instead of redefining it everytime.
| const docVersion = | ||
| version ?? | ||
| document.typeMigrationVersion ?? | ||
| document.migrationVersion?.[document.type] ?? | ||
| this.defaultVersion; |
There was a problem hiding this comment.
So I started by removing the version parameter altogether, assuming that retrieving the version from the document's typeMigrationVersion or migrationVersion was going to be sufficient.
Then I realized... some teams defined schemas for types that did not register migrations for ages. For example, the latest migration for space is 6.6.0, but it has a schema defined for 8.8.0. And then I understood why the version parameter was here in the first place and added it back.
I kept the logic retrieving the version from the document though, as I suspect it may be useful later for zdt and version cohabitation.
|
Pinging @elastic/kibana-core (Team:Core) |
To be fair, the feature hasn't been really utilized until 8.8 😅 |
|
@elasticmachine merge upstream |
💛 Build succeeded, but was flaky
Failed CI StepsMetrics [docs]Unknown metric groupsESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
## Summary Fix elastic#156423 We were only using the schema for validation if its version was an *exact* match with the current stack version, meaning that any previous schema was ignored (and even introducing a minor was causing the schema to be ignored). This PR addresses it, by always using the closest previous schema available. --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> (cherry picked from commit 0a0d822)
# Backport This will backport the following commits from `main` to `8.8`: - [[SOR] validation schema: use previous version (#156665)](#156665) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Pierre Gayvallet","email":"pierre.gayvallet@elastic.co"},"sourceCommit":{"committedDate":"2023-05-08T07:48:09Z","message":"[SOR] validation schema: use previous version (#156665)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/156423\r\n\r\nWe were only using the schema for validation if its version was an\r\n*exact* match with the current stack version, meaning that any previous\r\nschema was ignored (and even introducing a minor was causing the schema\r\nto be ignored).\r\n\r\nThis PR addresses it, by always using the closest previous schema\r\navailable.\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>","sha":"0a0d82216f46e905bdbf96ba9f790d0ba95adee7","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Core","Feature:Saved Objects","release_note:skip","backport:prev-minor","v8.8.0","v8.9.0"],"number":156665,"url":"https://github.com/elastic/kibana/pull/156665","mergeCommit":{"message":"[SOR] validation schema: use previous version (#156665)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/156423\r\n\r\nWe were only using the schema for validation if its version was an\r\n*exact* match with the current stack version, meaning that any previous\r\nschema was ignored (and even introducing a minor was causing the schema\r\nto be ignored).\r\n\r\nThis PR addresses it, by always using the closest previous schema\r\navailable.\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>","sha":"0a0d82216f46e905bdbf96ba9f790d0ba95adee7"}},"sourceBranch":"main","suggestedTargetBranches":["8.8"],"targetPullRequestStates":[{"branch":"8.8","label":"v8.8.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/156665","number":156665,"mergeCommit":{"message":"[SOR] validation schema: use previous version (#156665)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/156423\r\n\r\nWe were only using the schema for validation if its version was an\r\n*exact* match with the current stack version, meaning that any previous\r\nschema was ignored (and even introducing a minor was causing the schema\r\nto be ignored).\r\n\r\nThis PR addresses it, by always using the closest previous schema\r\navailable.\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>","sha":"0a0d82216f46e905bdbf96ba9f790d0ba95adee7"}}]}] BACKPORT--> Co-authored-by: Pierre Gayvallet <pierre.gayvallet@elastic.co>
Summary
Fix #156423
We were only using the schema for validation if its version was an exact match with the current stack version, meaning that any previous schema was ignored (and even introducing a minor was causing the schema to be ignored).
This PR addresses it, by always using the closest previous schema available.