Skip to content

Regression: "required" not respected for query parameters #2403

Closed

Description

@mvodanovic

I have this in my openapi.yaml:

parameters:
  - in: query
    name: custom_date
    required: true
    schema:
      type: string
      format: date

The auto-generated handler fails to return an error if the parameter isn't given in the request. This worked fine before, when we were using version 2.6.0, but it doesn't work on 2.7.1.

I think the bug is in runtime/bindparam.go around line 479. The code looks like this:

if !fieldsPresent {
    return nil
}

But it should look like this:

if !fieldsPresent {
    if required {
        return &RequiredParameterError{ParamName: paramName}
    } else {
        return nil
    }
}

Activity

  1. cschanhniem commented on Jun 18, 2026

    @cschanhniem

    Confirmed. The issue is in pkg/runtime/bindparam.go line 479. The change from v2.6.0 to v2.7.0 likely refactored the bind logic to use the fieldsPresent sentinel to detect when no form values were submitted, but the required check for query parameters was dropped in that refactor.

    The OpenAPI spec §3.0.2 is clear: when required: true on a query parameter, absence must produce a 400 error — not silently pass. The fix is exactly as described:

    if !fieldsPresent {
        if required {
            return &RequiredParameterError{ParamName: paramName}
        }
        return nil
    }

    The RequiredParameterError type is already defined in bind.go and param.go already has the required flag available — this is purely a guard clause restoration. The fix is safe because required is always false for optional params (the struct field uses a pointer type), so it does not break existing optional parameter handling.

  2. mromaszewicz commented on Jun 20, 2026

    @mromaszewicz
    Member

    I think this bug is caused by special handling for the Date struct. Doesn't look to bad, but I will need to push a new runtime version.

  3. mromaszewicz commented on Jun 20, 2026

    @mromaszewicz
    Member

    I think I broke this here:
    oapi-codegen/runtime#93

  4. mromaszewicz commented on Jun 20, 2026

    @mromaszewicz
    Member

    I was wrong, the real reason is:

    This is a combination of an oapi-codegen change and a long-standing gap in runtime — not PR #93.

    Why only date (and other struct types) break: types.Date is a Go struct. A default query parameter is form/explode=true, which routes struct-typed params through a different branch of runtime's BindQueryParameterWithOptions than plain strings. In that struct branch, when the parameter is absent, the code does if !fieldsPresent { return nil } — it never returns a RequiredParameterError. Plain string params go through the scalar branch, which does honor required. That's why a required string errors correctly but a required date doesn't.

    What changed between 2.6.0 and 2.7.x: the generated handler used to emit its own explicit required check (if Get(name) != "" { … } else { RequiredParamError }). In 2.7.0 that was removed in favor of letting runtime report missing required params (the new errors.As(err, &RequiredParameterError) block). That works for strings and slices, but for struct types like date the runtime returns nil, so the missing-required case is silently skipped. The old explicit check had been masking the runtime gap.

    So the runtime struct branch never honored required — that part is years old and predates #93. #93 only changed how present dates bind; it didn't touch the absence path.

    Fix (in runtime): the reflect.Struct case in BindQueryParameterWithOptions (and BindRawQueryParameter) should match the string/slice branches next to it:

    if !fieldsPresent {
        if required {
            return &RequiredParameterError{ParamName: paramName}
        }
        return nil
    }

    No codegen change is needed — generated code already maps RequiredParameterError to a 400. Users on oapi-codegen ≥ 2.7.0 just need the runtime release that includes this fix.

  5. added
    bug
    on Jun 20, 2026
  6. mromaszewicz commented on Jun 20, 2026

    @mromaszewicz
    Member

    I'm pushing a fix. You will need to update your runtime to v1.4.2 as this is purely an oversight in new runtime behavior.

  7. added a commit that references this issue on Jun 20, 2026
    7de8635
  8. added a commit that references this issue on Jun 23, 2026
    16be8ff
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions