fix(spec): keep zero-valued bounds through a gob round-trip - #310
Merged
fredbi merged 1 commit intoAug 25, 2026
Merged
Conversation
gob omits any struct field holding the zero value for its type, and flattens a pointer to the value it points at. An optional number that is present and zero travelled as the zero value, was omitted, and came back as a nil pointer: "minimum": 0 on a schema, "maximum": 0 on a parameter and every other bound spelled with a zero were dropped, with no error. Encoding a whole Swagger lost them at any depth, through the encoder this package already advertises. Schema, Parameter, Header and Items now send which of their optional numbers were present-and-zero as a bitmask alongside the struct, and put the zero back on decode. The encoders sit on those outer types rather than on CommonValidations or SchemaProps because a method on an embedded type is promoted to whatever embeds it: gob would call it for the whole value and drop everything the embedded type does not hold. Swagger and Operation already carry their own encoders for that reason. TestGob_EveryOptionalNumberIsCarried reads the *float64 and *int64 fields of those four types by reflection, so a bound added later is covered without editing the test, and fails until gob.go carries it. Round-tripping a mid-sized document through gob costs 153ms and 92MB against 47ms and 26MB before this change, because a GobEncode that returns bytes needs its own encoder per value. JSON round-trips the same document in 39ms and 12MB, so gob is the slower way to copy this model either way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #310 +/- ##
==========================================
+ Coverage 93.03% 93.19% +0.16%
==========================================
Files 30 31 +1
Lines 2512 2632 +120
==========================================
+ Hits 2337 2453 +116
- Misses 175 179 +4 ☔ View full report in Codecov by Harness. |
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.
gob omits any struct field holding the zero value for its type, and flattens a pointer to the value it points at. An optional number that is present and zero travelled as the zero value, was omitted, and came back as a nil pointer: "minimum": 0 on a schema, "maximum": 0 on a parameter and every other bound spelled with a zero were dropped, with no error. Encoding a whole Swagger lost them at any depth, through the encoder this package already advertises.
Schema, Parameter, Header and Items now send which of their optional numbers were present-and-zero as a bitmask alongside the struct, and put the zero back on decode. The encoders sit on those outer types rather than on CommonValidations or SchemaProps because a method on an embedded type is promoted to whatever embeds it: gob would call it for the whole value and drop everything the embedded type does not hold. Swagger and Operation already carry their own encoders for that reason.
TestGob_EveryOptionalNumberIsCarried reads the *float64 and *int64 fields of those four types by reflection, so a bound added later is covered without editing the test, and fails until gob.go carries it.
Round-tripping a mid-sized document through gob costs 153ms and 92MB against 47ms and 26MB before this change, because a GobEncode that returns bytes needs its own encoder per value. JSON round-trips the same document in 39ms and 12MB, so gob is the slower way to copy this model either way.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist