openapi: fix request validation for application/x-www-form-urlencoded#256
openapi: fix request validation for application/x-www-form-urlencoded#256
Conversation
… content type Co-authored-by: intel352 <77607+intel352@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes OpenAPI request-body validation to respect Content-Type when validation.request: true, adding support for validating application/x-www-form-urlencoded bodies instead of always treating bodies as JSON.
Changes:
- Branch request-body parsing/validation by content type, using
url.ParseQueryforapplication/x-www-form-urlencoded. - Add
validateFormBodyto validate form fields against the requestBody schema using existing scalar validation logic. - Add unit test coverage for form-encoded request validation (valid body, missing required field, empty required body).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
module/openapi.go |
Adds content-type aware request-body validation and new validateFormBody helper for form data. |
module/openapi_test.go |
Adds a focused test for form-encoded request-body validation scenarios. |
module/openapi.go
Outdated
| val := values.Get(field) | ||
| if val == "" { | ||
| continue | ||
| } |
There was a problem hiding this comment.
In validateFormBody, val == "" causes the code to skip validation entirely for present-but-empty form fields. That means constraints like minLength, pattern, and enum are not enforced on empty strings, and a required string field can be sent as Body= without any schema validation running. Consider validating when the key is present in values (even if the value is empty), and only skipping when the key is absent.
| val := values.Get(field) | |
| if val == "" { | |
| continue | |
| } | |
| vals, present := values[field] | |
| if !present { | |
| continue | |
| } | |
| var val string | |
| if len(vals) > 0 { | |
| val = vals[0] | |
| } |
module/openapi.go
Outdated
| if fieldErrs := validateScalarValue(val, field, propSchema); len(fieldErrs) > 0 { | ||
| errs = append(errs, fieldErrs...) | ||
| } |
There was a problem hiding this comment.
validateFormBody reuses validateScalarValue, but validateScalarValue hardcodes error text as "parameter ..." and uses "parameter" when applying string constraints. For request body form fields this produces misleading messages (they’re fields, not parameters) and is inconsistent with JSON body validation which reports "field" errors. Consider adding a field-specific scalar validator (or parameterize validateScalarValue with a kind) so form-body errors are labeled correctly.
|
@copilot apply changes based on the comments in this thread |
…' in errors Co-authored-by: intel352 <77607+intel352@users.noreply.github.com>
Applied both fixes in ca2cec3:
|
net/urlto imports inmodule/openapi.govalidateFormBodyfunction to validateurl.Valuesagainst an object schemavalidateto handleapplication/x-www-form-urlencodedby parsing form data instead of JSONkindparameter tovalidateScalarValueso form body errors say "field" not "parameter"validateFormBodyto validate present-but-empty form fields (check key presence, not empty string)TestOpenAPIModule_RequestValidation_FormEncodedtest with subtests: valid form body, missing required field, empty body when required, present-but-empty field violates minLengthOriginal prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.