Skip to content

Commit

Permalink
Emit a diagnostic when attributes deriving from JsonConverterAttribut…
Browse files Browse the repository at this point in the history
…e are being used. (#89199)

* Emit a diagnostic when attributes deriving from JsonConverterAttribute are being used.

* Address feedback.
  • Loading branch information
eiriktsarpalis authored Jul 21, 2023
1 parent 6db5113 commit 5bc100e
Show file tree
Hide file tree
Showing 20 changed files with 235 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL
| __`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. |
| __`SYSLIB1223`__ | *`SYSLIB1220`-`SYSLIB229` reserved for System.Text.Json.SourceGeneration.* |
| __`SYSLIB1223`__ | Attributes deriving from JsonConverterAttribute are not supported by the source generator. |
| __`SYSLIB1224`__ | *`SYSLIB1220`-`SYSLIB229` reserved for System.Text.Json.SourceGeneration.* |
| __`SYSLIB1225`__ | *`SYSLIB1220`-`SYSLIB229` reserved for System.Text.Json.SourceGeneration.* |
| __`SYSLIB1226`__ | *`SYSLIB1220`-`SYSLIB229` reserved for System.Text.Json.SourceGeneration.* |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ internal static class DiagnosticDescriptors
category: JsonConstants.SystemTextJsonSourceGenerationName,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static DiagnosticDescriptor DerivedJsonConverterAttributesNotSupported { get; } = new DiagnosticDescriptor(
id: "SYSLIB1223",
title: new LocalizableResourceString(nameof(SR.DerivedJsonConverterAttributesNotSupportedTitle), SR.ResourceManager, typeof(FxResources.System.Text.Json.SourceGeneration.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.DerivedJsonConverterAttributesNotSupportedMessageFormat), SR.ResourceManager, typeof(FxResources.System.Text.Json.SourceGeneration.SR)),
category: JsonConstants.SystemTextJsonSourceGenerationName,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,14 @@ bool MatchesConstructorParameter(ParameterGenerationSpec paramSpec)
private TypeRef? GetConverterTypeFromJsonConverterAttribute(INamedTypeSymbol contextType, ISymbol declaringSymbol, AttributeData attributeData)
{
Debug.Assert(_knownSymbols.JsonConverterAttributeType.IsAssignableFrom(attributeData.AttributeClass));

if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, _knownSymbols.JsonConverterAttributeType))
{
ReportDiagnostic(DiagnosticDescriptors.DerivedJsonConverterAttributesNotSupported, attributeData.GetDiagnosticLocation(), attributeData.AttributeClass!.ToDisplayString());
return null;
}

Debug.Assert(attributeData.ConstructorArguments.Length == 1 && attributeData.ConstructorArguments[0].Value is null or ITypeSymbol);
var converterType = (ITypeSymbol?)attributeData.ConstructorArguments[0].Value;
return GetConverterTypeFromAttribute(contextType, converterType, declaringSymbol, attributeData);
}
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@
<data name="JsonConverterAttributeInvalidTypeMessageFormat" xml:space="preserve">
<value>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</value>
</data>
<data name="DerivedJsonConverterAttributesNotSupportedTitle" xml:space="preserve">
<value>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</value>
</data>
<data name="DerivedJsonConverterAttributesNotSupportedMessageFormat" xml:space="preserve">
<value>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</value>
</data>
<data name="JsonUnsupportedLanguageVersionTitle" xml:space="preserve">
<value>C# language version not supported by the source generator.</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">Typ vlastnosti rozšíření dat není platný</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">Existuje několik typů s názvem {0}. Zdroj se vygeneroval pro první zjištěný typ. Tuto kolizi vyřešíte pomocí JsonSerializableAttribute.TypeInfoPropertyName.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">Der Typ der Datenerweiterungseigenschaft ist ungültig.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">Es sind mehrere Typen namens "{0}" vorhanden. Die Quelle wurde für den ersten festgestellten Typ generiert. Verwenden Sie "JsonSerializableAttribute.TypeInfoPropertyName", um diesen Konflikt zu beheben.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">Tipo de propiedad de extensión de datos no válido.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">Hay varios tipos denominados {0}. El origen se generó para el primero detectado. Use "JsonSerializableAttribute.TypeInfoPropertyName" para resolver esta colisión.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">Type de propriété d’extension de données non valide</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">Plusieurs types nommés {0}. La source a été générée pour la première détection détectée. Utilisez « JsonSerializableAttribute.TypeInfoPropertyName » pour résoudre cette collision.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">Il tipo di proprietà dell'estensione dati non è valido.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">Sono presenti più tipi denominati {0}. L'origine è stata generata per il primo tipo rilevato. Per risolvere questa collisione, usare 'JsonSerializableAttribute.TypeInfoPropertyName'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">データ拡張プロパティの型が無効です。</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">{0} と名前が付けられた種類が複数あります。最初に検出されたものに対してソースが生成されました。この問題を解決するには、'JsonSerializableAttribute.TypeInfoPropertyName' を使用します。</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">데이터 확장 속성 형식이 잘못되었습니다.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">이름이 {0}인 형식이 여러 개 있습니다. 처음 검색한 원본에 대해 원본이 생성되었습니다. 이 충돌을 해결하려면 'JsonSerializableAttribute.TypeInfoPropertyName'을 사용하세요.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<target state="translated">Nieprawidłowy typ właściwości rozszerzenia danych.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedMessageFormat">
<source>The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</source>
<target state="new">The custom attribute '{0}' deriving from JsonConverterAttribute is not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DerivedJsonConverterAttributesNotSupportedTitle">
<source>Attributes deriving from JsonConverterAttribute are not supported by the source generator.</source>
<target state="new">Attributes deriving from JsonConverterAttribute are not supported by the source generator.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">Istnieje wiele typów o nazwie {0}. Wygenerowano źródło dla pierwszego wykrytego elementu. Aby rozwiązać tę kolizję, użyj „JsonSerializableAttribute. TypeInfoPropertyName”.</target>
Expand Down
Loading

0 comments on commit 5bc100e

Please sign in to comment.