Skip to content

Template for gin wrappers is incorrectly reporting an 'err' var #617

@udhos

Description

@udhos

Querying an API without a proper header returned this:

{"msg":"Header parameter key is required, but not found: %!s(\u003cnil\u003e)"}

Looking at the generated code, I have found this:

c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Header parameter key is required, but not found: %s", err)})

From full generated method below, one can see the var err used is actually unrelated to the not found condition.

// GetLocation operation middleware
func (siw *ServerInterfaceWrapper) GetLocation(c *gin.Context) {

	var err error

	// ------------- Path parameter "location" -------------
	var location string

	err = runtime.BindStyledParameter("simple", false, "location", c.Param("location"), &location)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter location: %s", err)})
		return
	}

	// Parameter object where we will unmarshal all parameters from the context
	var params GetLocationParams

	headers := c.Request.Header

	// ------------- Required header parameter "key" -------------
	if valueList, found := headers[http.CanonicalHeaderKey("key")]; found {
		var Key string
		n := len(valueList)
		if n != 1 {
			c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Expected one value for key, got %d", n)})
			return
		}

		err = runtime.BindStyledParameterWithLocation("simple", false, "key", runtime.ParamLocationHeader, valueList[0], &Key)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter key: %s", err)})
			return
		}

		params.Key = Key

	} else {
		c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Header parameter key is required, but not found: %s", err)})
		return
	}

	for _, middleware := range siw.HandlerMiddlewares {
		middleware(c)
	}

	siw.Handler.GetLocation(c, location, params)
}

The issue is at: https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/templates/gin/gin-wrappers.tmpl#L114

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions