-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Text.Json using interpolated string with JsonSerializable.TypeInfoPropertyName fails with ArgumentException #69207
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionThe Using an interpolated string (prefixed with I discovered this issue as I wanted to use Reproduction Stepspublic class MyClass {}
[JsonSerializable(typeof(MyClass), TypeInfoPropertyName = $"{nameof(MyClass)}SomeUniqueSuffix")]
public partial class Context : JsonSerializerContext { } The following variants also do not function as expected: public class MyClass {}
//generates a type info property called "nameof"
[JsonSerializable(typeof(MyClass), TypeInfoPropertyName = nameof(MyClass) + "SomeUniqueSuffix")]
//generates a type info property called "A"
[JsonSerializable(typeof(MyClass), TypeInfoPropertyName = "A" + "Suffix")]
public partial class Context : JsonSerializerContext { } Expected behaviorGenerator correctly parses the interpolated string, or fails gracefully with a diagnostic. Actual behavior
Regression?No Known WorkaroundsEnsure a single string is used as the property value. Configuration.NET Version: 6.0 Other informationNo response
|
FYI @maryamariyan @joperezr for other source generators. |
cc: @stephentoub as well. I don't think this particular problem affects the Regex source generator since we have a static file name for the generated source produced by the generator, but it would be good to add tests for interpolation and concatenation on patterns in the attribute to make sure we handle that correctly. Same goes for the analyzer that I'm working for, making sure we handle concatenation correctly. |
A slight variation on the issue above - what would we expect the following to generate (this would probably be similar for the Regex generator, if we had the expression stored in a public class Test {}
public static class Constants
{
public const string TypeInfoName = "MyCustomName";
};
[JsonSerializable(typeof(Test), TypeInfoPropertyName = $"{Constants.TypeInfoName}" )]
public partial class Context : JsonSerializerContext { } |
Description
The
TypeInfoPropertyName
property on theJsonSerializableAttribute
is used to provide a unique name for a serializable class in case of naming conflicts.Using an interpolated string (prefixed with
$
, now possible due to constant interpolated strings) for this property causes the generator to fail ungracefully with anArgumentException
.I discovered this issue as I wanted to use
nameof
and then add a suffix to resolve a naming conflict.Reproduction Steps
The following variants also do not function as expected:
Expected behavior
Generator correctly parses the interpolated string, or fails gracefully with a diagnostic.
Actual behavior
Regression?
No
Known Workarounds
Ensure a single string is used as the property value.
Configuration
.NET Version: 6.0
System.Text.Json version: 7.0.0-preview.4.22229.4
Other information
No response
The text was updated successfully, but these errors were encountered: