Skip to content

Commit

Permalink
Merge #481: YamlIgnore attributes are inherited
Browse files Browse the repository at this point in the history
+semver:patch
  • Loading branch information
aaubry authored and Antoine Aubry committed Apr 21, 2020
2 parents 37964b2 + 4ba7eb6 commit f7da29d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
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

0 comments on commit f7da29d

Please sign in to comment.