Header parameter generation failure for fiber-server. "value" is an []string but the function BindStyledParameterWithOptions requires a single string
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})
Same error :(
same here, its pretty faulty, it probably needs a concat or picking 0 or at some index
@jamietanna sorry for tagging, but it seems to be critical for fiber users. if you agree, I may open a PR with template change
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.