Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 24, 2025

The built-in validation from AddValidation was not validating record struct types because the method checking for complex types only returned true for reference types (classes), excluding all value types including record structs.

Changes

  • Renamed IsClass to IsComplexType in RuntimeValidatableParameterInfoResolver.cs to reflect actual behavior
  • Updated logic to include value types: changed from type.IsClass to type.IsClass || type.IsValueType
  • Added test coverage for record struct validation scenarios including nested structs and property-level validation attributes

Example

// This now validates correctly
public record struct ValidatableRecordStruct(
    [Range(10, 100)] int Value,
    [Required] string Name
);

app.MapPost("/endpoint", (ValidatableRecordStruct request) => Results.Ok());

The method still excludes primitives, enums, and framework types like DateTime, Guid, CancellationToken, etc. via the existing exclusion list.

Original prompt

This section details on the original issue you should resolve

<issue_title>Minimal API validation does not work with record struct</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The built-in validation provided by AddValidation does not take into account record struct.

It could be related to this check?

private static bool IsClass(Type type)

Steps To Reproduce

Minimal repo at https://github.com/lkurzyniec/dotnet10-validation/

.NET Version

10.0.100</issue_description>

<agent_instructions>Fix this issue by renaming this class to IsComplexType and update the implementation to return true for types that are record struct.

Add test cases to https://github.com/dotnet/aspnetcore/blob/main/src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.RecordType.cs to validate the validation behavior on record structs.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…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
Copilot AI requested a review from captainsafia November 24, 2025 17:29
Copilot finished work on behalf of captainsafia November 24, 2025 17:29
@captainsafia captainsafia marked this pull request as ready for review November 24, 2025 20:26
Copilot AI review requested due to automatic review settings November 24, 2025 20:26
Copilot finished reviewing on behalf of captainsafia November 24, 2025 20:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 IsClass to IsComplexType to 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

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point isn't it just return true;?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some cases that would still be false here, the notable one being interfaces.

@captainsafia captainsafia merged commit cb092d8 into main Nov 24, 2025
35 of 36 checks passed
@captainsafia captainsafia deleted the copilot/fix-minimal-api-validation branch November 24, 2025 21:45
@captainsafia
Copy link
Member

/backport to release/10.0

@github-actions
Copy link
Contributor

Started backporting to release/10.0 (link to workflow run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Minimal API validation does not work with record struct

3 participants