-
Notifications
You must be signed in to change notification settings - Fork 0
✨ Add and verify .NET 10 minimal API validation support with DataAnnotations #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: desjoerd <2460430+desjoerd@users.noreply.github.com>
|
You should use the one from the service collection as it generates a Validator. It also handles nested objects which the default Validator doesnt |
desjoerd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use the one from the service collection as it generates a Validator. It also handles nested objects which the default Validator doesn't. The current added tests are already there. Also only create a few tests (just like the existing ones) just verifying that it works. This is because the annotations are delagating the validation anyway.
…rvice provider support Co-authored-by: desjoerd <2460430+desjoerd@users.noreply.github.com>
Updated in commit 4e27b1d. The tests now use
Updated in commit 4e27b1d. Now using |
…n tests Co-authored-by: desjoerd <2460430+desjoerd@users.noreply.github.com>
desjoerd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a minimal api. Maybe create a host builder and request builder which uses AddValidation and build a minimal api in the test.
I've implemented a minimal API test with host builder in commit [to be added]. However, there's an issue: .NET 10's automatic validation (with or without The error is: Question: Should we:
The DataAnnotations themselves work correctly with the standard |
|
@copilot commit the changes with the failing tests. It needs to compile. But then we've got a reproduction. |
…reference issue with AddValidation Co-authored-by: desjoerd <2460430+desjoerd@users.noreply.github.com>
test/OptionalValues.DataAnnotations.Tests/MinimalApiValidationTest.cs
Outdated
Show resolved
Hide resolved
…alValue Co-authored-by: desjoerd <2460430+desjoerd@users.noreply.github.com>
Done in commit 9387330. The code compiles successfully and tests fail as expected with the circular reference error:
Implemented in commit 9387330. Now using |
…ensure Microsoft.Extensions.Validation ignores validation on that property
|
@copilot I've pushed a change which fixes the issue. Please update the PR description and title to reflect the fix. Also use gitmoji in the title. |
Updated PR title to "✅ Add .NET 10 Minimal API Validation Support for DataAnnotations" and description to reflect the fix. The description now highlights that all tests pass and explains how the |
desjoerd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also update the version to 0.9 in the version.json.
…rence fix Co-authored-by: desjoerd <2460430+desjoerd@users.noreply.github.com>
Updated in commit 4bd6fb3. Version changed from 0.8 to 0.9 in version.json. |
✨ Add and verify .NET 10 minimal API validation support with DataAnnotations
.NET 10 introduced automatic validation for minimal API endpoints using DataAnnotations with
AddValidation(). This PR adds test coverage verifying that OptionalValues DataAnnotations work correctly with .NET 10's minimal API validation mechanism.Changes
MinimalApiValidationTest.cswith 3 focused tests that verify OptionalValue DataAnnotations work with minimal API validation:Microsoft.AspNetCore.Mvc.Testingpackage for .NET 10WebApplication.CreateBuilder()andUseTestServer()for integration testingAddValidation()to enable .NET 10's deep recursive validation[JsonIgnore]attribute toOptionalValue<T>.UnspecifiedpropertyImplementation
Tests create an actual minimal API host with
WebApplication.CreateBuilder()and use .NET 10'sAddValidation()method:Solution to Circular Reference Issue
The initial implementation encountered a circular reference error where .NET 10's deep validator would recursively traverse the
OptionalValue<T>.Unspecifiedproperty infinitely:Fix: Added
[JsonIgnore]attribute to theUnspecifiedstatic property inOptionalValue<T>. This prevents the validation framework from attempting to validate this property, resolving the circular reference issue while maintaining all validation functionality for actual property values.Test Results
✅ All tests pass successfully:
ValidModel_ShouldPass_MinimalApiValidation- Validates that models with correct values pass validationInvalidModel_ShouldFail_MinimalApiValidation- Validates that models with out-of-range values fail validationUnspecifiedOptionalFields_ShouldBeValid_MinimalApiValidation- Validates that unspecified optional fields are handled correctlyOptionalValues DataAnnotations now work seamlessly with .NET 10's minimal API automatic validation using
AddValidation().Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.