Fix form binding of Nullables#133
Merged
Merged
Conversation
Closes: oapi-codegen#129 `nullable.Nullable[T]` is defined as `map[bool]T`, but neither `bindFormImpl` nor `BindStringToObjectWithOptions` had a `reflect.Map` branch, so any Nullable field in a form body failed with "error binding string parameter: can not bind to destination of type: map". This change adds Map handling at both layers: - `BindStringToObjectWithOptions` (bindstring.go): a bool-keyed map destination is treated as a Nullable wrapper -- bind `src` into a fresh value of the map's element type, then store it under `map[bool]T{true: value}`. This covers query, path, and header parameters in addition to forms. - `bindFormImpl` (bindform.go): a bool-keyed map field recurses through `bindFormImpl` on the element type and wraps the result, so `Nullable[ComplexStruct]` works alongside scalars. A non-bool-keyed map field routes to a new `bindFormMap` helper that binds entries of the form `name[key]=value` into a generic `map[K]V`. The structural `map[bool]T` check keeps `runtime` decoupled from `github.com/oapi-codegen/nullable`; the data shape is the whole type, so detecting it by reflection is equivalent to importing the package. An absent form field still produces an unspecified (zero) Nullable, matching the pre-fix behavior. Form payloads have no way to encode an explicit null, so `name=` binds as the inner type's zero value (specified), symmetric with non-nullable form binding. Tests cover scalar Nullable, generic `map[K]V`, and the absent-field-stays-unspecified path. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
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.
Closes: #129
nullable.Nullable[T]is defined asmap[bool]T, but neitherbindFormImplnorBindStringToObjectWithOptionshad areflect.Mapbranch, so any Nullable field in a form body failed with "error binding string parameter: can not bind to destination of type: map".This change adds Map handling at both layers:
BindStringToObjectWithOptions(bindstring.go): a bool-keyed map destination is treated as a Nullable wrapper -- bindsrcinto a fresh value of the map's element type, then store it undermap[bool]T{true: value}. This covers query, path, and header parameters in addition to forms.bindFormImpl(bindform.go): a bool-keyed map field recurses throughbindFormImplon the element type and wraps the result, soNullable[ComplexStruct]works alongside scalars. A non-bool-keyed map field routes to a newbindFormMaphelper that binds entries of the formname[key]=valueinto a genericmap[K]V.The structural
map[bool]Tcheck keepsruntimedecoupled fromgithub.com/oapi-codegen/nullable; the data shape is the whole type, so detecting it by reflection is equivalent to importing the package.An absent form field still produces an unspecified (zero) Nullable, matching the pre-fix behavior. Form payloads have no way to encode an explicit null, so
name=binds as the inner type's zero value (specified), symmetric with non-nullable form binding.Tests cover scalar Nullable, generic
map[K]V, and the absent-field-stays-unspecified path.