Add null validation for relativeUri in Uri(Uri, Uri) constructor#123134
Merged
Conversation
…test Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/ncl |
Copilot
AI
changed the title
[WIP] Add null validation to Uri constructor with two arguments
Jan 13, 2026
MihaZupan
approved these changes
Jan 13, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug where the Uri(Uri, Uri) constructor threw NullReferenceException instead of ArgumentNullException when passed a null relativeUri parameter.
Changes:
- Added null validation for the
relativeUriparameter in theUri(Uri, Uri)constructor - Added a test case to verify that
ArgumentNullExceptionis thrown for nullrelativeUri
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Private.Uri/src/System/Uri.cs | Added ArgumentNullException.ThrowIfNull(relativeUri) validation in the Uri(Uri, Uri) constructor immediately after the existing baseUri validation |
| src/libraries/System.Private.Uri/tests/FunctionalTests/UriParameterValidationTest.cs | Added test case Uri_Ctor_NullRelativeUri_ThrowsArgumentNullException to verify proper exception type is thrown |
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
MihaZupan
approved these changes
Jan 13, 2026
wfurt
approved these changes
Jan 13, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
The
Uri(Uri, Uri)constructor threwNullReferenceExceptionwhen passed a nullrelativeUri, instead ofArgumentNullExceptionat parameter validation time.Changes:
ArgumentNullException.ThrowIfNull(relativeUri)in constructor (Uri.cs:598)Uri_Ctor_NullRelativeUri_ThrowsArgumentNullExceptionin UriParameterValidationTest.cs usingAssertExtensions.Throwsto verify both exception type and parameter nameusing System.Tests;import for AssertExtensions test utilitiesCustomer Impact
Applications relying on proper exception types for error handling would catch
NullReferenceExceptioninstead of expectedArgumentNullException, making debugging harder and potentially masking programming errors.Regression
No. This is a latent bug in the API.
Testing
Unit test added to verify
ArgumentNullExceptionis thrown with correct parameter name. Test usesAssertExtensions.Throwsfor stronger validation, matching patterns used in other test files (e.g., UriBuilderTests.cs, UriEscapingTest.cs). Existing Uri tests continue to pass.Risk
Minimal. Single-line parameter validation added at entry point, following identical pattern used for
baseUriparameter validation on the preceding line. No behavioral changes to valid inputs.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.