From 779d53611efd10526ca2bc3e94ce13ee18ad493a Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Sun, 2 Jul 2023 20:37:07 +0200 Subject: [PATCH] Fix OptionsValidator source-gen to skip static and const members (#88254) Co-authored-by: Nikita Balabaev --- docs/project/list-of-diagnostics.md | 2 +- .../Microsoft.Extensions.Options.sln | 28 +++++++ .../gen/DiagDescriptors.cs | 7 ++ .../gen/Parser.cs | 18 ++++- .../gen/Resources/Strings.resx | 6 ++ .../gen/Resources/xlf/Strings.cs.xlf | 10 +++ .../gen/Resources/xlf/Strings.de.xlf | 10 +++ .../gen/Resources/xlf/Strings.es.xlf | 10 +++ .../gen/Resources/xlf/Strings.fr.xlf | 10 +++ .../gen/Resources/xlf/Strings.it.xlf | 10 +++ .../gen/Resources/xlf/Strings.ja.xlf | 10 +++ .../gen/Resources/xlf/Strings.ko.xlf | 10 +++ .../gen/Resources/xlf/Strings.pl.xlf | 10 +++ .../gen/Resources/xlf/Strings.pt-BR.xlf | 10 +++ .../gen/Resources/xlf/Strings.ru.xlf | 12 ++- .../gen/Resources/xlf/Strings.tr.xlf | 10 +++ .../gen/Resources/xlf/Strings.zh-Hans.xlf | 10 +++ .../gen/Resources/xlf/Strings.zh-Hant.xlf | 10 +++ .../tests/SourceGeneration.Unit.Tests/Main.cs | 74 +++++++++++++++++++ .../Resources/Strings.resx | 6 ++ .../Resources/Strings.resx | 6 ++ 21 files changed, 275 insertions(+), 4 deletions(-) diff --git a/docs/project/list-of-diagnostics.md b/docs/project/list-of-diagnostics.md index d63a0b836519f..e5ea1f587da07 100644 --- a/docs/project/list-of-diagnostics.md +++ b/docs/project/list-of-diagnostics.md @@ -248,7 +248,7 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL | __`SYSLIB1211`__ | Options validation generator: Unsupported circular references in model types. | | __`SYSLIB1212`__ | Options validation generator: Member potentially missing transitive validation. | | __`SYSLIB1213`__ | Options validation generator: Member potentially missing enumerable validation. | -| __`SYSLIB1214`__ | *_`SYSLIB1214`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* | +| __`SYSLIB1214`__ | Options validation generator: Can't validate constants, static fields or properties. | | __`SYSLIB1215`__ | *_`SYSLIB1214`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* | | __`SYSLIB1216`__ | *_`SYSLIB1214`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* | | __`SYSLIB1217`__ | *_`SYSLIB1214`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* | diff --git a/src/libraries/Microsoft.Extensions.Options/Microsoft.Extensions.Options.sln b/src/libraries/Microsoft.Extensions.Options/Microsoft.Extensions.Options.sln index 87a7d0a3a5c5e..3935bd84d4004 100644 --- a/src/libraries/Microsoft.Extensions.Options/Microsoft.Extensions.Options.sln +++ b/src/libraries/Microsoft.Extensions.Options/Microsoft.Extensions.Options.sln @@ -113,8 +113,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Option EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Options", "src\Microsoft.Extensions.Options.csproj", "{9BA945E7-0970-4CA2-A54B-F8D9B3E69917}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Options.SourceGeneration", "gen\Microsoft.Extensions.Options.SourceGeneration.csproj", "{2B8ED012-22B5-47DD-A879-FD2AFD4C067D}" +EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Options.Tests", "tests\Microsoft.Extensions.Options.Tests\Microsoft.Extensions.Options.Tests.csproj", "{94CAA850-ABDB-4A1E-B18B-19DA0DE75CFD}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Options.SourceGeneration.Unit.Tests", "tests\SourceGeneration.Unit.Tests\Microsoft.Extensions.Options.SourceGeneration.Unit.Tests.csproj", "{D3B6805B-F10E-4A19-99FC-55506892BC18}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Options.SourceGeneration.Tests", "tests\SourceGenerationTests\Microsoft.Extensions.Options.SourceGeneration.Tests.csproj", "{D7C2C4C7-CF28-4E2B-8749-31D7E6072588}" +EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Primitives", "..\Microsoft.Extensions.Primitives\ref\Microsoft.Extensions.Primitives.csproj", "{36C471D8-1D7A-4C81-8B05-2EED0984FBB4}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Primitives", "..\Microsoft.Extensions.Primitives\src\Microsoft.Extensions.Primitives.csproj", "{AAB5D437-EFB2-4BAC-BA0F-7323BA691B89}" @@ -123,6 +129,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Collections.Concurre EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Collections", "..\System.Collections\ref\System.Collections.csproj", "{6AE427EF-C018-4075-A4C8-BF3831C5F88C}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.ComponentModel.Annotations", "..\System.ComponentModel.Annotations\src\System.ComponentModel.Annotations.csproj", "{90548F7B-C673-42C9-BD88-A6E6550ECE1C}" +EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Diagnostics.DiagnosticSource", "..\System.Diagnostics.DiagnosticSource\ref\System.Diagnostics.DiagnosticSource.csproj", "{6A1A3AFF-C018-498A-80A0-532396132AD5}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Diagnostics.DiagnosticSource", "..\System.Diagnostics.DiagnosticSource\src\System.Diagnostics.DiagnosticSource.csproj", "{429C9D71-4BBD-489D-9C86-EC240F652008}" @@ -487,6 +495,22 @@ Global {D7CEC738-5D2D-4FCB-9268-9650EB01BF31}.Debug|Any CPU.Build.0 = Debug|Any CPU {D7CEC738-5D2D-4FCB-9268-9650EB01BF31}.Release|Any CPU.ActiveCfg = Release|Any CPU {D7CEC738-5D2D-4FCB-9268-9650EB01BF31}.Release|Any CPU.Build.0 = Release|Any CPU + {2B8ED012-22B5-47DD-A879-FD2AFD4C067D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2B8ED012-22B5-47DD-A879-FD2AFD4C067D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2B8ED012-22B5-47DD-A879-FD2AFD4C067D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2B8ED012-22B5-47DD-A879-FD2AFD4C067D}.Release|Any CPU.Build.0 = Release|Any CPU + {D3B6805B-F10E-4A19-99FC-55506892BC18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D3B6805B-F10E-4A19-99FC-55506892BC18}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D3B6805B-F10E-4A19-99FC-55506892BC18}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D3B6805B-F10E-4A19-99FC-55506892BC18}.Release|Any CPU.Build.0 = Release|Any CPU + {D7C2C4C7-CF28-4E2B-8749-31D7E6072588}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7C2C4C7-CF28-4E2B-8749-31D7E6072588}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7C2C4C7-CF28-4E2B-8749-31D7E6072588}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7C2C4C7-CF28-4E2B-8749-31D7E6072588}.Release|Any CPU.Build.0 = Release|Any CPU + {90548F7B-C673-42C9-BD88-A6E6550ECE1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {90548F7B-C673-42C9-BD88-A6E6550ECE1C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {90548F7B-C673-42C9-BD88-A6E6550ECE1C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {90548F7B-C673-42C9-BD88-A6E6550ECE1C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -571,6 +595,10 @@ Global {B5F25A78-B7FB-460B-9B71-BE82D22923FD} = {C25891DB-FBAC-4B92-9BB9-A8181B5A0EF1} {E06CE29E-15EB-4C0E-97B7-4367FFEDD98D} = {C25891DB-FBAC-4B92-9BB9-A8181B5A0EF1} {9021FD06-E11E-42DE-87EF-A1040BA5DB56} = {C25891DB-FBAC-4B92-9BB9-A8181B5A0EF1} + {2B8ED012-22B5-47DD-A879-FD2AFD4C067D} = {C25891DB-FBAC-4B92-9BB9-A8181B5A0EF1} + {D3B6805B-F10E-4A19-99FC-55506892BC18} = {7C1317AA-5F4C-42A4-80F3-856BA5E204AF} + {D7C2C4C7-CF28-4E2B-8749-31D7E6072588} = {7C1317AA-5F4C-42A4-80F3-856BA5E204AF} + {90548F7B-C673-42C9-BD88-A6E6550ECE1C} = {7028EE0A-D314-4F48-91CA-51A1633BC3F4} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {FE89CDC6-6313-439C-85D7-A81D5DF593E9} diff --git a/src/libraries/Microsoft.Extensions.Options/gen/DiagDescriptors.cs b/src/libraries/Microsoft.Extensions.Options/gen/DiagDescriptors.cs index e76363fee0e94..10ab32c19c0bd 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/DiagDescriptors.cs +++ b/src/libraries/Microsoft.Extensions.Options/gen/DiagDescriptors.cs @@ -91,5 +91,12 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase messageFormat: SR.PotentiallyMissingEnumerableValidationMessage, category: Category, defaultSeverity: DiagnosticSeverity.Warning); + + public static DiagnosticDescriptor CantValidateStaticOrConstMember { get; } = Make( + id: "SYSLIB1214", + title: SR.CantValidateStaticOrConstMemberTitle, + messageFormat: SR.CantValidateStaticOrConstMemberMessage, + category: Category, + defaultSeverity: DiagnosticSeverity.Warning); } } diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Parser.cs b/src/libraries/Microsoft.Extensions.Options/gen/Parser.cs index 2a9215c676071..dad40ee3d7f43 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Parser.cs +++ b/src/libraries/Microsoft.Extensions.Options/gen/Parser.cs @@ -448,6 +448,20 @@ private List GetMembersToValidate(ITypeSymbol modelType, bool s } } + bool validationAttributeIsApplied = validationAttrs.Count > 0 || transValidatorTypeName is not null || enumerationValidatorTypeName is not null; + + if (member.IsStatic) + { + // generate a warning if the member is const/static and has a validation attribute applied + if (validationAttributeIsApplied) + { + Diag(DiagDescriptors.CantValidateStaticOrConstMember, member.GetLocation(), member.Name); + } + + // don't validate the member in any case + return null; + } + // generate a warning if the field/property seems like it should be transitively validated if (transValidatorTypeName == null && speculate && memberType.SpecialType == SpecialType.None) { @@ -462,7 +476,7 @@ private List GetMembersToValidate(ITypeSymbol modelType, bool s } // generate a warning if the field/property seems like it should be enumerated - if (enumerationValidatorTypeName == null && speculate) + if (enumerationValidatorTypeName == null && speculate && memberType.SpecialType != SpecialType.System_String) { var enumeratedType = GetEnumeratedType(memberType); if (enumeratedType is not null) @@ -478,7 +492,7 @@ private List GetMembersToValidate(ITypeSymbol modelType, bool s } } - if (validationAttrs.Count > 0 || transValidatorTypeName is not null || enumerationValidatorTypeName is not null) + if (validationAttributeIsApplied) { return new( member.Name, diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/Strings.resx b/src/libraries/Microsoft.Extensions.Options/gen/Resources/Strings.resx index 021f345f58cae..76c83d10f7ef4 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/Strings.resx +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/Strings.resx @@ -135,6 +135,12 @@ Can't use 'ValidateObjectMembersAttribute' or 'ValidateEnumeratedItemsAttribute' on fields or properties with open generic types. + + Can't apply validation attributes to constant or static member {0}. + + + Can't validate constants, static fields or properties. + There is a circular type reference involving type {0} preventing it from being used for static validation. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.cs.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.cs.xlf index e2488a7f17f6f..7d241c4e1efb0 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.cs.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.cs.xlf @@ -32,6 +32,16 @@ U polí nebo vlastností s otevřenými obecnými typy nelze použít ValidateObjectMembersAttribute nebo ValidateEnumeratedItemsAttribute. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Existuje cyklický odkaz obsahující typ {0}, který brání jeho použití pro statické ověření. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.de.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.de.xlf index df204efb8a5d6..926cc182d6353 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.de.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.de.xlf @@ -32,6 +32,16 @@ "ValidateObjectMembersAttribute" oder "ValidateEnumeratedItemsAttribute" kann nicht für Felder oder Eigenschaften mit offenen generischen Typen verwendet werden. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Es gibt einen Zirkelverweis, der den Typ "{0}" verhindert, dass er für die statische Validierung verwendet wird. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.es.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.es.xlf index 184bbb39d6dc4..26ceb1b83d61d 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.es.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.es.xlf @@ -32,6 +32,16 @@ No se puede usar “ValidateObjectMembersAttribute” o “ValidateEnumeratedItemsAttribute” en campos o propiedades con tipos genéricos abiertos. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Hay una referencia de tipo circular que implica al tipo {0} que impide que se use para la validación estática. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.fr.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.fr.xlf index cb0596b5def02..47973e86f2f56 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.fr.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.fr.xlf @@ -32,6 +32,16 @@ Impossible d’utiliser 'ValidateObjectMembersAttribute' ou 'ValidateEnumeratedItemsAttribute' sur des champs ou des propriétés avec des types génériques ouverts. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Une référence de type circulaire implique un type {0} l’empêche d’être utilisé pour la validation statique. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.it.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.it.xlf index 2bbb76dea399e..3ca838dbbb396 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.it.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.it.xlf @@ -32,6 +32,16 @@ Non è possibile usare 'ValidateObjectMembersAttribute' o 'ValidateEnumeratedItemsAttribute' nei campi o nelle proprietà con tipi generici aperti. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Esiste un riferimento di tipo circolare che interessa il tipo {0} e ne impedisce l'utilizzo per la convalida statica. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ja.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ja.xlf index 00a2e4f7f9c3f..64929dd3ae7ca 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ja.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ja.xlf @@ -32,6 +32,16 @@ 'ValidateObjectMembersAttribute' または 'ValidateEnumeratedItemsAttribute' は、オープンジェネリック型を持つフィールドまたはプロパティでは使用できません。 + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. 型{0}を含む循環型参照があるため、静的検証に使用できません。 diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ko.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ko.xlf index 14137b6f600c6..98d9d90356ad7 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ko.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ko.xlf @@ -32,6 +32,16 @@ 열린 제네릭 형식의 필드 또는 속성에는 'ValidateObjectMembersAttribute' 또는 'ValidateEnumeratedItemsAttribute'를 사용할 수 없습니다. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. 정적 유효성 검사에 사용할 수 없으므로 형식 {0} 관련된 순환 형식 참조가 있습니다. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pl.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pl.xlf index 5766d2aa6bfda..ce7e2e2be8173 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pl.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pl.xlf @@ -32,6 +32,16 @@ Nie można użyć atrybutu „ValidateObjectMembersAttribute” lub „ValidateEnumeratedItemsAttribute” w polach lub właściwościach z otwartymi typami ogólnymi. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Istnieje cykliczne odwołanie do typu dotyczące typu {0} uniemożliwiające użycie go do weryfikacji statycznej. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pt-BR.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pt-BR.xlf index b226f9c074d43..522506b4147da 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pt-BR.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.pt-BR.xlf @@ -32,6 +32,16 @@ Não é possível usar "ValidateObjectMembersAttribute" ou "ValidateEnumeratedItemsAttribute" em campos ou propriedades com tipos genéricos abertos. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Há uma referência de tipo circular que envolve o tipo {0} que a impede de ser usada para validação estática. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ru.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ru.xlf index 3bb2c0e505c51..61ac1d7432964 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ru.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.ru.xlf @@ -32,6 +32,16 @@ Нельзя использовать "ValidateObjectMembersAttribute" или "ValidateEnumeratedItemsAttribute" для полей или свойств с открытыми универсальными типами. + + Can't apply validation attributes to constant or static member {0}. + Не удается применить атрибуты проверки к константе, статическому полю или свойству {0}. + + + + Can't validate constants, static fields or properties. + Не удается проверить статические поля или свойства, в т.ч. константы. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Существует циклическая ссылка на тип, включающая тип {0}, предотвращающий его использование для статической проверки. @@ -59,7 +69,7 @@ Can't validate private fields or properties. - Не удается проверить частные поля или свойства. + Не удается проверить приватные поля или свойства. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.tr.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.tr.xlf index 6d56473c74d03..a0d0ab6255b00 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.tr.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.tr.xlf @@ -32,6 +32,16 @@ Açık genel türleri olan alanlarda veya özelliklerde 'ValidateObjectMembersAttribute' veya 'ValidateEnumeratedItemsAttribute' kullanılamaz. + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. Statik doğrulama için kullanılmasını engelleyen {0} türü içeren döngüsel bir tür başvurusu var. diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hans.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hans.xlf index edd5917cae6b1..f21739b0f10f8 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hans.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hans.xlf @@ -32,6 +32,16 @@ 不能对具有开放泛型类型的字段或属性使用 "ValidateObjectMembersAttribute" 或 "ValidateEnumeratedItemsAttribute"。 + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. 存在一个循环类型引用,其中涉及的类型 {0} 阻止将其用于静态验证。 diff --git a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hant.xlf b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hant.xlf index ee2d9774e8fef..9d05a46f55568 100644 --- a/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hant.xlf +++ b/src/libraries/Microsoft.Extensions.Options/gen/Resources/xlf/Strings.zh-Hant.xlf @@ -32,6 +32,16 @@ 無法在有開放式泛型型別的欄位或屬性上使用 'ValidateObjectMembersAttribute' 或 'ValidateEnumeratedItemsAttribute'。 + + Can't apply validation attributes to constant or static member {0}. + Can't apply validation attributes to constant or static member {0}. + + + + Can't validate constants, static fields or properties. + Can't validate constants, static fields or properties. + + There is a circular type reference involving type {0} preventing it from being used for static validation. 有涉及類型 {0} 的循環類型參考讓它無法用於靜態驗證。 diff --git a/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Main.cs b/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Main.cs index ad094f991803f..08805c76b308d 100644 --- a/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Main.cs +++ b/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Main.cs @@ -163,6 +163,80 @@ public partial class FirstValidator : IValidateOptions Assert.Equal(DiagDescriptors.PotentiallyMissingEnumerableValidation.Id, diagnostics[1].Id); } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] + public async Task IgnoredStaticMembers() + { + var (d, _) = await RunGenerator(@" + public class FirstModel + { + // Since we ignore static members, we shouldn't check SecondModel, + // and shouldn't emit the 'SYSLIB1212' warning about potentially missing transitive validation + public static SecondModel? P1 { get; set; } + + public static SecondModel P2 = new(); + + public static System.Collections.Generic.IList? P3 { get; set; } + + public const SecondModel P4 = null; + + [Required] + public string Name { get; set; } = nameof(FirstModel); + } + + public class SecondModel + { + [Required] + public string? P3; + } + + [OptionsValidator] + public partial class FirstModelValidator : IValidateOptions + { + } + "); + + Assert.Empty(d); + } + + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] + public async Task ValidationAttributeOnStaticMember() + { + var (d, _) = await RunGenerator(@" + public class FirstModel + { + [Required] + public static string? P1 { get; set; } + + [Required] + public const string? P1; + + [ValidateObjectMembers] + public static SecondModel P2 = new(); + + [ValidateEnumeratedItems] + public static System.Collections.Generic.IList? P3 { get; set; } + + [Required] + public string Name { get; set; } = nameof(FirstModel); + } + + public class SecondModel + { + [Required] + public string? P3; + } + + [OptionsValidator] + public partial class FirstModelValidator : IValidateOptions + { + } + "); + + Assert.Equal(4, d.Count); + Assert.All(d, x => Assert.Equal(DiagDescriptors.CantValidateStaticOrConstMember.Id, x.Id)); + Assert.All(d, x => Assert.Equal(DiagnosticSeverity.Warning, x.DefaultSeverity)); + } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] public async Task CircularTypeReferences() { diff --git a/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Resources/Strings.resx b/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Resources/Strings.resx index 1673d4621545b..b3a449b3c9812 100644 --- a/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Resources/Strings.resx +++ b/src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Resources/Strings.resx @@ -198,4 +198,10 @@ Member potentially missing transitive validation. + + Can't apply validation attributes to constant or static member {0}. + + + Can't validate constants, static fields or properties. + \ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.Options/tests/SourceGenerationTests/Resources/Strings.resx b/src/libraries/Microsoft.Extensions.Options/tests/SourceGenerationTests/Resources/Strings.resx index 1673d4621545b..b3a449b3c9812 100644 --- a/src/libraries/Microsoft.Extensions.Options/tests/SourceGenerationTests/Resources/Strings.resx +++ b/src/libraries/Microsoft.Extensions.Options/tests/SourceGenerationTests/Resources/Strings.resx @@ -198,4 +198,10 @@ Member potentially missing transitive validation. + + Can't apply validation attributes to constant or static member {0}. + + + Can't validate constants, static fields or properties. + \ No newline at end of file