-
-
Notifications
You must be signed in to change notification settings - Fork 957
Open
Description
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:
oapi-codegen/pkg/codegen/templates/fiber/fiber-middleware.tmpl
Lines 91 to 107 in 635bb4e
{{range .HeaderParams}}// ------------- {{if .Required}}Required{{else}}Optional{{end}} header parameter "{{.ParamName}}" ------------- | |
if value, found := headers[http.CanonicalHeaderKey("{{.ParamName}}")]; found { | |
var {{.GoName}} {{.TypeDef}} | |
{{if .IsPassThrough}} | |
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value | |
{{end}} | |
{{if .IsJson}} | |
err = json.Unmarshal([]byte(value), &{{.GoName}}) | |
if err != nil { | |
return fiber.NewError(fiber.StatusBadRequest, fmt.Errorf("Error unmarshaling parameter '{{.ParamName}}' as JSON: %w", err).Error()) | |
} | |
{{end}} | |
{{if .IsStyled}} | |
err = runtime.BindStyledParameterWithOptions("{{.Style}}", "{{.ParamName}}", value, &{{.GoName}}, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: {{.Explode}}, Required: {{.Required}}}) |
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})
Metadata
Metadata
Assignees
Labels
No labels