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

Invalid validation on Multi Value Headers (Chi Router implementation)

Open romulets opened this issue 1 year ago • 4 comments

I need a multi value header, namely Accept-Language. As per my understanding from RFC 9110 HTTP Semantics and Mozilla Docs, it's a valid HTTP request to have multiple values in a header field

Even though I have defined my request parameter as an multi value data structure (array):

        - name: Accept-Language
          in: header
          required: true
          schema:
            type: array
            items:
              type: string

I cannot receive a request with multiple Accept-Language headers. I always get the error Expected one value for Accept-Language, got 2, given by the generated validation:

     if valueList, found := headers[http.CanonicalHeaderKey("Accept-Language")]; found {
		var AcceptLanguage []string
		n := len(valueList)
		if n != 1 {
			siw.ErrorHandlerFunc(w, r, &TooManyValuesForParamError{ParamName: "Accept-Language", Count: n})
			return
		}

		err = runtime.BindStyledParameterWithLocation("simple", false, "Accept-Language", runtime.ParamLocationHeader, valueList[0], &AcceptLanguage)
		if err != nil {
			siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "Accept-Language", Err: err})
			return
		}

		params.AcceptLanguage = AcceptLanguage

	} else {
		err := fmt.Errorf("Header parameter Accept-Language is required, but not found")
		siw.ErrorHandlerFunc(w, r, &RequiredHeaderError{ParamName: "Accept-Language", Err: err})
		return
	}

The template that generates this code doesn't have any flow that allows that, it always goes through the assumption that we will have one value.

Can we add the flow of, case the spec allows multiple values, we remove the validation and pass not only the first position of the valueList further?

romulets avatar Mar 08 '23 14:03 romulets