Skip to content

Fix form binding of Nullables - #133

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-129
May 19, 2026
Merged

Fix form binding of Nullables#133
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-129

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #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.

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>
@mromaszewicz
mromaszewicz requested a review from a team as a code ownerMay 19, 2026 15:25
@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

DiffPackageSupply Chain
Security
VulnerabilityQualityMaintenanceLicense
Addedgithub.com/​oapi-codegen/​nullable@​v1.1.0100100100100100

View full report

@mromaszewicz
mromaszewicz merged commit 2755f15 into oapi-codegen:mainMay 19, 2026
14 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-129 branch May 19, 2026 15:29
@mromaszewiczmromaszewicz added the bug Something isn't working label May 19, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugSomething isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When a form field is nullable: error binding string parameter: can not bind to destination of type: map

1 participant

@mromaszewicz