Open adiessl opened 1 year ago
I't strange that warning is shown only for NSJ.
Newtonsoft.Json
is affected because the circumstances necessary apply to its JsonPropertyAttribute
. It can also affect other code, for example:
The attribute
[AttributeUsage(AttributeTargets.Property)]
public sealed class DemoAttribute : Attribute
{
public DemoAttribute(string propertyName)
{
PropertyName = propertyName;
}
public string PropertyName { get; }
}
applied to
[Demo("Property")]
public string? Property { get; init; }
leads to the same result:
Warning CA1507 : Use nameof in place of string literal 'Property'
finding another, fitting one might not be easy. System.Text.Json uses
name
[...]
name
is quite good, in my opinion. If that is not desired (but i could not imagine why), jsonPropertyName
should also do the trick...
We have a fairly large solution with multiple projects using
Newtonsoft.Json
and after upgrading Visual Studio to version 17.5 recently encountered a problem that I would like to bring to your attention and also document it here for others to find.How to reproduce
The easiest way to reproduce the problem is to have a project targeting .NET 7 that has all .NET Analyzers enabled:
If this project contains a class like the following,
the warning
CA1507
appears when building:If
TreatWarningsAsErrors
is enabled, the following error occurs:Excerpt from the Microsoft documentation explaining under which circumstances the rule CA1507 fires:
This is precisely the case for
JsonPropertyAttribute
used here:https://github.com/JamesNK/Newtonsoft.Json/blob/57025815e564d36821acf778e2c00d02225aab35/Src/Newtonsoft.Json/JsonPropertyAttribute.cs#L218
It seems that some change within the 17.5 release of MSBuild now makes this rule fire for attributes, which was not the case previously.
Related information:
Possible solutions
#pragma
: --> Works, but if there are lots of occurrences, a lot of#pragma
s have to be used, which makes the source code ugly..editorconfig
. --> Works as well, but silences this warning completely, so all valid use-cases (e.g.ArgumentNullException
) also disappear.Do not explicitly set
propertyName
if it matches the underlying property's name:--> The problem with this approach is that a future code refactoring might lead to bugs, e.g. if the property
Property
is renamed, but the name used in the JSON has to remain"Property"
for compatibility with an external API. The person doing the refactoring has to pay attention when doing so, which is prone to errors.JsonPropertyAttribute
's constructor parameterpropertyName
to something else, therefore making sureCA1507
does not fire anymore. --> The easiest solution for everybody usingNewtonsoft.Json
, butpropertyName
is the correct name for the parameter, finding another, fitting one might not be easy.System.Text.Json
usesname
in theirJsonPropertyNameAttribute
constructor: https://github.com/dotnet/runtime/blob/2b5f34218de7ff46dc9c0c37976c7a4abc64fcd2/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonPropertyNameAttribute.cs#L17