Skip to content

Conversation

oroztocil
Copy link
Member

@oroztocil oroztocil commented Sep 15, 2025

The PR solves two issues that prevent the new validation from working in Blazor Wasm SDK projects:

  • The Microsoft.Extensions.Validation NuGet package needs to include the source generator, so that it is available in Blazor Wasm SDK projects (which are not based on the ASP.NET Core runtime pack, unlike the Web SDK projects).
  • The various checks for "well-known types" throw when the types are not found in the assemblies (e.g. Microsoft.AspNetCore.Http.Abstractions is not present in Blazor Wasm SDK projects). To avoid the need to refactor larger portion of the existing code, the PR solves this by returning a type symbol for a dummy type instead of throwing. This type symbol is then used in place of the missing types and comparisons against it always return false, thus providing the same outer behavior.
    • Idea: We could differentiate between "required" well-known types for which we would throw if not present in the compiled assemblies, and "optional" well-known types for which we would use this trick with a dummy type, or something functionally equivalent.

Fixes #63670

@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Sep 15, 2025
@oroztocil oroztocil added area-blazor Includes: Blazor, Razor Components feature-validation Issues related to model validation in minimal and controller-based APIs and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Sep 15, 2025
@oroztocil oroztocil force-pushed the oroztocil/blazor-wasm-validation-fix-wip branch from 8708102 to 1bee48c Compare September 16, 2025 16:38
@oroztocil oroztocil marked this pull request as ready for review September 16, 2025 16:44
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 fixes validation source generator support for Blazor WebAssembly SDK projects by ensuring the generator is packaged with the NuGet package and handling missing well-known types gracefully. The changes allow the validation functionality to work in environments where certain ASP.NET Core assemblies are not available.

  • Package the validation source generator in the NuGet package for Blazor WASM compatibility
  • Replace exception throwing with dummy type symbols for missing well-known types
  • Introduce a fallback mechanism using MissingType when assemblies are not present

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Validation/src/Microsoft.Extensions.Validation.csproj Adds project reference and packaging configuration to include the validation source generator in the NuGet package
src/Shared/RoslynUtils/WellKnownTypes.cs Replaces exception throwing with dummy type symbol fallback when well-known types cannot be resolved

{
_lazyWellKnownTypes = new INamedTypeSymbol?[WellKnownTypeData.WellKnownTypeNames.Length];
_compilation = compilation;
_missingTypeSymbol = compilation.GetTypeByMetadataName(typeof(MissingType).FullName!)!;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should consider reusing SpecialType.None as the fallback in this case as Roslyn does in their WellKnownType implementation (ref). That let's us reuse an existing concept instead of defining out own marker type.

Copy link
Member Author

@oroztocil oroztocil Sep 17, 2025

Choose a reason for hiding this comment

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

Compilation.GetSpecialType throws out-of-range exception if you try to resolve INamedTypeSymbol for SpecialType.None. The way CodeAnalysis.WellKnownTypes is set up in Roslyn, there is no actual type symbol associated with an unknown type. So if I am not overlooking something, we can't use it as a marker type for our purposes.

@oroztocil
Copy link
Member Author

/backport to release/10.0

Copy link
Contributor

Started backporting to release/10.0: https://github.com/dotnet/aspnetcore/actions/runs/17827846351

Copy link
Member

@captainsafia captainsafia left a comment

Choose a reason for hiding this comment

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

LGTM! The WellKnownTypes are shared across all our analyzers.

I imagine the material impact here is that scenarios that previously threw exceptions/gold-barred will now fail silently assuming the logic where the symbol comparisons happen is sane.

Some smoke testing on our framework analyzers should give us confidence here.

@oroztocil
Copy link
Member Author

oroztocil commented Sep 18, 2025

Based on offline discussion with @captainsafia I updated the PR so that other analyzers are not affected by the change in type symbol retrieval logic.

I added the WellKnownTypes.GetOptional method that does not throw for missing types but returns the symbol for a special marker type. This is used by the Validation generator where needed to ensure compatibility with the Blazor Wasm SDK.

The existing WellKnownTypes.Get method keeps the same throwing behavior as before.

@oroztocil
Copy link
Member Author

/backport to release/10.0

Copy link
Contributor

Started backporting to release/10.0: https://github.com/dotnet/aspnetcore/actions/runs/17852955682

Copy link
Contributor

@oroztocil backporting to "release/10.0" failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Proof-of-concept fix for getting validation SG to work in Blazor Wasm SDK projects
Using index info to reconstruct a base tree...
M	src/Shared/RoslynUtils/SymbolExtensions.cs
M	src/Shared/RoslynUtils/WellKnownTypes.cs
M	src/Validation/gen/Extensions/ITypeSymbolExtensions.cs
M	src/Validation/src/Microsoft.Extensions.Validation.csproj
Falling back to patching base and 3-way merge...
Auto-merging src/Shared/RoslynUtils/SymbolExtensions.cs
Auto-merging src/Shared/RoslynUtils/WellKnownTypes.cs
CONFLICT (content): Merge conflict in src/Shared/RoslynUtils/WellKnownTypes.cs
Auto-merging src/Validation/gen/Extensions/ITypeSymbolExtensions.cs
Auto-merging src/Validation/src/Microsoft.Extensions.Validation.csproj
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Proof-of-concept fix for getting validation SG to work in Blazor Wasm SDK projects
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@oroztocil oroztocil merged commit f4f4d8d into main Sep 19, 2025
30 checks passed
@oroztocil oroztocil deleted the oroztocil/blazor-wasm-validation-fix-wip branch September 19, 2025 08:40
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components feature-validation Issues related to model validation in minimal and controller-based APIs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ValidatableType complex modal validation is not working with Blazor Web Assembly (Standalone) 10 RC1
2 participants