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 generator does not generate valid code for empty string default parameter constructors #62766

Closed
333fred opened this issue Dec 14, 2021 · 3 comments · Fixed by #62798
Assignees
Labels
area-System.Text.Json bug source-generator Indicates an issue with a source generator feature
Milestone

Comments

@333fred
Copy link
Member

333fred commented Dec 14, 2021

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

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Dec 14, 2021
@ghost
Copy link

ghost commented Dec 14, 2021

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

Issue Details

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
{
    public static string Implicit => "World";
}

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

Author: 333fred
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@layomia layomia added bug source-generator Indicates an issue with a source generator feature and removed untriaged New issue has not been triaged by the area owner labels Dec 14, 2021
@layomia layomia self-assigned this Dec 14, 2021
@layomia layomia added this to the 6.0.x milestone Dec 14, 2021
@layomia
Copy link
Contributor

layomia commented Dec 14, 2021

I was able to repro.
6.0.x candidate as we are preventing successful compilation for a trivial/common scenario.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Dec 14, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 6, 2022
@layomia
Copy link
Contributor

layomia commented Jan 7, 2022

Re-opening for 6.0 consideration.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json bug source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants