Skip to content

fix: reuse request body string across schema validation errors - #317

Open
BOTecko wants to merge 1 commit into
pb33f:mainfrom
BOTecko:fix/reuse-reference-object-body
Open

fix: reuse request body string across schema validation errors#317
BOTecko wants to merge 1 commit into
pb33f:mainfrom
BOTecko:fix/reuse-reference-object-body

Conversation

@BOTecko

@BOTecko BOTecko commented Aug 13, 2026

Copy link
Copy Markdown

Problem

When a payload fails schema validation, each flattened violation builds a SchemaValidationFailure and, for typical object bodies, fills ReferenceObject with:

referenceObject = string(requestBody)

In Go, string([]byte) always copies. The same fallback exists in:

  • requests/validate_request.go
  • responses/validate_response.go
  • schema_validation/validate_schema.go (string(payload) and string(renderedSchema))

The /^(\d+)/ slice-index branch almost never runs for object bodies, so an 8 MiB body with 384 field errors copies the body 384 times (~3 GiB extra per request).

Fix

Cache the fallback conversion once per validation pass and reuse it for every violation. extractBasicErrors also converts renderedSchema once. Strings are immutable, so sharing the backing array does not change the public contract.

Testing

  • Existing go test ./... passes.
  • Added TestReferenceObjectCopiesRequestBodyPerViolation: 80 type mismatches on a ~256 KiB object. Before the fix there are 80 distinct ReferenceObject backings; after, there is 1.

Local ValidateSchemaBytes benchmark, average of 5 independent runs (schema warmed first):

Scenario Current alloc Fixed alloc Current Fixed
1 MiB, 10 invalid fields 12.22 MiB 3.12 MiB 5.3 ms 4.6 ms
1 MiB, 100 invalid fields 103.12 MiB 3.63 MiB 8.1 ms 4.0 ms
5 MiB, 100 invalid fields 511.13 MiB 15.59 MiB 26.3 ms 14.6 ms
8 MiB, 384 invalid fields 3.02 GiB 26.14 MiB 96.5 ms 25.2 ms

Worst case matches 384 × 8 MiB ≈ 3 GiB. After the cache it is one copy plus parse/error overhead.

@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.27%. Comparing base (f309f59) to head (0d866a3).

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           
Flag Coverage Δ
unittests 98.27% <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 Harness.
📢 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.

@BOTecko
BOTecko force-pushed the fix/reuse-reference-object-body branch from e275e29 to 7113a5c Compare August 13, 2026 21:02
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
BOTecko force-pushed the fix/reuse-reference-object-body branch from 7113a5c to 0d866a3 Compare August 13, 2026 21:06
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