Skip to content

Conversation

devtekve
Copy link

  • 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.
image

- 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.
@devtekve devtekve mentioned this pull request Jul 13, 2025
Copy link

@NandanDevHub NandanDevHub left a comment

Choose a reason for hiding this comment

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

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 annotate TResultFactory with DynamicallyAccessedMembers.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 : IFluentValidationAutoValidationResultFactory

Ref: guidance on matching the attribute to what Activator.CreateInstance actually needs.

2) Mark the reflection heavy helper with RequiresUnreferencedCode

ServiceProviderExtensions.GetValidator(IServiceProvider, Type) constructs IValidator<> at runtime via MakeGenericType and 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.cs remarks, the sample shows typeof(YourValidator!) — the ! inside typeof(...) 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants