feat(libRequest): add optional JSON body binding modes - #59
Merged
Merged
Conversation
Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…in TestingParser Extend TestingParser.GetBody to return webFramework.ErrEmptyBody for empty string and empty byte slice fixtures, matching the nil body behavior. Add table-driven test cases for both empty string and empty []byte. Replace if-nil check with type switch to handle string/[]byte length checks before setTarget. Preserve existing precedence: BodyError takes priority over empty-body detection.
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
libRequest.parseRequesttreats every body parse failure as fatal (ERROR_IN_GET_REQUEST_BODY→ 400). For endpoints where the body is optional — e.g.POST /api/card/:cardNumber/harim, which takes all required input from the path and only readscvv2/expirywhen present — aPOSTwithContent-Length: 0is rejected even though the contract allows it. Clients that send no body (e.g. Swagger UI when no requestBody is defined) get an unexpected 400.Design
webFramework.ErrEmptyBody, returned byRequestParser.GetBodyimplementations when the body is absent or zero-length (wrappable; callers useerrors.Is). Empty body is now distinguishable from a successful bind.libRequest.Typevalues, appended at the end of the const block so existing iota values — which are JSON-serialized and persisted — are unchanged:JSONOptional— binds JSON when present; empty body yields the zero-valued request.JSONWithURIOptional— same optional body semantics; URI binding stays mandatory.requestTypeEnum.goregenerated viaenumer(v1.7.2).libGinmapsio.EOF→ sentinel;libFiberreturns it whenlen(Body()) == 0;libNetHttpreturns it for absent/empty bodies (previously returned nil);FakeParsergains aBodyErrorfield and honors the sentinel;TestingParserhonors it for nil/empty fixtures.TestingParser.setTargetno longer panics: it previously calledSeton an unaddressablereflect.Value; now usesElem()with kind/assignability guards.Backwards compatibility
JSONandJSONWithURIstill fail on empty bodies withERROR_IN_GET_REQUEST_BODY— only the new modes tolerate the sentinel.libNetHttppreviously accepted empty bodies under requiredJSON; it now matches Gin/Fiber and rejects them.libValidate.ValidateStruct) still runs for optional modes on the zero-valued request — optional body fields needomitempty/non-required tags.Usage
Testing
JSON/JSONWithURIstill reject empty bodies.ErrEmptyBodyassertions for Gin, Fiber, and net/http.go build ./...,go test ./...,go vet ./...all green.