From 959ed0ac92bf19a4b38ed1811f5b4d4553ca9343 Mon Sep 17 00:00:00 2001 From: Jose Perez Rodriguez Date: Wed, 19 May 2021 11:18:49 -0700 Subject: [PATCH] Resolve ILLink warning on Microsoft.Extensions.Options.DataAnnotations (#52721) * Resolve ILLink warning on Microsoft.Extensions.Options.DataAnnotations * Fix build and add annotations to TOptions to try to keep as much as we can --- .../Microsoft.Extensions.Options.DataAnnotations.cs | 6 ++++-- ...icrosoft.Extensions.Options.DataAnnotations.csproj | 3 +++ .../src/DataAnnotationValidateOptions.cs | 8 +++++++- .../src/ILLink/ILLink.Suppressions.xml | 11 ----------- ...icrosoft.Extensions.Options.DataAnnotations.csproj | 5 +++++ .../src/OptionsBuilderDataAnnotationsExtensions.cs | 5 ++++- 6 files changed, 23 insertions(+), 15 deletions(-) delete mode 100644 src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/ILLink/ILLink.Suppressions.xml diff --git a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.cs b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.cs index 3a993b9d4173f..a75af9f995e7b 100644 --- a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.cs +++ b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.cs @@ -8,13 +8,15 @@ namespace Microsoft.Extensions.DependencyInjection { public static partial class OptionsBuilderDataAnnotationsExtensions { - public static Microsoft.Extensions.Options.OptionsBuilder ValidateDataAnnotations(this Microsoft.Extensions.Options.OptionsBuilder optionsBuilder) where TOptions : class { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Uses DataAnnotationValidateOptions which is unsafe given that the options type passed in when calling Validate cannot be statically analyzed so its members may be trimmed.")] + public static Microsoft.Extensions.Options.OptionsBuilder ValidateDataAnnotations<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties)] TOptions>(this Microsoft.Extensions.Options.OptionsBuilder optionsBuilder) where TOptions : class { throw null; } } } namespace Microsoft.Extensions.Options { - public partial class DataAnnotationValidateOptions : Microsoft.Extensions.Options.IValidateOptions where TOptions : class + public partial class DataAnnotationValidateOptions<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties)] TOptions> : Microsoft.Extensions.Options.IValidateOptions where TOptions : class { + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The implementation of Validate method on this type will walk through all properties of the passed in options object, and its type cannot be statically analyzed so its members may be trimmed.")] public DataAnnotationValidateOptions(string name) { } public string Name { get { throw null; } } public Microsoft.Extensions.Options.ValidateOptionsResult Validate(string name, TOptions options) { throw null; } diff --git a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.csproj b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.csproj index c7fe04287c373..3c1c982479cf1 100644 --- a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.csproj +++ b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/ref/Microsoft.Extensions.Options.DataAnnotations.csproj @@ -4,6 +4,9 @@ + + + diff --git a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs index 5229ca1c43436..fee4bca8cad4b 100644 --- a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs +++ b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.Extensions.Options { @@ -11,12 +12,15 @@ namespace Microsoft.Extensions.Options /// Implementation of that uses DataAnnotation's for validation. /// /// The instance being validated. - public class DataAnnotationValidateOptions : IValidateOptions where TOptions : class + public class DataAnnotationValidateOptions<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] TOptions> + : IValidateOptions where TOptions : class { /// /// Constructor. /// /// The name of the option. + [RequiresUnreferencedCode("The implementation of Validate method on this type will walk through all properties of the passed in options object, and its type cannot be " + + "statically analyzed so its members may be trimmed.")] public DataAnnotationValidateOptions(string name) { Name = name; @@ -33,6 +37,8 @@ public DataAnnotationValidateOptions(string name) /// The name of the options instance being validated. /// The options instance. /// The result. + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Suppressing the warnings on this method since the constructor of the type is annotated as RequiresUnreferencedCode.")] public ValidateOptionsResult Validate(string name, TOptions options) { // Null name is used to configure all named options. diff --git a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/ILLink/ILLink.Suppressions.xml b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/ILLink/ILLink.Suppressions.xml deleted file mode 100644 index 2d629e7eb906b..0000000000000 --- a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/ILLink/ILLink.Suppressions.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - ILLink - IL2026 - member - M:Microsoft.Extensions.Options.DataAnnotationValidateOptions`1.Validate(System.String,`0) - - - \ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/Microsoft.Extensions.Options.DataAnnotations.csproj b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/Microsoft.Extensions.Options.DataAnnotations.csproj index c7a02b8336630..149921ab06d4b 100644 --- a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/Microsoft.Extensions.Options.DataAnnotations.csproj +++ b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/Microsoft.Extensions.Options.DataAnnotations.csproj @@ -9,6 +9,11 @@ + + + + + diff --git a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/OptionsBuilderDataAnnotationsExtensions.cs b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/OptionsBuilderDataAnnotationsExtensions.cs index 6e69725ad355c..55f8b33a70311 100644 --- a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/OptionsBuilderDataAnnotationsExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/OptionsBuilderDataAnnotationsExtensions.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.Options; namespace Microsoft.Extensions.DependencyInjection @@ -16,7 +17,9 @@ public static class OptionsBuilderDataAnnotationsExtensions /// The options type to be configured. /// The options builder to add the services to. /// The so that additional calls can be chained. - public static OptionsBuilder ValidateDataAnnotations(this OptionsBuilder optionsBuilder) where TOptions : class + [RequiresUnreferencedCode("Uses DataAnnotationValidateOptions which is unsafe given that the options type passed in when calling Validate cannot be statically analyzed so its" + + " members may be trimmed.")] + public static OptionsBuilder ValidateDataAnnotations<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] TOptions>(this OptionsBuilder optionsBuilder) where TOptions : class { optionsBuilder.Services.AddSingleton>(new DataAnnotationValidateOptions(optionsBuilder.Name)); return optionsBuilder;