feat: add WithBodyDecoder option for custom JSON body decoding#61
Merged
cubahno merged 2 commits intodoordash-oss:mainfrom Mar 28, 2026
Merged
Conversation
84d3cd9 to
37cf95b
Compare
Add a BodyDecoderFunc type and WithBodyDecoder router option that lets consumers customize how JSON request bodies are decoded. The default remains runtime.DecodeJSONBody, so this is fully backward-compatible. Use cases: - Pre-process request bodies before decoding (e.g. strip nulls) - Use a lenient decoder that tolerates schema mismatches - Enrich decode errors with application-specific context The option follows the same pattern as WithErrorHandler and is supported across all router backends.
37cf95b to
5ec4b8f
Compare
cubahno
reviewed
Mar 27, 2026
Collaborator
cubahno
left a comment
There was a problem hiding this comment.
Other than renames, looks good, thank you!
|
|
||
| // BodyDecoderFunc decodes a request body into the target value. | ||
| // The default implementation is DecodeJSONBody. | ||
| type BodyDecoderFunc func(body io.Reader, target any) error |
Collaborator
There was a problem hiding this comment.
could u plz rename it to JSONBodyDecoderFunc and the With option too because it applies only to json bodies.
Rename all body decoder identifiers to clarify they apply only to JSON: - BodyDecoderFunc → JSONBodyDecoderFunc - WithBodyDecoder → WithJSONBodyDecoder - bodyDecoder field → jsonBodyDecoder Addresses PR review feedback. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
cubahno
approved these changes
Mar 28, 2026
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.
Summary
Adds a
WithBodyDecoderrouter option that lets consumers customize how JSON request bodies are decoded, following the same functional options pattern asWithErrorHandlerandWithMiddleware.Motivation
The generated adapter calls
runtime.DecodeJSONBodydirectly, with no way to intercept the decoding pipeline. When deserialization fails (e.g.,nullin a typed array, invalid date formats), the error handler receives a raw Go error with limited context.A custom body decoder enables:
Changes
pkg/runtime/json.go— AddedBodyDecoderFunctype:func(body io.Reader, target any) errorpkg/codegen/templates/handler/adapter.tmpl— AddedbodyDecoderfield toHTTPAdapter, defaults toruntime.DecodeJSONBody, callsa.bodyDecoderinstead of hardcodedruntime.DecodeJSONBodybodyDecodertorouterConfig, addedWithBodyDecoderoption, wired to adapter after constructionexamples/server/test/server_test.go— Integration tests for the new optionThe remaining generated file changes are from
make generate.Backward compatibility
NewHTTPAdaptersignature is unchanged — still(svc, errHandler)runtime.DecodeJSONBodyis used when no custom decoder is setWithBodyDecoderis additive — existing code that doesn't use it is unaffectedUsage
Test plan
make test)NewHTTPAdaptersignature unchanged (no breaking change)