Skip to content

Comments

fix: add TypeHint-aware parameter binding and styling for []byte (#97)#98

Open
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/issue-97
Open

fix: add TypeHint-aware parameter binding and styling for []byte (#97)#98
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/issue-97

Conversation

@mromaszewicz
Copy link
Member

When an OpenAPI spec uses type: string, format: byte, oapi-codegen generates a []byte field. Previously the runtime treated []byte as a generic []uint8 slice -- splitting on commas and parsing individual integers -- instead of base64 encoding/decoding.

This adds a TypeHint mechanism via new WithOptions variants of existing functions. Existing functions delegate to the new variants with zero-value options, preserving all current behavior. Only when TypeHintBinary is explicitly passed does base64 handling activate.

Encoding uses base64.StdEncoding (standard alphabet, padded) per OpenAPI 3.0 reference to RFC 4648 Section 4.

Decoding is lenient: it inspects the input to select the correct decoder rather than blindly cascading (which could corrupt data when RawStdEncoding silently accepts padded input and treats '=' as data). If '=' padding is present, it uses the padded decoder; otherwise the raw (unpadded) decoder. If URL-safe characters ('-' or '_') are present, it uses the URL-safe alphabet; otherwise the standard alphabet. This accommodates real-world clients that may send unpadded or URL-safe base64, while still being correct for spec-compliant padded standard base64.

New public API:

  • TypeHint, TypeHintNone, TypeHintBinary
  • BindStringToObjectOptions / BindStringToObjectWithOptions
  • StyleParamOptions / StyleParamWithOptions
  • BindQueryParameterOptions / BindQueryParameterWithOptions
  • BindStyledParameterOptions.TypeHint field

The oapi-codegen code generator must be updated separately to emit calls to these new WithOptions functions with TypeHintBinary for format: byte fields. Until then, generated code continues to use the existing functions with unchanged behavior.

…api-codegen#97)

When an OpenAPI spec uses type: string, format: byte, the generated Go
code produces *[]byte fields. Previously the runtime treated []byte as
a generic []uint8 slice -- splitting on commas and parsing individual
integers -- instead of base64-encoding/decoding it as a single value.

This commit adds Type and Format string fields (matching the OpenAPI
spec type/format) to the Options structs for parameter binding and
styling functions. When Format is "byte" and the destination is
[]byte, the runtime base64-encodes (styling) or base64-decodes
(binding) the value as a single string.

New WithOptions functions and options structs:
- BindStringToObjectWithOptions + BindStringToObjectOptions
- StyleParamWithOptions + StyleParamOptions
- BindQueryParameterWithOptions + BindQueryParameterOptions

Extended existing options struct:
- BindStyledParameterOptions: added Type and Format fields

Existing functions delegate to WithOptions with zero-value options,
preserving backward compatibility for all current callers.

Encoding uses base64.StdEncoding (standard alphabet, padded) per
OpenAPI 3.0 / RFC 4648 Section 4. Decoding is lenient: it inspects
padding and URL-safe characters to select the correct decoder,
avoiding the silent corruption that occurs when RawStdEncoding
accepts padded input.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant