Skip to content

Fix FluentValidation not applying to AsParameters type with FromBody#2368

Merged
jeremydmiller merged 1 commit intomainfrom
fix-fluent-validation-asparameters
Mar 29, 2026
Merged

Fix FluentValidation not applying to AsParameters type with FromBody#2368
jeremydmiller merged 1 commit intomainfrom
fix-fluent-validation-asparameters

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

Fixes #2358

  • Root cause: When [AsParameters] is used with a [FromBody] property, AsParametersBindingFrame overwrites chain.RequestType to the body property type. The FluentValidation policy only checked RequestType, so validators on the [AsParameters] type itself were silently ignored.
  • Fix: Added AsParametersType property to HttpChain that tracks the original [AsParameters] type. The HttpChainFluentValidationPolicy now checks for validators on both the RequestType (body type) and the AsParametersType, adding middleware for both when validators exist.
  • Documentation: Updated the validation docs with a new "AsParameters with [FromBody]" section (badged 5.25) showing the combined usage pattern.

Files changed

File Change
HttpChain.cs Added AsParametersType property
AsParametersBindingFrame.cs Sets chain.AsParametersType during parameter binding
HttpChainFluentValidationPolicy.cs Extracted addValidationMiddleware helper; also validates AsParametersType when different from RequestType
FormEndpoints.cs Added test endpoint with [AsParameters] + [FromBody] + validator
asparameters_binding.cs Added 3 new tests (happy path, missing Name, missing body)
validation.md Added documentation section with code sample

Test plan

  • 3 new tests pass (happy path, validation failure on AsParameters type, missing body)
  • All 9 AsParameters binding tests pass
  • All 14 FluentValidation tests pass
  • Full HTTP test suite: 540 passed, 0 failed, 10 skipped (pre-existing)
  • Existing post_body_services_and_route_arguments test still passes (mentioned in issue as potential regression)

🤖 Generated with Claude Code

…present

When [AsParameters] was used with a [FromBody] property, the
AsParametersBindingFrame overwrote chain.RequestType to the body
property type. The FluentValidation policy only checked RequestType,
so validators on the AsParameters type itself were never applied.

The fix tracks the AsParameters type separately on HttpChain and
updates the FluentValidation policy to also add validation middleware
for the AsParameters type when it differs from RequestType and has
a registered validator.

Fixes #2358

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FluentValidation middleware is ignored for HTTP with AsParameters models that contain FromBody attribute

1 participant