Skip to content

Commit

Permalink
Add diagnostics for the inaccessible attribute case
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh committed Jul 10, 2023
1 parent c59cb6a commit 3880f92
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 9 deletions.
10 changes: 5 additions & 5 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL
| __`SYSLIB1212`__ | Options validation generator: Member potentially missing transitive validation. |
| __`SYSLIB1213`__ | Options validation generator: Member potentially missing enumerable validation. |
| __`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.* |
| __`SYSLIB1218`__ | *_`SYSLIB1214`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1219`__ | *_`SYSLIB1214`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1215`__ | Options validation generator: Validation attribute on the member is inaccessible from the validator type. |
| __`SYSLIB1216`__ | *_`SYSLIB1216`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1217`__ | *_`SYSLIB1217`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1218`__ | *_`SYSLIB1218`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1219`__ | *_`SYSLIB1219`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1220`__ | JsonSourceGenerator encountered a [JsonConverterAttribute] with an invalid type argument. |
| __`SYSLIB1221`__ | JsonSourceGenerator does not support this C# language version. |
| __`SYSLIB1222`__ | Constructor annotated with JsonConstructorAttribute is inaccessible. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,12 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
messageFormat: SR.CantValidateStaticOrConstMemberMessage,
category: Category,
defaultSeverity: DiagnosticSeverity.Warning);

public static DiagnosticDescriptor InaccessibleValidationAttribute { get; } = Make(
id: "SYSLIB1215",
title: SR.InaccessibleValidationAttributeTitle,
messageFormat: SR.InaccessibleValidationAttribute,
category: Category,
defaultSeverity: DiagnosticSeverity.Info);
}
}
9 changes: 7 additions & 2 deletions src/libraries/Microsoft.Extensions.Options/gen/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,14 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
// pop the stack
_ = _visitedModelTypes.Remove(enumeratedType.WithNullableAnnotation(NullableAnnotation.None));
}
else if (ConvertTo(attributeType, _symbolHolder.ValidationAttributeSymbol) &&
_compilation.IsSymbolAccessibleWithin(attributeType, validatorType))
else if (ConvertTo(attributeType, _symbolHolder.ValidationAttributeSymbol))
{
if (!_compilation.IsSymbolAccessibleWithin(attributeType, validatorType))
{
Diag(DiagDescriptors.InaccessibleValidationAttribute, location, attributeType.Name, member.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat), validatorType.Name);
continue;
}

var validationAttr = new ValidationAttributeInfo(attributeType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
validationAttrs.Add(validationAttr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,10 @@
<data name="ValidatorsNeedSimpleConstructorTitle" xml:space="preserve">
<value>Validators used for transitive or enumerable validation must have a constructor with no parameters.</value>
</data>
<data name="InaccessibleValidationAttribute" xml:space="preserve">
<value>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</value>
</data>
<data name="InaccessibleValidationAttributeTitle" xml:space="preserve">
<value>Validation attribute on the member is inaccessible from the validator type..</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Typ anotovaný třídou OptionsValidatorAttribute neimplementuje nezbytné rozhraní.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Ověřovací atributy nelze použít u privátního pole nebo vlastnosti {0}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Ein mit "OptionsValidatorAttribute" versehener Typ implementiert nicht die erforderliche Schnittstelle.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Validierungsattribute können nicht auf private Felder oder Eigenschaften {0} angewendet werden.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Un tipo anotado con “OptionsValidatorAttribute” no implementa la interfaz necesaria.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">No se pueden aplicar atributos de validación a la propiedad o campo privado {0}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Un type annoté avec 'OptionsValidatorAttribute' n’implémente pas l’interface nécessaire.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Impossible d’appliquer les attributs de validation au champ privé ou à la propriété {0}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Un tipo annotato con 'OptionsValidatorAttribute' non implementa l'interfaccia necessaria.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Non è possibile applicare gli attributi di convalida al campo privato o alla proprietà {0}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">'OptionsValidatorAttribute' の注釈が付けられた型が、必要とされるインターフェイスを実装していません。</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">プライベート フィールドまたはプロパティ {0} には検証属性を適用できません。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">'OptionsValidatorAttribute'로 주석이 추가된 형식은 필요한 인터페이스를 구현하지 않습니다.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">프라이빗 필드 또는 속성 {0}에 유효성 검사 특성을 적용할 수 없습니다.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Typ z adnotacją „OptionsValidatorAttribute” nie implementuje wymaganego interfejsu.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Nie można zastosować atrybutów weryfikacji do pola prywatnego lub właściwości {0}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Um tipo anotado com "OptionsValidatorAttribute" não implementa a interface necessária.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Não é possível aplicar atributos de validação a um campo privado ou propriedade {0}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Тип с аннотацией OptionsValidatorAttribute не реализует необходимый интерфейс.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Не удается применить атрибуты проверки к закрытому полю или свойству {0}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">'OptionsValidatorAttribute' ile açıklama eklenmiş bir tür gerekli arabirimi uygulamıyor.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">Doğrulama öznitelikleri, {0} özel alanına veya özelliğine uygulanamıyor.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">用 "OptionsValidatorAttribute" 批注的类型未实现必要的接口。</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">无法将验证属性应用于专用字段或属性 {0}。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">以 'OptionsValidatorAttribute' 附註的類型未實作必要的介面。</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttribute">
<source>Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</source>
<target state="new">Validation attribute '{0}' on the member '{1}' is inaccessible from the validator type '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="InaccessibleValidationAttributeTitle">
<source>Validation attribute on the member is inaccessible from the validator type..</source>
<target state="new">Validation attribute on the member is inaccessible from the validator type..</target>
<note />
</trans-unit>
<trans-unit id="MemberIsInaccessibleMessage">
<source>Can't apply validation attributes to private field or property {0}.</source>
<target state="translated">無法將驗證屬性套用至私用欄位或屬性 {0}。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ public class MyOptions
}

[ConditionalFact(nameof(SupportRemoteExecutionAndNotInBrowser))]
public void IgnoreNotAccessibleValidationAttributesTest()
public void InaccessibleValidationAttributesTest()
{
string source = """
using System;
Expand Down Expand Up @@ -1357,7 +1357,8 @@ internal sealed partial class ExtOptionsValidator : IValidateOptions<ExtOptions>
.ConfigureAwait(false);
_ = Assert.Single(generatedSources);
Assert.Empty(diagnostics);
Assert.Single(diagnostics);
Assert.Equal(DiagDescriptors.InaccessibleValidationAttribute.Id, diagnostics[0].Id);
string generatedSource = generatedSources[0].SourceText.ToString();
Assert.Contains("global::System.ComponentModel.DataAnnotations.RangeAttribute", generatedSource);
Assert.Contains("global::System.ComponentModel.DataAnnotations.RequiredAttribute", generatedSource);
Expand Down
Loading

0 comments on commit 3880f92

Please sign in to comment.