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 source generator JsonIgnoreCondition missing namespace #54385

Closed
romfir opened this issue Jun 18, 2021 · 2 comments · Fixed by #54526
Closed

System.Text.Json source generator JsonIgnoreCondition missing namespace #54385

romfir opened this issue Jun 18, 2021 · 2 comments · Fixed by #54526
Assignees
Milestone

Comments

@romfir
Copy link

romfir commented Jun 18, 2021

Description

When using JsonIgnoreAttribute with JsonSerializerContext using Source Generators, generated code is not correct

example classes:

using System.Text.Json.Serialization;

namespace ConsoleApp3
{
    [JsonSerializable(typeof(Test))]
    internal partial class TestContext : JsonSerializerContext
    {
    }
}
using System.Text.Json.Serialization;

namespace ConsoleApp3
{
    public class Test
    {
        [JsonIgnore]
        public int X { get; set; }

        public int Y { get; set; }
    }
}

error:
error CS0103: The name 'JsonIgnoreCondition' does not exist in the current context

generated:

ignoreCondition: JsonIgnoreCondition.Always,

expected:

ignoreCondition: global::System.Text.Json.Serialization.JsonIgnoreCondition.Always,

Configuration

dotnet: 6.0.100-preview.5.21302.13
System.Text.Json: 6.0.0-preview.5.21301.5

Other information

generated code
TestContext.Test.g.cs

// <auto-generated/>

namespace ConsoleApp3
{
    internal partial class TestContext
    {
        private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::ConsoleApp3.Test> _Test;
        public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::ConsoleApp3.Test> Test
        {
            get
            {
                if (_Test == null)
                {
                    global::System.Text.Json.Serialization.JsonConverter customConverter;
                        if (Options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(typeof(global::ConsoleApp3.Test))) != null)
                        {
                            _Test = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::ConsoleApp3.Test>(Options, customConverter);
                        }
                        else
                        {
                            global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::ConsoleApp3.Test> objectInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo<global::ConsoleApp3.Test>(
                                Options,
                                createObjectFunc: static () => new global::ConsoleApp3.Test(),
                                propInitFunc: TestPropInit,
                                default,
                                serializeFunc: TestSerialize);
            
                                _Test = objectInfo;
                        }
                }
        
                return _Test;
            }
        }
        
        private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] TestPropInit(global::System.Text.Json.Serialization.JsonSerializerContext context)
        {
            global::ConsoleApp3.TestContext jsonContext = (global::ConsoleApp3.TestContext)context;
            global::System.Text.Json.JsonSerializerOptions options = context.Options;
        
            global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[2];
        
            properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.Int32>(
                options,
                isProperty: true,
                declaringType: typeof(global::ConsoleApp3.Test),
                propertyTypeInfo: jsonContext.Int32,
                converter: null,
                getter: static (obj) => { return ((global::ConsoleApp3.Test)obj).X; },
                setter: static (obj, value) => { ((global::ConsoleApp3.Test)obj).X = value; },
                ignoreCondition: JsonIgnoreCondition.Always,
                numberHandling: default,
                propertyName: "X",
                jsonPropertyName: null);
            
            properties[1] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.Int32>(
                options,
                isProperty: true,
                declaringType: typeof(global::ConsoleApp3.Test),
                propertyTypeInfo: jsonContext.Int32,
                converter: null,
                getter: static (obj) => { return ((global::ConsoleApp3.Test)obj).Y; },
                setter: static (obj, value) => { ((global::ConsoleApp3.Test)obj).Y = value; },
                ignoreCondition: default,
                numberHandling: default,
                propertyName: "Y",
                jsonPropertyName: null);
            
            return properties;
        }
        
        private static void TestSerialize(global::System.Text.Json.Utf8JsonWriter writer, global::ConsoleApp3.Test value)
        {
            if (value == null)
            {
                writer.WriteNullValue();
                return;
            }
        
            writer.WriteStartObject();
                writer.WriteNumber(XPropName, value.X);
                writer.WriteNumber(YPropName, value.Y);
        
                writer.WriteEndObject();
        }
    }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jun 18, 2021
