Skip to content

fix: let self-parsing types bind as a last resort - #197

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-196-bind-string-custom-types
Nov 23, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-196-bind-string-custom-types

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Part of #196 (1 of 2).

Problem

BindStringToObject only found a type's own parsing logic in two narrow cases: Binder on the destination exactly as passed, and encoding.TextUnmarshaler on array kinds (the uuid.UUID case). Everything else with no built-in conversion failed with can not bind to destination of type: slice / struct:

  • a Binder slice, struct or map behind the pointer of an optional field, where the destination is a **T and the interface is not visible;
  • a TextUnmarshaler slice or struct, with or without a pointer, such as net.IP, netip.Addr or big.Int;
  • an optional *uuid.UUID property of an exploded form object, for the same **T reason.

Fix

Where no built-in conversion applies, check the dereferenced destination for Binder, then encoding.TextUnmarshaler, before giving up. The array case uses the same check.

Compatibility

Additive only. The new check runs solely on paths that previously returned an error, so every destination that binds today takes the same path as before. In particular:

  • time.Time and types.Date keep their lenient parsing ahead of their stricter UnmarshalText;
  • format: byte still base64-decodes first, even into a TextUnmarshaler such as net.IP;
  • plain slices and structs are still errors.

TestBindStringToObject_SelfParsingIsLastResort pins these.

Not in this PR

A Binder on a primitive kind behind an optional pointer is still parsed as a plain primitive without calling Bind. Fixing that changes the result of requests that bind today, so it is in a separate PR. The two are independent and merge cleanly in either order.

🤖 Generated with Claude Code

BindStringToObject only found a type's own parsing logic in two narrow
cases: Binder on the destination exactly as passed, and
encoding.TextUnmarshaler on array kinds. An optional field hands it a
**T, on which neither interface is visible, and a TextUnmarshaler slice
or struct such as net.IP, netip.Addr or big.Int was never consulted, so
all of these failed with "can not bind to destination of type".

Where no built-in conversion applies, check the dereferenced destination
for Binder and then encoding.TextUnmarshaler before giving up. The array
case uses the same check, which also fixes an optional *uuid.UUID
property of an exploded form object.

This is a fallback only. Every destination which binds today takes the
same path as before, including time.Time and types.Date, whose lenient
parsing stays ahead of their stricter UnmarshalText, and base64 decoding
for format: byte.

A Binder on a primitive kind behind an optional pointer is still parsed
as a plain primitive without calling Bind. Honoring Bind there changes
the result of requests which bind today, so it is a separate change.

Part of oapi-codegen#196

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner November 23, 2026 00:15
@mromaszewicz mromaszewicz added the bug Something isn't working label Nov 23, 2026
@mromaszewicz
mromaszewicz merged commit fb97b97 into oapi-codegen:main Nov 23, 2026
10 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-196-bind-string-custom-types branch November 23, 2026 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant