Skip to content

codegen: emit TypeHintBinary for format: byte parameters #2248

@mromaszewicz

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.

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