@ghost
Copy link

ghost commented Jun 18, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

When using JsonIgnoreAttribute with JsonSerializerContext using Source Generators, generated code is not correct

example classes:

using System.Text.Json.Serialization;

namespace ConsoleApp3
{
    [JsonSerializable(typeof(Test))]
    internal partial class TestContext : JsonSerializerContext
    {
    }
}
using System.Text.Json.Serialization;

namespace ConsoleApp3
{
    public class Test
    {
        [JsonIgnore]
        public int X { get; set; }

        public int Y { get; set; }
    }
}

error:
error CS0103: The name 'JsonIgnoreCondition' does not exist in the current context

generated:

ignoreCondition: JsonIgnoreCondition.Always,

expected:

ignoreCondition: global::System.Text.Json.Serialization.JsonIgnoreCondition.Always,

Configuration

dotnet: 6.0.100-preview.5.21302.13
System.Text.Json: 6.0.0-preview.5.21301.5

Other information

generated code
TestContext.Test.g.cs

// <auto-generated/>

namespace ConsoleApp3
{
    internal partial class TestContext
    {
        private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::ConsoleApp3.Test> _Test;
        public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::ConsoleApp3.Test> Test
        {
            get
            {
                if (_Test == null)
                {
                    global::System.Text.Json.Serialization.JsonConverter customConverter;
                        if (Options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(typeof(global::ConsoleApp3.Test))) != null)
                        {
                            _Test = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::ConsoleApp3.Test>(Options, customConverter);
                        }
                        else
                        {
                            global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::ConsoleApp3.Test> objectInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo<global::ConsoleApp3.Test>(
                                Options,
                                createObjectFunc: static () => new global::ConsoleApp3.Test(),
                                propInitFunc: TestPropInit,
                                default,
                                serializeFunc: TestSerialize);
            
                                _Test = objectInfo;
                        }
                }
        
                return _Test;
            }
        }
        
        private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] TestPropInit(global::System.Text.Json.Serialization.JsonSerializerContext context)
        {
            global::ConsoleApp3.TestContext jsonContext = (global::ConsoleApp3.TestContext)context;
            global::System.Text.Json.JsonSerializerOptions options = context.Options;
        
            global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[2];
        
            properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.Int32>(
                options,
                isProperty: true,
                declaringType: typeof(global::ConsoleApp3.Test),
                propertyTypeInfo: jsonContext.Int32,
                converter: null,
                getter: static (obj) => { return ((global::ConsoleApp3.Test)obj).X; },
                setter: static (obj, value) => { ((global::ConsoleApp3.Test)obj).X = value; },
                ignoreCondition: JsonIgnoreCondition.Always,
                numberHandling: default,
                propertyName: "X",
                jsonPropertyName: null);
            
            properties[1] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.Int32>(
                options,
                isProperty: true,
                declaringType: typeof(global::ConsoleApp3.Test),
                propertyTypeInfo: jsonContext.Int32,
                converter: null,
                getter: static (obj) => { return ((global::ConsoleApp3.Test)obj).Y; },
                setter: static (obj, value) => { ((global::ConsoleApp3.Test)obj).Y = value; },
                ignoreCondition: default,
                numberHandling: default,
                propertyName: "Y",
                jsonPropertyName: null);
            
            return properties;
        }
        
        private static void TestSerialize(global::System.Text.Json.Utf8JsonWriter writer, global::ConsoleApp3.Test value)
        {
            if (value == null)
            {
                writer.WriteNullValue();
                return;
            }
        
            writer.WriteStartObject();
                writer.WriteNumber(XPropName, value.X);
                writer.WriteNumber(YPropName, value.Y);
        
                writer.WriteEndObject();
        }
    }
}
Author: romfir
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

cc @layomia

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jun 21, 2021
@layomia layomia self-assigned this Jun 21, 2021
@layomia layomia added this to the 6.0.0 milestone Jun 21, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 21, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 13, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Aug 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants