Skip to content
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

Merged
merged 4 commits into from
May 8, 2023

Conversation

pgayvallet
Copy link
Contributor

@pgayvallet pgayvallet commented May 4, 2023

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.

@pgayvallet pgayvallet added Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc backport:prev-minor Backport to (9.0) the previous minor version (i.e. one version back from main) v8.8.0 v8.9.0 release_note:skip Skip the PR/issue when compiling release notes Feature:Saved Objects labels May 4, 2023
Copy link
Contributor Author

@pgayvallet pgayvallet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self-review

Comment on lines +2773 to +2784
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]!;
}
Copy link
Contributor Author

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>({
Copy link
Contributor Author

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.

Comment on lines +49 to +53
const docVersion =
version ??
document.typeMigrationVersion ??
document.migrationVersion?.[document.type] ??
this.defaultVersion;
Copy link
Contributor Author

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.

@pgayvallet pgayvallet marked this pull request as ready for review May 4, 2023 14:02
@pgayvallet pgayvallet requested a review from a team as a code owner May 4, 2023 14:02
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

Copy link
Contributor

@TinaHeiligers TinaHeiligers left a 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.

Copy link
Contributor

@TinaHeiligers TinaHeiligers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pgayvallet
Copy link
Contributor Author

It's surprising we haven't come across issues until now.

To be fair, the feature hasn't been really utilized until 8.8 😅

@pgayvallet
Copy link
Contributor Author

@elasticmachine merge upstream

@pgayvallet pgayvallet enabled auto-merge (squash) May 8, 2023 06:14
@kibana-ci
Copy link
Collaborator

kibana-ci commented May 8, 2023

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

Unknown metric groups

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
securitySolution 398 401 +3
total +5

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
securitySolution 478 481 +3
total +5

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.8

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request May 8, 2023
## 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)
kibanamachine added a commit that referenced this pull request May 8, 2023
# 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (9.0) the previous minor version (i.e. one version back from main) Feature:Saved Objects release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v8.8.0 v8.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Previous versionSaved Object schemas are not used to validate objects in later versions
5 participants