From 2eade35bc714b91aec62c145311ab637bf3445cf Mon Sep 17 00:00:00 2001 From: Rain Date: Thu, 5 Nov 2026 20:05:46 -0800 Subject: [PATCH] [dropshot-api-manager] explain how to fix validation failures on blessed versions When a blessed version fails validation, the only output was the error itself followed by "fix failures, then run generate to update". That doesn't help, since a blessed document can't be regenerated. Attach a help line to validation errors on blessed versions. * In all cases, it says the fix is to add a new API version. * For an older blessed version, it also explains how to skip rules that are newer than that version. Skipping every blessed version, which the current docs suggest, would also skip the latest blessed version, so it isn't what the help recommends. (We would really like the latest version to pass validation, whether or not it is blessed!) --- CHANGELOG.md | 1 + crates/dropshot-api-manager/src/apis.rs | 35 +++++-- crates/dropshot-api-manager/src/output.rs | 24 +++++ crates/dropshot-api-manager/src/resolved.rs | 92 +++++++++++++++++++ crates/integration-tests/src/fixtures.rs | 16 ++++ .../tests/integration/versioned.rs | 57 ++++++++++++ ...ssed_validation_failure_latest_version.txt | 19 ++++ ...ssed_validation_failure_older_versions.txt | 32 +++++++ 8 files changed, 269 insertions(+), 7 deletions(-) create mode 100644 crates/integration-tests/tests/output/integration/blessed_validation_failure_latest_version.txt create mode 100644 crates/integration-tests/tests/output/integration/blessed_validation_failure_older_versions.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d757c0..4425493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - On Unix, errors from Git and Jujutsu commands, including reading Git stub contents, now report `exit code: N` instead of `exit status: N`, matching Windows. - If a blessed version's local files are corrupted, the fixes now mention every corrupted file that is removed or replaced. If both the `.json` and `.json.gitstub` files are corrupted, it is now represented as a single problem. +- When a blessed version fails validation, the check output now includes a `help:` line explaining that blessed documents can't be changed and that the fix is to add a new API version. For older blessed versions, it also explains how to skip particular validation rules, i.e. to only enforce them when `cx.is_latest() || cx.is_blessed() != Some(true)`. - Updated git-stub-vcs to 0.1.1. ## [0.9.0] - 2026-10-30 diff --git a/crates/dropshot-api-manager/src/apis.rs b/crates/dropshot-api-manager/src/apis.rs index 452d8c6..dac1a0a 100644 --- a/crates/dropshot-api-manager/src/apis.rs +++ b/crates/dropshot-api-manager/src/apis.rs @@ -71,11 +71,6 @@ pub struct ManagedApi { fn() -> Result, ApiDescriptionBuildErrors>, /// Extra validation to perform on the OpenAPI document, if any. - /// - /// For versioned APIs, extra validation is performed on *all* versions, - /// including blessed ones. You may want to skip performing validation on - /// blessed versions, though, because they're immutable. To do so, use - /// [`ValidationContext::is_blessed`]. extra_validation: Option>, /// If true, allow trivial changes (doc updates, type renames) for the @@ -222,8 +217,19 @@ impl ManagedApi { /// /// For versioned APIs, extra validation is performed on *all* versions, /// including blessed ones. You may want to skip performing validation on - /// blessed versions, though, because they're immutable. To do so, use - /// [`ValidationContext::is_blessed`]. + /// older blessed versions, though, because they're immutable, while still + /// performing validation on the latest blessed version (and recommending + /// that a new version be created instead). To do so, use the pattern: + /// + /// ``` + /// # use dropshot_api_manager_types::ValidationContext; + /// # use openapiv3::OpenAPI; + /// fn validate(doc: &OpenAPI, cx: ValidationContext<'_>) { + /// if cx.is_latest() || cx.is_blessed() != Some(true) { + /// // Perform validation. + /// } + /// } + /// ``` pub fn with_extra_validation(mut self, f: F) -> Self where F: Fn(&OpenAPI, ValidationContext<'_>) + Send + Sync + 'static, @@ -374,6 +380,21 @@ impl ManagedApis { /// This function will be called for each API document. The /// [`ValidationContext`] can be used to report errors, as well as extra /// files for which the contents need to be compared with those on disk. + /// + /// You may want to skip performing validation on older blessed versions + /// because they're immutable, while still performing validation on the + /// latest blessed version (and recommending that a new version be created + /// instead). To do so, use the pattern: + /// + /// ``` + /// # use dropshot_api_manager_types::ValidationContext; + /// # use openapiv3::OpenAPI; + /// fn validate(doc: &OpenAPI, cx: ValidationContext<'_>) { + /// if cx.is_latest() || cx.is_blessed() != Some(true) { + /// // Perform validation. + /// } + /// } + /// ``` pub fn with_validation(mut self, validation: F) -> Self where F: Fn(&OpenAPI, ValidationContext<'_>) + Send + Sync + 'static, diff --git a/crates/dropshot-api-manager/src/output.rs b/crates/dropshot-api-manager/src/output.rs index ab68e65..a07cfd2 100644 --- a/crates/dropshot-api-manager/src/output.rs +++ b/crates/dropshot-api-manager/src/output.rs @@ -599,6 +599,10 @@ where )?; } + if let Some(help) = p.help() { + write_help(writer, &help, styles)?; + } + let Some(fix) = p.fix() else { continue; }; @@ -721,6 +725,26 @@ fn write_problem_header( ) } +fn write_help( + writer: &mut dyn io::Write, + help: &str, + styles: &Styles, +) -> io::Result<()> { + let first_indent = + format!("{:>HEADER_WIDTH$}: ", "help".style(styles.header)); + let more_indent = " ".repeat(HEADER_WIDTH + 2); + writeln!( + writer, + "{}", + textwrap::fill( + help, + textwrap::Options::new(term_width()) + .initial_indent(&first_indent) + .subsequent_indent(&more_indent) + ) + ) +} + /// Write the `fix:` line(s) for a single fix, splitting multi-step fixes into /// separate `will ...` lines that share the column structure of /// [`write_problem_header`]. diff --git a/crates/dropshot-api-manager/src/resolved.rs b/crates/dropshot-api-manager/src/resolved.rs index 4ccca84..4c52f63 100644 --- a/crates/dropshot-api-manager/src/resolved.rs +++ b/crates/dropshot-api-manager/src/resolved.rs @@ -373,6 +373,7 @@ pub enum VersionProblem<'a> { GeneratedValidationError { api_ident: ApiIdent, version: semver::Version, + blessed_help: Option, #[source] source: anyhow::Error, }, @@ -572,6 +573,41 @@ impl<'a> VersionProblem<'a> { self.fix().is_some() } + pub(crate) fn help(&self) -> Option { + // This is an exhaustive match so new variants cause a compile error. + match self { + VersionProblem::GeneratedValidationError { + version, + blessed_help: Some(help), + .. + } => Some(help.message(version)), + VersionProblem::GeneratedValidationError { + blessed_help: None, + .. + } + | VersionProblem::BlessedVersionNeedsRestore { .. } + | VersionProblem::BlessedVersionRestoreBlocked { .. } + | VersionProblem::BlessedVersionExtraLocalDoc { .. } + | VersionProblem::BlessedVersionCompareError { .. } + | VersionProblem::BlessedVersionBroken { .. } + | VersionProblem::BlessedLatestVersionBytewiseMismatch { .. } + | VersionProblem::LockstepMissingLocal { .. } + | VersionProblem::LockstepStale { .. } + | VersionProblem::LocalVersionMissingLocal { .. } + | VersionProblem::LocalVersionExtra { .. } + | VersionProblem::LocalVersionStale { .. } + | VersionProblem::LocalVersionGitStub { .. } + | VersionProblem::GeneratedSourceMissing { .. } + | VersionProblem::ExtraFileStale { .. } + | VersionProblem::BlessedVersionShouldBeGitStub { .. } + | VersionProblem::GitStubShouldBeJson { .. } + | VersionProblem::BlessedVersionCorruptedExtra { .. } + | VersionProblem::DuplicateLocalFile { .. } + | VersionProblem::GitStubCommitStale { .. } + | VersionProblem::GitStubFirstCommitUnknown { .. } => None, + } + } + pub(crate) fn compatibility_issues(&self) -> &[ApiCompatIssue] { // Exhaustive match to ensure new variants cause a compile error. match self { @@ -2353,9 +2389,16 @@ fn validate_generated( generated, ) { Err(source) => { + let blessed_help = match version.is_blessed { + None | Some(false) => None, + Some(true) => { + Some(BlessedValidationHelp::new(version.is_latest)) + } + }; problems.push(VersionProblem::GeneratedValidationError { api_ident: api.ident().clone(), version: version.version.clone(), + blessed_help, source, }); } @@ -2376,6 +2419,43 @@ fn validate_generated( } } +/// The help text shown when a blessed version fails validation. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum BlessedValidationHelp { + /// For the latest blessed version, add a new version. + AddVersion, + /// For an older blessed version, suggest adding a new version and skipping + /// validation on them in code. + AddVersionOrSkipOlderBlessed, +} + +impl BlessedValidationHelp { + fn new(is_latest: bool) -> Self { + if is_latest { + BlessedValidationHelp::AddVersion + } else { + BlessedValidationHelp::AddVersionOrSkipOlderBlessed + } + } + + fn message(self, version: &semver::Version) -> String { + let base = format!( + "version {version} is blessed, so its OpenAPI document can't be \ + changed. To fix these errors, add a new API version that \ + addresses them." + ); + match self { + BlessedValidationHelp::AddVersion => base, + BlessedValidationHelp::AddVersionOrSkipOlderBlessed => format!( + "{base} If these validation rules are newer than version \ + {version}, only enforce them on the latest version and on \ + unblessed versions (see the documentation on \ + `ManagedApis::with_validation` for more)." + ), + } + } +} + /// Describes the first commit for the latest version. /// /// Used to decide whether to suggest Git stub conversion for older versions. @@ -2452,6 +2532,18 @@ mod tests { use super::*; use assert_matches::assert_matches; + #[test] + fn test_blessed_validation_help_selection() { + assert_eq!( + BlessedValidationHelp::new(true), + BlessedValidationHelp::AddVersion, + ); + assert_eq!( + BlessedValidationHelp::new(false), + BlessedValidationHelp::AddVersionOrSkipOlderBlessed, + ); + } + #[test] fn test_displayable_vec() { let v = DisplayableVec(Vec::::new()); diff --git a/crates/integration-tests/src/fixtures.rs b/crates/integration-tests/src/fixtures.rs index ca332c4..4e640a6 100644 --- a/crates/integration-tests/src/fixtures.rs +++ b/crates/integration-tests/src/fixtures.rs @@ -1827,6 +1827,22 @@ fn validate_with_extra_file( } } +pub fn report_error_for_every_version( + _doc: &openapiv3::OpenAPI, + mut cx: ValidationContext<'_>, +) { + cx.report_error(anyhow::anyhow!("synthetic validation error")); +} + +pub fn report_error_for_latest_and_unblessed( + _doc: &openapiv3::OpenAPI, + mut cx: ValidationContext<'_>, +) { + if cx.is_latest() || cx.is_blessed() != Some(true) { + cx.report_error(anyhow::anyhow!("synthetic validation error")); + } +} + pub fn versioned_health_with_validation_api() -> ManagedApi { ManagedApi::from(ManagedApiConfig { ident: "versioned-health", diff --git a/crates/integration-tests/tests/integration/versioned.rs b/crates/integration-tests/tests/integration/versioned.rs index 6c91ab4..7af0283 100644 --- a/crates/integration-tests/tests/integration/versioned.rs +++ b/crates/integration-tests/tests/integration/versioned.rs @@ -2353,3 +2353,60 @@ fn test_corrupted_blessed_json_restored() -> Result<()> { Ok(()) } + +fn bless_versioned_health( + env: &TestEnvironment, + versions: &[&str], +) -> Result<()> { + let mut paths = Vec::new(); + for version in versions { + let path = env + .find_versioned_document_path("versioned-health", version)? + .with_context(|| format!("v{version} document should exist"))?; + paths.push(path); + } + let path_refs: Vec<_> = paths.iter().map(|p| p.as_path()).collect(); + env.git_add(&path_refs)?; + env.git_commit(&format!("bless {}", versions.join(", ")))?; + Ok(()) +} + +#[test] +fn test_blessed_validation_failure_help_older_versions() -> Result<()> { + let env = TestEnvironment::new_git()?; + env.generate_documents(&versioned_health_apis()?)?; + bless_versioned_health(&env, &["1.0.0", "2.0.0"])?; + + let apis = versioned_health_apis()? + .with_validation(report_error_for_every_version); + let (result, _summaries, rendered) = + check_apis_with_render(env.environment(), &apis)?; + assert_eq!(result, CheckResult::Failures); + + crate::snapshot::assert_render_snapshot( + &env, + "blessed_validation_failure_older_versions.txt", + &rendered, + ); + Ok(()) +} + +#[test] +fn test_blessed_validation_failure_help_latest_version() -> Result<()> { + let env = TestEnvironment::new_git()?; + env.generate_documents(&versioned_health_apis()?)?; + bless_versioned_health(&env, &["1.0.0", "2.0.0", "3.0.0"])?; + + let apis = versioned_health_apis()? + .with_validation(report_error_for_latest_and_unblessed); + let (result, _summaries, rendered) = + check_apis_with_render(env.environment(), &apis)?; + assert_eq!(result, CheckResult::Failures); + + crate::snapshot::assert_render_snapshot( + &env, + "blessed_validation_failure_latest_version.txt", + &rendered, + ); + Ok(()) +} diff --git a/crates/integration-tests/tests/output/integration/blessed_validation_failure_latest_version.txt b/crates/integration-tests/tests/output/integration/blessed_validation_failure_latest_version.txt new file mode 100644 index 0000000..6e6184a --- /dev/null +++ b/crates/integration-tests/tests/output/integration/blessed_validation_failure_latest_version.txt @@ -0,0 +1,19 @@ + ------- + Generating OpenAPI documents from API definitions ... + Loading local OpenAPI documents from "" ... + Loading blessed OpenAPI documents from VCS revision "main" path "documents" + ------- + Checking 3 OpenAPI documents... + Fresh versioned-health (versioned v1.0.0 (blessed)): Versioned Health API + Fresh versioned-health (versioned v2.0.0 (blessed)): Versioned Health API + Failure versioned-health (versioned v3.0.0 (blessed)): Versioned Health API + error: Generated OpenAPI document for API "versioned-health" version + 3.0.0 is not valid: OpenAPI document validation failed: + synthetic validation error + help: version 3.0.0 is blessed, so its OpenAPI document can't be + changed. To fix these errors, add a new API version that addresses + them. + Fresh versioned-health "latest" symlink + ------- + Failure 3 documents checked: 3 fresh, 0 stale, 1 failed, 0 other problems + (fix failures, then run test-openapi-manager generate to update) diff --git a/crates/integration-tests/tests/output/integration/blessed_validation_failure_older_versions.txt b/crates/integration-tests/tests/output/integration/blessed_validation_failure_older_versions.txt new file mode 100644 index 0000000..5fc4586 --- /dev/null +++ b/crates/integration-tests/tests/output/integration/blessed_validation_failure_older_versions.txt @@ -0,0 +1,32 @@ + ------- + Generating OpenAPI documents from API definitions ... + Loading local OpenAPI documents from "" ... + Loading blessed OpenAPI documents from VCS revision "main" path "documents" + ------- + Checking 3 OpenAPI documents... + Failure versioned-health (versioned v1.0.0 (blessed)): Versioned Health API + error: Generated OpenAPI document for API "versioned-health" version + 1.0.0 is not valid: OpenAPI document validation failed: + synthetic validation error + help: version 1.0.0 is blessed, so its OpenAPI document can't be + changed. To fix these errors, add a new API version that addresses + them. If these validation rules are newer than version 1.0.0, only + enforce them on the latest version and on unblessed versions (see + the documentation on `ManagedApis::with_validation` for more). + Failure versioned-health (versioned v2.0.0 (blessed)): Versioned Health API + error: Generated OpenAPI document for API "versioned-health" version + 2.0.0 is not valid: OpenAPI document validation failed: + synthetic validation error + help: version 2.0.0 is blessed, so its OpenAPI document can't be + changed. To fix these errors, add a new API version that addresses + them. If these validation rules are newer than version 2.0.0, only + enforce them on the latest version and on unblessed versions (see + the documentation on `ManagedApis::with_validation` for more). + Failure versioned-health (versioned v3.0.0 (added locally)): Versioned Health API + error: Generated OpenAPI document for API "versioned-health" version + 3.0.0 is not valid: OpenAPI document validation failed: + synthetic validation error + Fresh versioned-health "latest" symlink + ------- + Failure 3 documents checked: 1 fresh, 0 stale, 3 failed, 0 other problems + (fix failures, then run test-openapi-manager generate to update)