You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regression: "required" not respected for query parameters #2403
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
}
}
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:
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.
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:
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.
Activity
cschanhniem commentedon Jun 18, 2026
Confirmed. The issue is in
pkg/runtime/bindparam.goline 479. The change from v2.6.0 to v2.7.0 likely refactored the bind logic to use thefieldsPresentsentinel to detect when no form values were submitted, but therequiredcheck for query parameters was dropped in that refactor.The OpenAPI spec §3.0.2 is clear: when
required: trueon a query parameter, absence must produce a 400 error — not silently pass. The fix is exactly as described:The
RequiredParameterErrortype is already defined inbind.goandparam.goalready has therequiredflag available — this is purely a guard clause restoration. The fix is safe becauserequiredis alwaysfalsefor optional params (the struct field uses a pointer type), so it does not break existing optional parameter handling.mromaszewicz commentedon Jun 20, 2026
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.
mromaszewicz commentedon Jun 20, 2026
I think I broke this here:
oapi-codegen/runtime#93
mromaszewicz commentedon Jun 20, 2026
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.Dateis a Gostruct. A default query parameter isform/explode=true, which routes struct-typed params through a different branch ofruntime'sBindQueryParameterWithOptionsthan plain strings. In that struct branch, when the parameter is absent, the code doesif !fieldsPresent { return nil }— it never returns aRequiredParameterError. Plainstringparams go through the scalar branch, which does honorrequired. That's why a requiredstringerrors correctly but a requireddatedoesn'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 lettingruntimereport missing required params (the newerrors.As(err, &RequiredParameterError)block). That works for strings and slices, but for struct types likedatethe runtime returnsnil, 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): thereflect.Structcase inBindQueryParameterWithOptions(andBindRawQueryParameter) should match the string/slice branches next to it:No codegen change is needed — generated code already maps
RequiredParameterErrorto a 400. Users on oapi-codegen ≥ 2.7.0 just need theruntimerelease that includes this fix.mromaszewicz commentedon Jun 20, 2026
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.
Bump runtime to v1.4.2 (oapi-codegen#2419)
Bump runtime to v1.4.2 (oapi-codegen#2419)