Replace bare type assertions with errors.As in request/response validation#270
Open
SebTardif wants to merge 1 commit intopb33f:mainfrom
Open
Replace bare type assertions with errors.As in request/response validation#270SebTardif wants to merge 1 commit intopb33f:mainfrom
SebTardif wants to merge 1 commit intopb33f:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #270 +/- ##
=======================================
Coverage 98.08% 98.08%
=======================================
Files 64 64
Lines 6987 6989 +2
=======================================
+ Hits 6853 6855 +2
Misses 133 133
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ValidateResponseSchemaandValidateRequestSchemause bare type assertions to cast the error returned bycompiledSchema.Validate():If
Validate()ever returns an error that is not exactly*jsonschema.ValidationError(e.g., a wrapped error in a future library version), this panics with a runtime type assertion failure.The same operation in
validate_schema.go:247-248already uses the safe pattern:The bare assertions were introduced in PR #18 (2023-08-07) and PR #20 (2023-09-02).
Fix
Replace both bare type assertions with
errors.As, following the existing pattern invalidate_schema.go. When the assertion fails, the function still reports a validation error but withnilSchemaValidationErrors, consistent with other error paths in the codebase.Testing
All existing tests pass. The fix cannot be demonstrated with a red-green test because
compiledSchemais a concrete type fromsanthosh-tekuri/jsonschema/v6and cannot be mocked to return a different error type. This is a defensive fix following the established codebase pattern.