Skip to content
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

Closed
Tracked by #77019
JakeYallop opened this issue May 11, 2022 · 4 comments · Fixed by #87796
Assignees
Labels
area-System.Text.Json bug help wanted [up-for-grabs] Good issue for external contributors Priority:3 Work that is nice to have source-generator Indicates an issue with a source generator feature
Milestone

Comments

@JakeYallop
Copy link
Contributor

Description

The TypeInfoPropertyName property on the JsonSerializableAttribute 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 an ArgumentException.

I discovered this issue as I wanted to use nameof and then add a suffix to resolve a naming conflict.

Reproduction Steps

public 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 behavior

Generator correctly parses the interpolated string, or fails gracefully with a diagnostic.

Actual behavior

warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'Context.$".g.cs' contains an invalid character '$' at position 8.

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 11, 2022
@ghost
Copy link

ghost commented May 11, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

The TypeInfoPropertyName property on the JsonSerializableAttribute 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 an ArgumentException.

I discovered this issue as I wanted to use nameof and then add a suffix to resolve a naming conflict.

Reproduction Steps

public 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 behavior

Generator correctly parses the interpolated string, or fails gracefully with a diagnostic.

Actual behavior

warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'Context.$".g.cs' contains an invalid character '$' at position 8.

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

Author: JakeYallop
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label May 11, 2022
@layomia layomia added this to the 7.0.0 milestone May 11, 2022
@layomia layomia added the bug label May 11, 2022
@layomia
Copy link
Contributor

layomia commented May 11, 2022

FYI @maryamariyan @joperezr for other source generators.

@joperezr
Copy link
Member

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.

@JakeYallop
Copy link
Contributor Author

JakeYallop commented May 12, 2022

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 const field, and then used within the attribute?

public class Test {}
public static class Constants
{
    public const string TypeInfoName = "MyCustomName";
};

[JsonSerializable(typeof(Test), TypeInfoPropertyName = $"{Constants.TypeInfoName}" )]
public partial class Context : JsonSerializerContext { }

@jeffhandley jeffhandley modified the milestones: 7.0.0, Future Jul 10, 2022
@eiriktsarpalis eiriktsarpalis added the Priority:3 Work that is nice to have label Aug 1, 2022
@eiriktsarpalis eiriktsarpalis added the source-generator Indicates an issue with a source generator feature label Sep 2, 2022
@eiriktsarpalis eiriktsarpalis added the help wanted [up-for-grabs] Good issue for external contributors label Jun 7, 2023
@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 8.0.0 Jun 20, 2023
@eiriktsarpalis eiriktsarpalis self-assigned this Jun 20, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 20, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 20, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json bug help wanted [up-for-grabs] Good issue for external contributors Priority:3 Work that is nice to have source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants