-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix UrlAttribute handling of relative Uris #115234
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
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.
Pull Request Overview
This PR addresses the issue where checking the .Scheme property on a relative Uri throws an exception in UrlAttribute, ensuring that only absolute Uris are evaluated.
- Updated UrlAttribute to guard against relative Uris by adding a condition on IsAbsoluteUri
- Expanded test coverage with relative Uri cases to validate the new behavior
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/UrlAttributeTests.cs | Added test cases for relative Uris to ensure exception-free handling |
src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/UrlAttribute.cs | Modified the Uri case to only process absolute Uris to prevent exceptions |
Comments suppressed due to low confidence (3)
src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/UrlAttributeTests.cs:29
- [nitpick] The test case for a relative Uri starting with '/' successfully verifies that relative Uris are now handled without throwing an exception. Consider if additional relative Uri variants might further solidify test coverage.
yield return new TestCase(new UrlAttribute(), new Uri("/foo.png"));
src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/UrlAttributeTests.cs:30
- [nitpick] Including a test for a relative Uri without a leading '/' enhances coverage for various relative Uri formats.
yield return new TestCase(new UrlAttribute(), new Uri("foo.png"));
src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/UrlAttribute.cs:22
- The added guard 'when valueAsUri.IsAbsoluteUri' prevents accessing the Scheme property on relative Uris, addressing the exception. Confirm that all relative Uri scenarios are now appropriately handled by this change.
case Uri valueAsUri when valueAsUri.IsAbsoluteUri:
Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations |
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.
Thanks @MihaZupan
/ba-g The System.Net.Mail failure looks like #131 |
Follow up after #114992
Checking
.Scheme
on a relative Uri throws.Spotted in dotnet/aspnetcore#61745