From 51e08147b89f1f2e09602db1df10d69902d70792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20Szepczy=C5=84ski?= Date: Tue, 21 Apr 2026 08:06:08 +0000 Subject: [PATCH] [Validation]: tighten endpoint-filter gate so it only emits when AddEndpointFilter resolves MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously the generator emitted ValidationEndpointFilter.g.cs whenever `Microsoft.AspNetCore.Http.Results` resolved in the compilation. That type lives in Microsoft.AspNetCore.Http.Results.dll, while the generated source also calls `RouteHandlerBuilder.AddEndpointFilter(...)` which lives in Microsoft.AspNetCore.Http.Extensions.dll (class EndpointFilterExtensions). Test projects that pulled `Results` in transitively — e.g. via a ProjectReference to an ASP.NET Core web sample used by WebApplicationFactory — but did not wire up the full minimal-API extension surface tripped the gate and then failed to compile with: RouteHandlerBuilder does not contain a definition for AddEndpointFilter Gate now requires all three types the emitted source uses — RouteHandlerBuilder, Results, and EndpointFilterExtensions — so the filter source is only emitted when the compilation actually has a chance of compiling it. --- .../ValidationGenerator.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/ZibStack.NET.Validation/src/ZibStack.NET.Validation/ValidationGenerator.cs b/packages/ZibStack.NET.Validation/src/ZibStack.NET.Validation/ValidationGenerator.cs index c18b9ea..2fe2196 100644 --- a/packages/ZibStack.NET.Validation/src/ZibStack.NET.Validation/ValidationGenerator.cs +++ b/packages/ZibStack.NET.Validation/src/ZibStack.NET.Validation/ValidationGenerator.cs @@ -32,10 +32,23 @@ public void Initialize(IncrementalGeneratorInitializationContext context) ctx.AddSource("IValidationConfigurator.g.cs", CrossFieldInterfacesSource); }); - // Emit ASP.NET endpoint filter only when Microsoft.AspNetCore.Http is referenced + // Emit the ASP.NET Core endpoint filter only when EVERY type the generated + // source references is actually resolvable in this compilation. Previously the + // gate only checked `Microsoft.AspNetCore.Http.Results`, so any project that + // happened to transitively pull that assembly in (e.g. tests referencing an + // ASP.NET Core web project for WebApplicationFactory, but not wiring the full + // minimal-API extension surface) hit a compile error: + // `RouteHandlerBuilder does not contain a definition for AddEndpointFilter`. + // The three types below live in three separate assemblies — RouteHandlerBuilder + // in Microsoft.AspNetCore.Routing, Results in Microsoft.AspNetCore.Http.Results, + // EndpointFilterExtensions (which supplies AddEndpointFilter) in + // Microsoft.AspNetCore.Http.Extensions — so all three must be present before + // the filter source can compile. context.RegisterSourceOutput( context.CompilationProvider.Select(static (comp, _) => - comp.GetTypeByMetadataName("Microsoft.AspNetCore.Http.Results") is not null), + comp.GetTypeByMetadataName("Microsoft.AspNetCore.Http.EndpointFilterExtensions") is not null + && comp.GetTypeByMetadataName("Microsoft.AspNetCore.Http.Results") is not null + && comp.GetTypeByMetadataName("Microsoft.AspNetCore.Builder.RouteHandlerBuilder") is not null), static (spc, hasAspNet) => { if (hasAspNet)