Skip to content

Add null validation for relativeUri in Uri(Uri, Uri) constructor#123134

Merged
MihaZupan merged 3 commits into
mainfrom
copilot/add-null-validation-uri-constructor
Jan 13, 2026
Merged

Add null validation for relativeUri in Uri(Uri, Uri) constructor#123134
MihaZupan merged 3 commits into
mainfrom
copilot/add-null-validation-uri-constructor

Conversation

Copilot AI commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

Description

The Uri(Uri, Uri) constructor threw NullReferenceException when passed a null relativeUri, instead of ArgumentNullException at parameter validation time.

Changes:

  • Added ArgumentNullException.ThrowIfNull(relativeUri) in constructor (Uri.cs:598)
  • Added test case Uri_Ctor_NullRelativeUri_ThrowsArgumentNullException in UriParameterValidationTest.cs using AssertExtensions.Throws to verify both exception type and parameter name
  • Added using System.Tests; import for AssertExtensions test utilities
// Before: throws NullReferenceException deep in CreateThisFromUri
// After: throws ArgumentNullException immediately
Uri baseUri = new Uri("http://localhost/");
Uri result = new Uri(baseUri, (Uri)null);  // ArgumentNullException with correct parameter name

Customer Impact

Applications relying on proper exception types for error handling would catch NullReferenceException instead of expected ArgumentNullException, making debugging harder and potentially masking programming errors.

Regression

No. This is a latent bug in the API.

Testing

Unit test added to verify ArgumentNullException is thrown with correct parameter name. Test uses AssertExtensions.Throws for 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 baseUri parameter validation on the preceding line. No behavioral changes to valid inputs.

Original prompt

The Uri constructor that accepts two Uri arguments doesn't properly validate that the second argument is not null, leading to a NRE instead of an ArgumentNullException being thrown.
Add the null validation and add a test for this case.


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

…test

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
@MihaZupan MihaZupan added this to the 11.0.0 milestone Jan 13, 2026
@MihaZupan MihaZupan requested a review from a team January 13, 2026 15:55
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Copilot AI changed the title [WIP] Add null validation to Uri constructor with two arguments Jan 13, 2026
Copilot AI requested a review from MihaZupan January 13, 2026 16:12
@MihaZupan MihaZupan marked this pull request as ready for review January 13, 2026 16:13
Copilot AI review requested due to automatic review settings January 13, 2026 16:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 relativeUri parameter in the Uri(Uri, Uri) constructor
  • Added a test case to verify that ArgumentNullException is thrown for null relativeUri

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 MihaZupan enabled auto-merge (squash) January 13, 2026 18:25
@MihaZupan MihaZupan merged commit d5ac199 into main Jan 13, 2026
89 of 91 checks passed
@jkotas jkotas deleted the copilot/add-null-validation-uri-constructor branch January 24, 2026 03:48
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

4 participants