Skip to content

Bind OpenAPI 3.1 multi-type union parameters into any destinations - #154

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/issue-153
Aug 16, 2026
Merged

Bind OpenAPI 3.1 multi-type union parameters into any destinations#154
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/issue-153

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

A parameter declared with a 3.1 multi-type union (type: [string, integer]) generates an any destination, which the binder rejected unconditionally: "can not bind to destination of type: interface". The binder is destination-driven, and an interface destination carries no information.

Add a Types field to BindStyledParameterOptions, BindQueryParameterOptions and BindStringToObjectOptions carrying the union's member list. It is only consulted when the destination is an empty interface, so concrete destinations keep the reflection-driven path unchanged. The value binds to the first member that parses, in specificity order (boolean, integer, number, string) rather than declaration order: JSON Schema defines the type array as an unordered set, and the always-succeeding string member would otherwise shadow the rest. Numeric detection follows JSON number grammar, so "007" and "+1" stay strings instead of being reinterpreted, and the bound value matches what the same token would produce inside a JSON document. Format applies only to its host member and only where it changes decoding: int32/int64 and float/double select numeric widths, byte base64-decodes the string member; annotation-only formats are ignored per 3.1 semantics. Non-scalar members and the "null" marker are skipped.

Closes #153

@mromaszewicz
mromaszewicz requested a review from a team as a code owner August 16, 2026 04:45
@mromaszewicz mromaszewicz added the enhancement New feature or request label Aug 16, 2026
A parameter declared with a 3.1 multi-type union (type: [string, integer])
generates an `any` destination, which the binder rejected unconditionally:
"can not bind to destination of type: interface". The binder is
destination-driven, and an interface destination carries no information.

Add a Types field to BindStyledParameterOptions, BindQueryParameterOptions
and BindStringToObjectOptions carrying the union's member list. It is only
consulted when the destination is an empty interface, so concrete
destinations keep the reflection-driven path unchanged. The value binds to
the first member that parses, in specificity order (boolean, integer,
number, string) rather than declaration order: JSON Schema defines the type
array as an unordered set, and the always-succeeding string member would
otherwise shadow the rest. Numeric detection follows the JSON number
production (RFC 8259), so "007" and "+1" stay strings instead of being
reinterpreted.

The bound value's dynamic type is one of exactly bool, int64, float64,
string, or []byte. Format "byte" is the one load-bearing format (it changes
the wire decoding, base64-decoding the string member); width formats
(int32/int64, float/double) and annotation-only formats (date-time, uuid,
...) are ignored by default so that a spec edit to `format` can never
silently change the dynamic type a running handler's type switch sees.
Applications that want width narrowing opt in via the
NarrowUnionNumericFormats package variable — the DefaultQueryEncoder
pattern — which makes format int32 produce int32 and format float produce
float32, with out-of-range values falling through to the next member.
The "null" nullability marker is ignored wherever it appears, whether or
not the generator stripped it. Arrays of unions and deepObject-style
binding are documented as out of scope.

Closes oapi-codegen#153

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz merged commit f2e468c into oapi-codegen:main Aug 16, 2026
11 checks passed
@mromaszewicz
mromaszewicz deleted the feat/issue-153 branch August 16, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bind styled parameters into any destinations using a declared Types list (OpenAPI 3.1 multi-type unions)

1 participant