Fix minimal API validation for record structs#64514
Merged
captainsafia merged 2 commits intomainfrom Nov 24, 2025
Merged
Conversation
…IsComplexType Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix minimal API validation for record struct
Fix minimal API validation for record structs
Nov 24, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where the built-in validation from AddValidation was not working with record struct types. The issue was that the validation system only checked for reference types (classes) when determining if a type was complex enough to require property-level validation.
Key changes:
- Renamed
IsClasstoIsComplexTypeto accurately reflect the method's purpose - Updated validation logic to include value types (structs, record structs) in addition to reference types
- Added comprehensive test coverage for record struct validation including nested structures and various validation attributes
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Validation/src/RuntimeValidatableParameterInfoResolver.cs | Renamed method from IsClass to IsComplexType and updated logic to return true for both classes and value types (excluding primitives and framework types), enabling validation of record structs |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.RecordType.cs | Added comprehensive test CanValidateRecordStructTypes that validates record structs with Range, Required, StringLength, and Display attributes, including nested record struct validation |
| src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateRecordStructTypes#ValidatableInfoResolver.g.verified.cs | Generated snapshot file showing the validation resolver code for the new record struct test scenarios |
BrennanConroy
approved these changes
Nov 24, 2025
| return type.IsClass; | ||
| // Complex types include both reference types (classes) and value types (structs, record structs) | ||
| // that aren't in the exclusion list above | ||
| return type.IsClass || type.IsValueType; |
Member
There was a problem hiding this comment.
At this point isn't it just return true;?
Contributor
There was a problem hiding this comment.
I think there are some cases that would still be false here, the notable one being interfaces.
Contributor
|
/backport to release/10.0 |
Contributor
|
Started backporting to |
10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The built-in validation from
AddValidationwas not validatingrecord structtypes because the method checking for complex types only returnedtruefor reference types (classes), excluding all value types including record structs.Changes
IsClasstoIsComplexTypeinRuntimeValidatableParameterInfoResolver.csto reflect actual behaviortype.IsClasstotype.IsClass || type.IsValueTypeExample
The method still excludes primitives, enums, and framework types like
DateTime,Guid,CancellationToken, etc. via the existing exclusion list.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.