Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 4 commits into from
May 3, 2025
Merged

Conversation

MihaZupan
Copy link
Member

Follow up after #114992

Checking .Scheme on a relative Uri throws.

Spotted in dotnet/aspnetcore#61745

Copy link
Contributor

@Copilot 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 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:

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

Thanks @MihaZupan

@MihaZupan
Copy link
Member Author

/ba-g The System.Net.Mail failure looks like #131

@MihaZupan MihaZupan merged commit 4a75bea into main May 3, 2025
83 of 85 checks passed
@MihaZupan MihaZupan deleted the urlattribute-absolute branch May 3, 2025 20:59
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.

2 participants