Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,26 @@ correct, media-type-defined behavior.
their original precision, but the round-trip through JSON means struct
field tags are honored (not raw Go field names).


## Request body binding

`BindJSONBody` is what strict net/http servers generated by `oapi-codegen` call
to decode a JSON request body. When the request has passed through a validation
middleware built on kin-openapi's `openapi3filter` with
`Options.KeepValidatedValue` set (for example
`nethttp-middleware.OapiRequestValidatorWithOptions`), the body carries the
value the validator decoded and validated, and `BindJSONBody` binds that value
into the request struct with `BindJSONValue` instead of decoding the body a
second time.

Binding follows `encoding/json`'s rules (struct tags, embedded structs,
`json.Unmarshaler` / `encoding.TextUnmarshaler`, base64 for `[]byte`, `float64`
for `any`), with one deliberate difference: object member names are matched to
struct fields **exactly**, whereas `encoding/json` matches them
case-insensitively. This closes the gap where `{"role":"user","ROLE":"admin"}`
validates as `role: user` but a second `encoding/json` decode binds
`Role: "admin"`.

Without a validated value, or when the value cannot be bound into the
destination type, `BindJSONBody` decodes the body with `encoding/json` exactly
as before, so error responses are unchanged.
Loading
Loading