Skip to content

"required" of date query parameters is ignored #369

Closed

Description

@Makinami

If a query parameter of date format is required in the API definition but not passed in a request, runtime does not return an error.

I have created a minimal example of this bug: https://github.com/Makinami/oapi-codegen-bug together with my analysis of the problem, which I'm copy/pasting below for convenience:


Analysis

The problem is with how date and time are internally handled.

If a query parameter is required, it's generated as a non-pointer member. When passed to BindQueryParameter it goes to the switch statement on line 326 in bindparam.go where it's type is checked:

// abbreviated
switch k {
case reflect.Slice:
    // contains checks if param is required and found
case reflect.Struct:
    // no additional checks
    err = bindParamsToExplodedObject(paramName, queryParams, output)
default:
    // contains checks if param is required and found
}

Since oapi-codegen's Date type and time.Time are both structs they fall into second case where information about whether the parameter is required or not is completely lost.


I think that the problem can be solved by adding checks to the above switch, passing required flag down to bindParamsToExplodedObject and/or better handling of time.Time like struct. This, I think, can be done without braking public API, although it would change the behaviour (return an error rather than set to zero value).

I'd be happy to work on this as we have recently started using this package extensively in my company, but I'd just like to hear a comment or even a simply yes/no on whether my analysis and proposed "solution" is correct.

Activity

  1. mromaszewicz commented on Nov 7, 2026

    @mromaszewicz
    Member

    This is fixed in github.com/oapi-codegen/runtime by oapi-codegen/runtime#135, released in runtime v1.4.2.

    The cause was in BindQueryParameterWithOptions: for an exploded form parameter whose Go type is a struct, including types.Date and time.Time, a missing parameter returned nil without checking the required flag. The struct branch now returns a RequiredParameterError, the same as the primitive and slice cases.

    I re-checked against current runtime main. A required format: date query parameter that is left out of the request now fails binding with query parameter 'date' is required. Upgrading to runtime v1.4.2 or later picks this up; the generated code doesn't need to change.

    Closing as fixed. Please reopen if you still see this on a current runtime.


    This message was drafted with the help of Claude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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