Add AOT compatibility annotations and flags for FluentValidation#67
Add AOT compatibility annotations and flags for FluentValidation#67devtekve wants to merge 1 commit intoSharpGrip:masterfrom
Conversation
- Mark relevant methods and properties with `[RequiresUnreferencedCode]` or `[DynamicDependency]` to support Ahead-Of-Time (AOT) compilation. - Add `<IsAotCompatible>` property to project files for compatibility metadata. - Suppress warnings where validators are expected to be preserved by user annotations.
NandanDevHub
left a comment
There was a problem hiding this comment.
Hello i was just going thorugh, saw few things that can be reviewed again -
1) Tighten the trimming contract on the generic constraint
In
AutoValidationEndpointsConfiguration.cs, you annotateTResultFactorywithDynamicallyAccessedMembers.PublicConstructors, but if the factory is only ever created with a parameterless ctor, you should narrow this to PublicParameterlessConstructor (smaller linker root + fewer false positives).- public void OverrideDefaultResultFactoryWith<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TResultFactory>() + public void OverrideDefaultResultFactoryWith<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResultFactory>() where TResultFactory : IFluentValidationAutoValidationResultFactoryRef: guidance on matching the attribute to what
Activator.CreateInstanceactually needs.
2) Mark the reflection heavy helper with RequiresUnreferencedCode
ServiceProviderExtensions.GetValidator(IServiceProvider, Type)constructsIValidator<>at runtime viaMakeGenericTypeand DI lookup. This is trim-unsafe for NativeAOT unless consumers preserve their validators. You can add:[RequiresUnreferencedCode("Resolves validators via reflection; consumers must preserve concrete validator types for AOT (eg. DynamicDependency or link.xml).")] public static object? GetValidator(this IServiceProvider serviceProvider, Type type)(You already suppress IL3050 in one place, this adds the caller visible warning so AOT apps don’t get surprised later XD)
3) Fix the sample attribute text in XML docs
In
EndpointRouteExtensions.csremarks, the sample showstypeof(YourValidator!)— the!insidetypeof(...)isn’t valid C#.
Suggest:- [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(YourValidator!))] + [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(YourValidator))]you can also consider clarifying that open-generics like
IValidator<>can’t be preserved this way, consumers i think must reference concrete validators or use descriptors.
[RequiresUnreferencedCode]or[DynamicDependency]to support Ahead-Of-Time (AOT) compilation.<IsAotCompatible>property to project files for compatibility metadata.