-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Closed
Labels
area-System.Text.Jsonbugsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature
Milestone
Description
Description
As the title says, when there is a get-only property with an empty string as the default parameter in the constructor, the generator generates invalid C# code.
Reproduction Steps
Attempt to compile the following code:
using System.Text.Json.Serialization;
public class Test
{
public string Property { get; }
public Test(string property = "") { Property = property; }
}
[JsonSerializable(typeof(Test))]
public partial class Context : JsonSerializerContext
{
}Expected behavior
The code compiles.
Actual behavior
1>C:\temp\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\Context.Test.g.cs(92,31,92,31): error CS1525: Invalid expression term '}'
It appears the generator is generating the following invalid object initializer:
private static global::System.Text.Json.Serialization.Metadata.JsonParameterInfoValues[] TestCtorParamInit()
{
global::System.Text.Json.Serialization.Metadata.JsonParameterInfoValues[] parameters = new global::System.Text.Json.Serialization.Metadata.JsonParameterInfoValues[1];
global::System.Text.Json.Serialization.Metadata.JsonParameterInfoValues info;
info = new()
{
Name = "property",
ParameterType = typeof(global::System.String),
Position = 0,
HasDefaultValue = true,
DefaultValue =
};
parameters[0] = info;
return parameters;
}Regression?
No
Known Workarounds
Don't use empty strings as the default value.
Configuration
.NET 6.0.100.
Other information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-System.Text.Jsonbugsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature