Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added requested tests
  • Loading branch information
YohDeadfall committed Apr 30, 2020
commit c3d7228d560f02c99e6da530373348ca692a9845
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,90 @@ public static void Ignore_public_property_on_conflict_with_private_due_to_policy
Assert.Equal("ConflictingValue", obj.myString);
}

[Fact]
public static void Throw_when_public_properties_conflict_due_to_attributes()
{
// Serialize
var obj = new ClassWithPropertyNamingConflictWhichThrows();
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Serialize(obj));

// Deserialize
string json = @"{""MyString"":""NewValue""}";
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Deserialize<ClassWithPropertyNamingConflictWhichThrows>(json));
}

[Fact]
public static void Throw_when_public_properties_conflict_due_to_attributes_and_inheritance()
{
// Serialize
var obj = new ClassInheritedWithPropertyNamingConflictWhichThrows();
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Serialize(obj));

// Deserialize
string json = @"{""MyString"":""NewValue""}";
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Deserialize<ClassInheritedWithPropertyNamingConflictWhichThrows>(json));
}

[Fact]
public static void Throw_when_public_properties_conflict_due_to_attributes_and_double_inheritance()
{
// Serialize
var obj = new ClassTwiceInheritedWithPropertyNamingConflictWhichThrows();
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Serialize(obj));

// Deserialize
string json = @"{""MyString"":""NewValue""}";
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Deserialize<ClassTwiceInheritedWithPropertyNamingConflictWhichThrows>(json));
}

[Fact]
public static void Throw_when_public_properties_conflict_due_to_policy()
{
// Serialize
var obj = new ClassWithPropertyPolicyConflictWhichThrows();
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Serialize(obj));

// Deserialize
string json = @"{""MyString"":""NewValue""}";
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Deserialize<ClassWithPropertyPolicyConflictWhichThrows>(json));
}

[Fact]
public static void Throw_when_public_properties_conflict_due_to_policy_and_inheritance()
{
// Serialize
var obj = new ClassInheritedWithPropertyPolicyConflictWhichThrows();
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Serialize(obj));

// Deserialize
string json = @"{""MyString"":""NewValue""}";
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Deserialize<ClassInheritedWithPropertyPolicyConflictWhichThrows>(json));
}

[Fact]
public static void Throw_when_public_properties_conflict_due_to_policy_and_double_inheritance()
{
// Serialize
var obj = new ClassTwiceInheritedWithPropertyPolicyConflictWhichThrows();
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Serialize(obj));

// Deserialize
string json = @"{""MyString"":""NewValue""}";
Assert.Throws<InvalidOperationException>(
() => JsonSerializer.Deserialize<ClassTwiceInheritedWithPropertyPolicyConflictWhichThrows>(json));
}

public class ClassWithInternalProperty
{
internal string MyString { get; set; } = "DefaultValue";
Expand Down Expand Up @@ -195,13 +279,59 @@ public class ClassWithPropertyNamingConflict
internal string ConflictingString { get; set; } = "ConflictingValue";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a class variant where this property is public; and a test variant where the conflict spans across two inheritance levels.

}

public class ClassWithPropertyNamingConflictWhichThrows
{
public string MyString { get; set; } = "DefaultValue";

[JsonPropertyName(nameof(MyString))]
public string ConflictingString { get; set; } = "ConflictingValue";
}

public class ClassInheritedWithPropertyNamingConflictWhichThrows : ClassWithPublicProperty
{
[JsonPropertyName(nameof(MyString))]
public string ConflictingString { get; set; } = "ConflictingValue";
}

public class ClassTwiceInheritedWithPropertyNamingConflictWhichThrowsDummy : ClassWithPublicProperty
{

}

public class ClassTwiceInheritedWithPropertyNamingConflictWhichThrows : ClassTwiceInheritedWithPropertyNamingConflictWhichThrowsDummy
{
[JsonPropertyName(nameof(MyString))]
public string ConflictingString { get; set; } = "ConflictingValue";
}

public class ClassWithPropertyPolicyConflict
{
public string MyString { get; set; } = "DefaultValue";

internal string myString { get; set; } = "ConflictingValue";
}

public class ClassWithPropertyPolicyConflictWhichThrows
{
public string MyString { get; set; } = "DefaultValue";

public string myString { get; set; } = "ConflictingValue";
}

public class ClassInheritedWithPropertyPolicyConflictWhichThrows : ClassWithPublicProperty
{
public string myString { get; set; } = "ConflictingValue";
}

public class ClassInheritedWithPropertyPolicyConflictWhichThrowsDummy : ClassWithPublicProperty
{
}

public class ClassTwiceInheritedWithPropertyPolicyConflictWhichThrows : ClassInheritedWithPropertyPolicyConflictWhichThrowsDummy
{
public string myString { get; set; } = "ConflictingValue";
}

public class ClassWithIgnoredNewSlotProperty : ClassWithInternalProperty
{
[JsonIgnore]
Expand Down