Skip to content

Replace bare type assertions with errors.As in request/response validation#270

Open
SebTardif wants to merge 1 commit intopb33f:mainfrom
SebTardif:fix/unsafe-type-assertion-errors-as
Open

Replace bare type assertions with errors.As in request/response validation#270
SebTardif wants to merge 1 commit intopb33f:mainfrom
SebTardif:fix/unsafe-type-assertion-errors-as

Conversation

@SebTardif
Copy link
Copy Markdown

Problem

ValidateResponseSchema and ValidateRequestSchema use bare type assertions to cast the error returned by compiledSchema.Validate():

jk := scErrs.(*jsonschema.ValidationError)

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-248 already uses the safe pattern:

var jk *jsonschema.ValidationError
if errors.As(scErrs, &jk) {

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 in validate_schema.go. When the assertion fails, the function still reports a validation error but with nil SchemaValidationErrors, consistent with other error paths in the codebase.

Testing

All existing tests pass. The fix cannot be demonstrated with a red-green test because compiledSchema is a concrete type from santhosh-tekuri/jsonschema/v6 and cannot be mocked to return a different error type. This is a defensive fix following the established codebase pattern.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.08%. Comparing base (f95f219) to head (5928f18).

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           
Flag Coverage Δ
unittests 98.08% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant