oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Header parameter generation failure for fiber-server. "value" is an []string but the function BindStyledParameterWithOptions requires a single string

Open Somebodyisnobody opened this issue 7 months ago • 4 comments

I have the following OpenAPI:

openapi: 3.0.3
info:
  title: Foo
  description: FooDesc
  version: 0.0.0
servers:
  - url: 'http://localhost:8080/'
paths:
  /{someString}:
    parameters:
      - name: someString
        in: path
        description: String of something.
        required: true
        schema:
          type: string
      - name: User-Agent
        in: header
        description: User agent.
        required: true
        schema:
          type: string

    get:
        [...]

I try to generate with the following options:

# yaml-language-server: $schema=https://raw.githubusercontent.com/oapi-codegen/oapi-codegen/HEAD/configuration-schema.json
package: foo_api
output: server.gen.go
generate:
  models: true
  fiber-server: true

and run //go:generate go tool oapi-codegen -config cfg.yml openapi.yml using github.com/oapi-codegen/oapi-codegen/v2 v2.4.1

After generation go build fails with the following reason: cannot use value (variable of type []string) as string value in argument to runtime.BindStyledParameterWithOptions

The generated code snippet is the following:

// ------------- Required header parameter "User-Agent" -------------
if value, found := headers[http.CanonicalHeaderKey("User-Agent")]; found {
	var UserAgent string

	err = runtime.BindStyledParameterWithOptions("simple", "User-Agent", value, &UserAgent, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true})
	if err != nil {
		return fiber.NewError(fiber.StatusBadRequest, fmt.Errorf("Invalid format for parameter User-Agent: %w", err).Error())
	}

	params.UserAgent = UserAgent

} else {
	err = fmt.Errorf("Header parameter User-Agent is required, but not found: %w", err)
	return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

The expected string variable "value" is an array.

I think I found the correct lines in the template here: https://github.com/oapi-codegen/oapi-codegen/blob/635bb4e59de33ccf8c447d53a0c50fa61d9ae8b8/pkg/codegen/templates/fiber/fiber-middleware.tmpl#L91-L107

As workaround I change the faulty parametr in a way that only the first element is selected:

err = runtime.BindStyledParameterWithOptions("simple", "User-Agent", value[0], &UserAgent, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true})

Somebodyisnobody avatar Jul 16 '25 12:07 Somebodyisnobody

Same error :(

rugleb avatar Aug 30 '25 14:08 rugleb

same here, its pretty faulty, it probably needs a concat or picking 0 or at some index

fedya-eremin avatar Sep 06 '25 08:09 fedya-eremin

@jamietanna sorry for tagging, but it seems to be critical for fiber users. if you agree, I may open a PR with template change

fedya-eremin avatar Sep 06 '25 08:09 fedya-eremin

I mean a header in general can appear multiple times so can make sense to give back an array of headers with this name found.

Somebodyisnobody avatar Sep 07 '25 10:09 Somebodyisnobody