Skip to content

Error when unmarshaling types.Date query param #21

@0gener

Description

@0gener

Description

I am encountering an issue when binding date query parameters in a Gin-Gonic application. The problem arises when using oapi-codegen to generate models that include a date query parameter.

Steps to Reproduce

  1. OpenAPI Specification: I defined a date query parameter in my OpenAPI specification as follows:

    - name: date_from
      in: query
      schema:
        type: string
        format: date
  2. Generated Go Model: Using oapi-codegen, the above specification generates a Go struct resembling:

    type Params struct {
        DateFrom   *openapi_types.Date `form:"date_from,omitempty" json:"date_from,omitempty"`
    }
  3. Binding in Gin-Gonic: When I attempt to bind this query parameter using Gin's BindQuery method, I encounter an error.

    var queryParams models.Params
    if err := c.BindQuery(&queryParams); err != nil {
        c.AbortWithStatusJSON(http.StatusBadRequest, api.NewError(err.Error()))
        return
    }
    • Request URL: /endpoint?date_from=2023-01-01
    • Error Received: invalid character '-' after top-level value

Expected Behavior

I expect the date_from query parameter to be bound successfully to the Params struct without errors, parsing the date string correctly.

Actual Behavior

The binding process fails with an error indicating an issue with the '-' character in the date string, which suggests a possible problem with how the date string is being parsed or handled during the binding process.

Additional Information

  • Gin-Gonic Version: v1.9.1
  • oapi-codegen Version: v2.0.0
  • oapi-codegen/runtime Version: v1.1.0
  • Go Version: go1.21.4

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