-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SOR] validation schema: use previous version #156665
[SOR] validation schema: use previous version #156665
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self-review
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some optimization: keep a map of the per-type validators, to reuse the same instance everytime.
@@ -19,33 +19,40 @@ type SavedObjectSanitizedDocSchema = { | |||
[K in keyof Required<SavedObjectSanitizedDoc>]: Type<SavedObjectSanitizedDoc[K]>; | |||
}; | |||
|
|||
const baseSchema = schema.object<SavedObjectSanitizedDocSchema>({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's surprising we haven't come across issues until now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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.