Skip to content

Commit

Permalink
Fix options source gen with non-accessible validation attributes (#88613
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tarekgh committed Jul 11, 2023
1 parent 57f691a commit 24150b3
Show file tree
Hide file tree
Showing 20 changed files with 349 additions and 16 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`__ | *_`SYSLIB1201`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1217`__ | *_`SYSLIB1201`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1218`__ | *_`SYSLIB1201`-`SYSLIB1219` reserved for Microsoft.Extensions.Options.SourceGeneration.* |
| __`SYSLIB1219`__ | *_`SYSLIB1201`-`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.InaccessibleValidationAttributeMessage,
category: Category,
defaultSeverity: DiagnosticSeverity.Info);
}
}
26 changes: 16 additions & 10 deletions src/libraries/Microsoft.Extensions.Options/gen/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public IReadOnlyList<ValidatorType> GetValidatorTypes(IEnumerable<(TypeDeclarati
? modelType.GetLocation()
: syntax.GetLocation();

var membersToValidate = GetMembersToValidate(modelType, true, lowerLocationInCompilation);
var membersToValidate = GetMembersToValidate(modelType, true, lowerLocationInCompilation, validatorType);
if (membersToValidate.Count == 0)
{
// this type lacks any eligible members
Expand Down Expand Up @@ -243,7 +243,7 @@ private static bool HasOpenGenerics(ITypeSymbol type, out string genericType)
return null;
}

private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool speculate, Location lowerLocationInCompilation)
private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool speculate, Location lowerLocationInCompilation, ITypeSymbol validatorType)
{
// make a list of the most derived members in the model type

Expand Down Expand Up @@ -271,7 +271,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
? member.GetLocation()
: lowerLocationInCompilation;

var memberInfo = GetMemberInfo(member, speculate, location);
var memberInfo = GetMemberInfo(member, speculate, location, validatorType);
if (memberInfo is not null)
{
if (member.DeclaredAccessibility != Accessibility.Public && member.DeclaredAccessibility != Accessibility.Internal)
Expand All @@ -287,7 +287,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
return membersToValidate;
}

private ValidatedMember? GetMemberInfo(ISymbol member, bool speculate, Location location)
private ValidatedMember? GetMemberInfo(ISymbol member, bool speculate, Location location, ITypeSymbol validatorType)
{
ITypeSymbol memberType;
switch (member)
Expand Down Expand Up @@ -370,7 +370,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
if (transValidatorTypeName == null)
{
transValidatorIsSynthetic = true;
transValidatorTypeName = AddSynthesizedValidator(memberType, member, location);
transValidatorTypeName = AddSynthesizedValidator(memberType, member, location, validatorType);
}

// pop the stack
Expand Down Expand Up @@ -433,14 +433,20 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
if (enumerationValidatorTypeName == null)
{
enumerationValidatorIsSynthetic = true;
enumerationValidatorTypeName = AddSynthesizedValidator(enumeratedType, member, location);
enumerationValidatorTypeName = AddSynthesizedValidator(enumeratedType, member, location, validatorType);
}

// pop the stack
_ = _visitedModelTypes.Remove(enumeratedType.WithNullableAnnotation(NullableAnnotation.None));
}
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 Expand Up @@ -475,7 +481,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
{
if (!HasOpenGenerics(memberType, out var genericType))
{
var membersToValidate = GetMembersToValidate(memberType, false, location);
var membersToValidate = GetMembersToValidate(memberType, false, location, validatorType);
if (membersToValidate.Count > 0)
{
Diag(DiagDescriptors.PotentiallyMissingTransitiveValidation, location, memberType.Name, member.Name);
Expand All @@ -491,7 +497,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
{
if (!HasOpenGenerics(enumeratedType, out var genericType))
{
var membersToValidate = GetMembersToValidate(enumeratedType, false, location);
var membersToValidate = GetMembersToValidate(enumeratedType, false, location, validatorType);
if (membersToValidate.Count > 0)
{
Diag(DiagDescriptors.PotentiallyMissingEnumerableValidation, location, enumeratedType.Name, member.Name);
Expand Down Expand Up @@ -519,7 +525,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
return null;
}

private string? AddSynthesizedValidator(ITypeSymbol modelType, ISymbol member, Location location)
private string? AddSynthesizedValidator(ITypeSymbol modelType, ISymbol member, Location location, ITypeSymbol validatorType)
{
var mt = modelType.WithNullableAnnotation(NullableAnnotation.None);
if (mt.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
Expand All @@ -533,7 +539,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
return "global::" + validator.Namespace + "." + validator.Name;
}

var membersToValidate = GetMembersToValidate(mt, true, location);
var membersToValidate = GetMembersToValidate(mt, true, location, validatorType);
if (membersToValidate.Count == 0)
{
// this type lacks any eligible members
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="InaccessibleValidationAttributeMessage" 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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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="InaccessibleValidationAttributeMessage">
<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
Loading

0 comments on commit 24150b3

Please sign in to comment.