From 547a648429e3101a10f276ad0a09619492ec842e Mon Sep 17 00:00:00 2001 From: Xavier John <1859710+xavierjohn@users.noreply.github.com> Date: Sat, 6 Jun 2026 23:34:05 -0700 Subject: [PATCH] Add regression test ensuring user-registered IProblemDetailsWriter is preserved by AddApiVersioning. Related to #1191 The Rfc7231ProblemDetailsWriter wrapper introduced in 7b4cb60 (released in 8.1.1) was removed for .NET 10 in 9e5f4d34 and is no longer in v10.0.0, but the repo had no test guarding against reintroduction. This test snapshots the IProblemDetailsWriter descriptors after registering AddProblemDetails() and a custom singleton writer, then calls AddApiVersioning() and asserts the sequence of writer descriptors is unchanged and the custom instance is still resolvable from DI. Verified the assertion fails when the old in-place replace workaround is reintroduced. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../IServiceCollectionExtensionsTest.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/AspNetCore/WebApi/test/Asp.Versioning.Http.Tests/DependencyInjection/IServiceCollectionExtensionsTest.cs b/src/AspNetCore/WebApi/test/Asp.Versioning.Http.Tests/DependencyInjection/IServiceCollectionExtensionsTest.cs index 094b2c7c..8e7f2219 100644 --- a/src/AspNetCore/WebApi/test/Asp.Versioning.Http.Tests/DependencyInjection/IServiceCollectionExtensionsTest.cs +++ b/src/AspNetCore/WebApi/test/Asp.Versioning.Http.Tests/DependencyInjection/IServiceCollectionExtensionsTest.cs @@ -5,6 +5,7 @@ namespace Microsoft.Extensions.DependencyInjection; using Asp.Versioning; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; public class IServiceCollectionExtensionsTest @@ -25,4 +26,35 @@ public void add_api_versioning_should_not_allow_default_neutral_api_version() // assert options.Should().Throw(); } + + // REF: https://github.com/dotnet/aspnet-api-versioning/issues/1191 + [Fact] + public void add_api_versioning_should_not_displace_or_wrap_user_registered_problem_details_writers() + { + // arrange + var services = new ServiceCollection(); + var customWriter = new TestProblemDetailsWriter(); + + services.AddProblemDetails(); + services.AddSingleton( customWriter ); + + var writersBefore = services.Where( s => s.ServiceType == typeof( IProblemDetailsWriter ) ).ToArray(); + + // act + services.AddApiVersioning(); + + // assert + var writersAfter = services.Where( s => s.ServiceType == typeof( IProblemDetailsWriter ) ).ToArray(); + writersAfter.Should().Equal( writersBefore ); + + using var provider = services.BuildServiceProvider(); + provider.GetServices().Should().Contain( customWriter ); + } + + private sealed class TestProblemDetailsWriter : IProblemDetailsWriter + { + public bool CanWrite( ProblemDetailsContext context ) => true; + + public ValueTask WriteAsync( ProblemDetailsContext context ) => ValueTask.CompletedTask; + } } \ No newline at end of file