Repository navigation
Conversation
|
Thanks for your PR, @@ANcpLua. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug in the ValidationsGenerator source generator where generic type parameters (like TRequest from a generic endpoint extension method) were either silently skipped or caused compilation errors when they leaked into generated typeof() expressions. The fix adds type parameter handling to discover validatable types through constraint types, while preventing type parameters from appearing in emitted code.
Changes:
- Added
ITypeParameterSymbolbranch inTryExtractValidatableTypeto walk constraint types and discover validatable types - Added
ContainsTypeParameterguards in both record and regular property extraction paths to prevent emittingtypeof(T)expressions - Added
ContainsTypeParameterhelper method to recursively detect type parameters in type trees
@dotnet-policy-service agree |
|
CI failures are pre-existing infrastructure flakes, unrelated to this change:
Happy to re-run if needed. |
|
Commenter does not have sufficient privileges for PR 65419 in repo dotnet/aspnetcore |
1 similar comment
|
Commenter does not have sufficient privileges for PR 65419 in repo dotnet/aspnetcore |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Youssef1313
left a comment
There was a problem hiding this comment.
The added test seems to already be passing without the changes.
@ANcpLua Can you please rebase your branch on latest main, and ensure to create a test case that fails on main to better demonstrate the issue?
|
@copilot apply changes based on the comments in this thread |
|
@Youssef1313 thanks for the review — pushed 1. Linux x64 / Source-Build CS1615 The most recent merge from 2. Test that fails on You were right that public static RouteHandlerBuilder MapValidated<T>(this IEndpointRouteBuilder endpoints, string pattern)
where T : UserRequest
=> endpoints.MapPost(pattern, (T req) => Results.Ok());The inner + if (type == typeof(global::UserRequest))
+ {
+ validatableInfo = new GeneratedValidatableTypeInfo(
+ type: typeof(global::UserRequest),
+ members: [
+ new GeneratedValidatablePropertyInfo(
+ containingType: typeof(global::UserRequest),
+ propertyType: typeof(string),
+ name: "Name",
+ displayName: "Name"
+ ),
+ ... (Age member) ...
+ ]
+ );
+ return true;
+ }With the fix the Runtime endpoint behavior is identical fixed-vs-unfixed because the framework falls back to reflection-based validation when the static resolver returns false, so the snapshot is the only deterministic signal. The runtime Locally |
|
@Youssef1313 All review threads now have replies, and the unrelated Re-ran validation via close/reopen: zero failing tests on the rerun. The remaining red legs are infrastructure only — macOS arm64 matched to known issues dotnet/dnceng#1883 / dotnet/dnceng#3008, and the two Helix x64 subsets hit 240-minute agent timeouts (queue congestion; no test results, nothing failed). I don't have Ready for another look — thanks for the patience on this one. |
|
Pushed
Local: GeneratorTests 49/49, Validation.Tests 81/81. One deliberate non-goal, flagged for review: skipping registration makes |
|
Merged latest Also on the branch since the last review: a type-level guard extension covering the All six PR-owned snapshots were regenerated against the new emitter (async validation, |
ANcpLua
left a comment
There was a problem hiding this comment.
Sorry, these replies were sitting in a pending review the whole time. All threads answered, two resolved. Let me know what you think on the constraint walking point. Can split that part out if you prefer.
|
@Youssef1313 checking back in on this one. Both items from your review are done:
All four threads are resolved. The one thing I do not have your read on is the constraint-walking half (your |
592768b to
7281d6b
Compare
|
I've cut this PR down to the one thing that is provably broken, redeeming the split I offered in July. The previous revision bundled two changes — the emission-safety fix and constraint-walking discovery — and that mix is what stalled review. The criticism was fair: walking What remains is one early guard and two tests that fail on main with CS0246 in the generated code. The member-level guards from the earlier revision are gone too: with the type-level guard applied up front they were unreachable — members of a closed construction are fully substituted — and the test suites confirm it (GeneratorTests 55/55, Validation.Tests 134/134). Net: 4 files instead of 8, +57 lines of generator code, no behavior change for any closed type. Retitled accordingly; this addresses the open-generic emission failure in #65418 rather than closing the whole issue. |
The generated resolver is non-generic: every typeof(...) it emits must name a fully closed type. TryExtractValidatableType now rejects any symbol whose type tree still contains a type parameter — bare T, open Wrapper<T>, T[] and List<T> through type arguments, and Outer<T>.Inner through its containing type. On main these shapes emit typeof(global::Wrapper<T>) / typeof(T) and the generated code fails to compile (CS0246). Co-Authored-By: Claude Fable 5 <[email protected]>
Both fail on main with CS0246 in the generated resolver: [ValidatableType] on an open Wrapper<T>, and [ValidatableType] on Outer<T>.Inner whose emitted name carries the outer's parameter. The closed constructions Wrapper<string> and Outer<string>.Inner keep registering and validating. Co-Authored-By: Claude Fable 5 <[email protected]>
7281d6b to
2608a57
Compare
…validations-generator-type-parameter-handling
…main Main replaced the MessageKeyProvider lookup with the convention-based FindLocalizedTemplate/GetKeySegment emission; no registration changes. Co-Authored-By: Claude Fable 5 <[email protected]>
Per the dotnet#65418 discussion, the silent skip becomes an author-facing diagnostic: ASP0039 reports at the declaration when a [ValidatableType] type tree still contains a type parameter (its own, or an enclosing type's). Lives in the ValidationsDiagnosticAnalyzer introduced in dotnet#67993 and reuses the generator's ContainsTypeParameter walk so the analyzer and the emit-time guard cannot drift apart. ID and wording are placeholders for review. The guard itself stays as the safety net for shapes no attribute-based analyzer can see: post-dotnet#67821 a generic registration helper forwarding Func<Command<TRequest>, IResult> to MapPost hands the parser the open Command<TRequest>, which passes the accessibility check and would reach emission as typeof(global::Command<TRequest>). A regression test covers that shape end to end. Co-Authored-By: Claude Fable 5 <[email protected]>
A diagnostic that exists to end the silent skip should not call the skip silent once it fires; the message now says "skipped by the validation source generator", matching ASP0035's phrasing without the contradiction. Also normalizes punctuation in nearby comments. Co-Authored-By: Claude Fable 5 <[email protected]>
|
@Youssef1313 rewrote as discussed on #65418: ASP0039 in the analyzer from #67993 (reuses the generator's ContainsTypeParameter walk so diagnostic and guard cannot drift), guard kept as emit-time safety net, regression test for the delegate-parameter shape included. Severity is Warning for consistency with ASP0033-0038; happy to bump to an error if you prefer. PTAL. |
ASP0039 is now used on main by the experimental passkey endpoints APIs.
…validations-generator-type-parameter-handling # Conflicts: # docs/list-of-diagnostics.md
Fixes #65418
The validations source generator emits a non-generic
ValidatableInfoResolver, so everytypeof(...)it writes must name a fully closed type. Today an open generic can reach emission and producetypeof(Wrapper<T>)/typeof(T): CS0246 in generated code the user can't act on.Following the discussion with @Youssef1313 on #65418, this PR now has two halves:
1. ASP0040 analyzer diagnostic (author-facing)
[ValidatableType]on a type whose type tree still contains a type parameter (its own, as inWrapper<T>, or an enclosing type's, as inOuter<T>.Inner) can never be handled statically. Instead of silently skipping, the analyzer introduced in #67993 now reports ASP0040 at the declaration:The check reuses the generator's
ContainsTypeParameterwalk, so the diagnostic and the generator's behaviour cannot drift apart. ID and wording are placeholders (happy to rename in review), and severity isWarningto stay consistent with ASP0033-0038; if you'd rather make this one an error, as your #65418 comment suggests, I'm happy to bump it.2. Emit-time guard (safety net)
TryExtractValidatableTypeskips any symbol whose type tree contains a type parameter. This covers shapes no attribute-based analyzer can see: since #67821 resolves delegate-typed handler parameters, a generic registration helper (MapCommand<TRequest>forwardingFunc<Command<TRequest>, IResult>toMapPost) hands the parser the openCommand<TRequest>, which passes the accessibility check and, without the guard, reaches emission astypeof(global::Command<TRequest>). With the guard, such shapes degrade to "no validation" instead of a build break; a regression test covers the delegate-parameter shape end to end.Behaviour table
[ValidatableType]onWrapper<T>[ValidatableType]onOuter<T>.InnerWrapper<string>endpoint parameter (closed)Func<Command<TRequest>, ...>via generic helper (post-#67821)Closed constructions at endpoints are covered by existing tests; the two pre-existing snapshots only changed by main's localization emitter update (
FindLocalizedTemplate), regenerated in a separate commit.