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 2 years 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

Yeah, you're right. I never thought multiple values were possible when I originally wrote this.

deepmap-marcinr avatar Mar 14 '23 23:03 deepmap-marcinr

I see failing tests, but mostly because I added the multi value header to the petstore-extended.

How would you test this feature? Do I keep it there in the yaml? Do I remove it?

And also, I fixed only for chi, which is the library I'm using.

romulets avatar Mar 15 '23 11:03 romulets

Updated the MR with tests. It all works, even the other libs with multiple headers.

romulets avatar Mar 15 '23 11:03 romulets

Hey, I'd like to ping this issue, as I am running into a similar problem where I need multiple values for a header in my application. Is there anything that I can do to help with solving this problem?

DanielHoffmann-BO avatar May 09 '24 21:05 DanielHoffmann-BO