fix: reuse request body string across schema validation errors - #317
Open
BOTecko wants to merge 1 commit into
Open
fix: reuse request body string across schema validation errors#317BOTecko wants to merge 1 commit into
BOTecko wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #317 +/- ##
=======================================
Coverage 98.27% 98.27%
=======================================
Files 75 75
Lines 9192 9202 +10
=======================================
+ Hits 9033 9043 +10
Misses 132 132
Partials 27 27
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
BOTecko
force-pushed
the
fix/reuse-reference-object-body
branch
from
August 13, 2026 21:02
e275e29 to
7113a5c
Compare
string([]byte) copies the payload. The fallback that fills ReferenceObject ran that conversion once per flattened violation, so an 8MiB object with hundreds of field errors allocated gigabytes per request. Cache the fallback string once per validation pass in request, response, and schema paths. Also convert renderedSchema once in extractBasicErrors. Validation results are unchanged.
BOTecko
force-pushed
the
fix/reuse-reference-object-body
branch
from
August 13, 2026 21:06
7113a5c to
0d866a3
Compare
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
When a payload fails schema validation, each flattened violation builds a
SchemaValidationFailureand, for typical object bodies, fillsReferenceObjectwith:In Go,
string([]byte)always copies. The same fallback exists in:requests/validate_request.goresponses/validate_response.goschema_validation/validate_schema.go(string(payload)andstring(renderedSchema))The
/^(\d+)/slice-index branch almost never runs for object bodies, so an8 MiBbody with384field errors copies the body 384 times (~3 GiBextra per request).Fix
Cache the fallback conversion once per validation pass and reuse it for every violation.
extractBasicErrorsalso convertsrenderedSchemaonce. Strings are immutable, so sharing the backing array does not change the public contract.Testing
go test ./...passes.TestReferenceObjectCopiesRequestBodyPerViolation: 80 type mismatches on a ~256 KiB object. Before the fix there are 80 distinctReferenceObjectbackings; after, there is 1.Local
ValidateSchemaBytesbenchmark, average of 5 independent runs (schema warmed first):Worst case matches
384 × 8 MiB ≈ 3 GiB. After the cache it is one copy plus parse/error overhead.