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

YamlIgnore attributes are inherited #481

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,25 @@ public String IgnoreMe
}
}

public class IgnoreExampleBase
{
[YamlIgnore]
public virtual String IgnoreMe
{
get { throw new InvalidOperationException("Accessing a [YamlIgnore] property"); }
set { throw new InvalidOperationException("Accessing a [YamlIgnore] property"); }
}
}

public class IgnoreExampleDerived : IgnoreExampleBase
{
public override String IgnoreMe
{
get { throw new InvalidOperationException("Accessing a [YamlIgnore] property"); }
set { throw new InvalidOperationException("Accessing a [YamlIgnore] property"); }
}
}

public class ScalarStyleExample
{
public ScalarStyleExample()
Expand Down
13 changes: 13 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,19 @@ public void SerializationRespectsYamlIgnoreAttribute()
serialized.Should().NotContain("IgnoreMe");
}

[Fact]
public void SerializationRespectsYamlIgnoreAttributeOfDerivedClasses()
{

var writer = new StringWriter();
var obj = new IgnoreExampleDerived();

Serializer.Serialize(writer, obj);
var serialized = writer.ToString();

serialized.Should().NotContain("IgnoreMe");
}

[Fact]
public void SerializationRespectsYamlIgnoreOverride()
{
Expand Down
25 changes: 25 additions & 0 deletions YamlDotNet/Helpers/Portability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ public static bool IsInstanceOf(this Type type, object o)
{
return o.GetType() == type || o.GetType().GetTypeInfo().IsSubclassOf(type);
}

public static Attribute[] GetAllCustomAttributes<TAttribute>(this PropertyInfo member)
{
// IMemberInfo.GetCustomAttributes ignores it's "inherit" parameter for properties,
// and the suggested replacement (Attribute.GetCustomAttributes) is not available
// on netstandard1.3
var result = new List<Attribute>();
var type = member.DeclaringType;

while (type != null)
{
type.GetPublicProperty(member.Name);
result.AddRange(member.GetCustomAttributes(typeof(TAttribute)));

type = type.BaseType();
}

return result.ToArray();
}
}

internal sealed class CultureInfoAdapter : CultureInfo
Expand Down Expand Up @@ -374,6 +393,12 @@ public static bool IsInstanceOf(this Type type, object o)
{
return type.IsInstanceOfType(o);
}

public static Attribute[] GetAllCustomAttributes<TAttribute>(this PropertyInfo property)
{
// Don't use IMemberInfo.GetCustomAttributes, it ignores the inherit parameter
return Attribute.GetCustomAttributes(property, typeof(TAttribute));
}
}

internal sealed class CultureInfoAdapter : CultureInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Write(object target, object? value)

public T GetCustomAttribute<T>() where T : Attribute
{
var attributes = propertyInfo.GetCustomAttributes(typeof(T), true);
var attributes = propertyInfo.GetAllCustomAttributes<T>();
return (T)attributes.FirstOrDefault();
}

Expand Down