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

Compile error in generated code when there are multiple cookie parameters

Open shuz opened this issue 3 years ago • 0 comments

The template generates multiple parameters. Example:

	var cookie *http.Cookie

	if cookie, err = r.Cookie("cookie1"); err == nil {
		var value string
		err = runtime.BindStyledParameter("simple", true, "oauth2_authentication_csrf", cookie.Value, &value)
		if err != nil {
			siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "oauth2_authentication_csrf", Err: err})
			return
		}
		params.Oauth2AuthenticationCsrf = &value

	}

	var cookie *http.Cookie

	if cookie, err = r.Cookie("cookie2"); err == nil {
		var value string
		err = runtime.BindStyledParameter("simple", true, "ulsid", cookie.Value, &value)
		if err != nil {
			siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "ulsid", Err: err})
			return
		}
		params.Ulsid = &value

	}

Note that var cookie *http.Cookie is duplicated and caused compile error.

I checked it's true for both chi and gorilla middleware. Code reference for Chi: https://github.com/deepmap/oapi-codegen/blob/a444d309616eaac712c1b1a633c0818613918e11/pkg/codegen/templates/chi/chi-middleware.tmpl#L129

Code reference for Gorilla: https://github.com/deepmap/oapi-codegen/blob/ab90f1927bc5ec3e29af216d4298fbb4780ae36d/pkg/codegen/templates/gorilla/gorilla-middleware.tmpl#L129

shuz avatar Jan 14 '23 00:01 shuz