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

YamlIgnoreAttribute can not apply to overrides #271

Closed
qaqz111 opened this issue Jul 28, 2017 · 3 comments
Closed

YamlIgnoreAttribute can not apply to overrides #271

qaqz111 opened this issue Jul 28, 2017 · 3 comments
Assignees
Labels

Comments

@qaqz111
Copy link

qaqz111 commented Jul 28, 2017

public abstract class CfgFileBase {
	[YamlIgnore]
	public virtual string Path {get;set;} = @"C:\settings.yaml";
	[YamlIgnore]
        public virtual xxx xxx1 {get;set;}

	//...

	[YamlIgnore]
        public virtual xxx xxx20 {get;set;}
}

public class CfgFileForClass1 : CfgFileBase {
	public override string Path {get;set;} = @"D:\c1.test\settings.yaml";
	//...
}

//...

public class CfgFileForClass100 : CfgFileBase {
	public override string Path {get;set;} = @"D:\c100.test\settings.yaml";
	//...
}

While serializing CfgFile to yaml, the overriden Path was write to the output which is not desired.

Yes I can give a [YamlIgnore] to overridden property, but what if I have 100 or more derived class and 10 or more properties per class have to attach a [YamlIgnore]?

It's better to append a property ApplyToOverrides to indicate this attribute should apply to overrides, like ScriptIgnore in System.Web.Script.Serialization:
ApplyToOverrides of ScriptIgnore in System.Web.Script.Serialization

@aaubry
Copy link
Owner

aaubry commented Sep 28, 2017

I agree that this needs fixing. Is is necessary to add a property to control this behavior? Is there any use case where one would not want the YamlIgnore to apply to an overriden property?

@aaubry aaubry self-assigned this Sep 28, 2017
@qaqz111
Copy link
Author

qaqz111 commented Sep 29, 2017

I think it is very rarely that one would not want the XXXIgnore to apply to an overriden property while using a serializer library, so as you can see the ApplyToOverrides of ScriptIgnore is default to true, but provided a way for user to choose not to apply it to an overriden.

This pattern can be used by YamlDotNet as well. So, my suggestion is to add a property to control this behavior and set it's default value to allow attribute to apply to overrides.

pensono added a commit to pensono/YamlDotNet that referenced this issue Apr 18, 2020
The source of this bug is the fact that IMemberInfo.GetCustomAttributes
actually ignores it's "inherit" parameter.

https://docs.microsoft.com/en-us/dotnet/api/system.reflection.memberinfo.getcustomattributes

This addresses aaubry#271
@aaubry aaubry added the bug label Apr 19, 2020
@EdwardCooke
Copy link
Collaborator

I just verified this issue is resolved with the following code and result:

using YamlDotNet.Serialization;

var serializer = new SerializerBuilder()
    .Build();

var test = new Y
{
    NotIgnored = "hi",
    Ignored = "bye"
};
Console.WriteLine(serializer.Serialize(test));


public class X
{
    public string NotIgnored { get; set; }

    [YamlIgnore]
    public virtual string Ignored { get; set; }
}

public class Y : X
{
    public override string Ignored { get => base.Ignored; set => base.Ignored = value; }
}

Result:

NotIgnored: hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants