Skip to content

codegen: emit TypeHintBinary for format: byte parameters #2248

Closed

Description

@mromaszewicz

Summary

The runtime library now supports proper base64 encoding/decoding for []byte parameters (OpenAPI type: string, format: byte) via a new TypeHint mechanism. The code generator needs to be updated to use these new WithOptions functions so that []byte fields are correctly handled at runtime.

Background

When a spec defines type: string, format: byte, oapi-codegen generates a *[]byte field. The runtime currently treats []byte as a generic []uint8 slice — splitting on commas and parsing individual integers — instead of base64-encoding/decoding it.

This was reported as oapi-codegen/runtime#97 and fixed in oapi-codegen/runtime#98, which adds:

  • TypeHint / TypeHintBinary constants
  • BindStringToObjectWithOptions (with BindStringToObjectOptions{TypeHint: TypeHintBinary})
  • StyleParamWithOptions (with StyleParamOptions{TypeHint: TypeHintBinary})
  • BindQueryParameterWithOptions (with BindQueryParameterOptions{TypeHint: TypeHintBinary})
  • BindStyledParameterOptions.TypeHint field

Existing functions delegate to the new variants with zero-value options, so there is no behavior change until the generator emits the new calls.

What needs to change in the generator

For any parameter or field with format: byte (Go type []byte), generated code should call the WithOptions variants with TypeHintBinary instead of the current functions. Specifically:

  1. Styled parameter binding — pass TypeHint: runtime.TypeHintBinary in BindStyledParameterOptions
  2. Query parameter binding — call runtime.BindQueryParameterWithOptions(...) with BindQueryParameterOptions{TypeHint: runtime.TypeHintBinary} instead of runtime.BindQueryParameter(...)
  3. Style parameter serialization — call runtime.StyleParamWithOptions(...) with StyleParamOptions{TypeHint: runtime.TypeHintBinary} instead of runtime.StyleParamWithLocation(...)

Blocked on

A new release of github.com/oapi-codegen/runtime containing the changes from oapi-codegen/runtime#98.

Activity

  1. self-assigned this
    on Feb 25, 2026
  2. mromaszewicz commented on Feb 25, 2026

    @mromaszewicz
    MemberAuthor

    Update: The runtime PR (#98 in oapi-codegen/runtime) has been updated. The API now uses Type and Format string fields (matching the OpenAPI spec's own type/format strings) instead of the earlier TypeHint enum approach. This is more extensible and lets the runtime make spec-informed decisions.

    The new options structs accept:

    • Type string — the OpenAPI type (e.g. "string", "integer")
    • Format string — the OpenAPI format (e.g. "byte", "date-time")

    When Format == "byte" and the destination is []byte, the runtime base64-encodes/decodes the value. The code generator should populate these fields from the spec when calling the WithOptions variants.

  3. added a commit that references this issue on Feb 26, 2026
    375bf71
  4. added a commit that references this issue on Feb 27, 2026
    15fc536
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions