Skip to content

Commit 2a8c633

Browse files
authored
Merge pull request #73 from jfhs/fix-multirelationship-save
fix(RelationshipAttribute): correct Equals and ToString
2 parents 2c7d07f + 4cc1000 commit 2a8c633

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ Install-Package JsonApiDotnetCore
4646

4747
- project.json
4848
```json
49-
"JsonApiDotNetCore": "1.2.0"
49+
"JsonApiDotNetCore": "1.2.1"
5050
```
5151

5252
- *.csproj
5353
```xml
5454
<ItemGroup>
5555
<!-- ... -->
56-
<PackageReference Include="JsonApiDotNetCore" Version="1.2.0" />
56+
<PackageReference Include="JsonApiDotNetCore" Version="1.2.1" />
5757
</ItemGroup>
5858
```
5959

src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<VersionPrefix>1.2.0</VersionPrefix>
4+
<VersionPrefix>1.2.1</VersionPrefix>
55
<TargetFramework>netcoreapp1.0</TargetFramework>
66
<AssemblyName>JsonApiDotNetCore</AssemblyName>
77
<PackageId>JsonApiDotNetCore</PackageId>

src/JsonApiDotNetCore/Models/RelationshipAttribute.cs

+15
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,20 @@ protected RelationshipAttribute(string publicName)
1616
public bool IsHasOne { get { return this.GetType() == typeof(HasOneAttribute); } }
1717

1818
public abstract void SetValue(object entity, object newValue);
19+
20+
public override string ToString()
21+
{
22+
return base.ToString() + ":" + PublicRelationshipName;
23+
}
24+
25+
public override bool Equals(object obj)
26+
{
27+
RelationshipAttribute attr = obj as RelationshipAttribute;
28+
if (attr == null)
29+
{
30+
return false;
31+
}
32+
return IsHasMany == attr.IsHasMany && PublicRelationshipName.Equals(attr.PublicRelationshipName);
33+
}
1934
}
2035
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using JsonApiDotNetCore.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace JsonApiDotNetCoreExampleTests.Unit.Models
8+
{
9+
public class AttributesEqualsTests
10+
{
11+
[Fact]
12+
public void HasManyAttribute_Equals_Returns_True_When_Same_Name()
13+
{
14+
var a = new HasManyAttribute("test");
15+
var b = new HasManyAttribute("test");
16+
17+
Assert.Equal(a, b);
18+
}
19+
20+
[Fact]
21+
public void HasManyAttribute_Equals_Returns_False_When_Different_Name()
22+
{
23+
var a = new HasManyAttribute("test");
24+
var b = new HasManyAttribute("test2");
25+
26+
Assert.NotEqual(a, b);
27+
}
28+
29+
[Fact]
30+
public void HasOneAttribute_Equals_Returns_True_When_Same_Name()
31+
{
32+
var a = new HasOneAttribute("test");
33+
var b = new HasOneAttribute("test");
34+
35+
Assert.Equal(a, b);
36+
}
37+
38+
[Fact]
39+
public void HasOneAttribute_Equals_Returns_False_When_Different_Name()
40+
{
41+
var a = new HasOneAttribute("test");
42+
var b = new HasOneAttribute("test2");
43+
44+
Assert.NotEqual(a, b);
45+
}
46+
47+
[Fact]
48+
public void AttrAttribute_Equals_Returns_True_When_Same_Name()
49+
{
50+
var a = new AttrAttribute("test");
51+
var b = new AttrAttribute("test");
52+
53+
Assert.Equal(a, b);
54+
}
55+
56+
[Fact]
57+
public void AttrAttribute_Equals_Returns_False_When_Different_Name()
58+
{
59+
var a = new AttrAttribute("test");
60+
var b = new AttrAttribute("test2");
61+
62+
Assert.NotEqual(a, b);
63+
}
64+
65+
[Fact]
66+
public void HasManyAttribute_Doesnt_Equal_HasOneAttribute_With_Same_Name()
67+
{
68+
RelationshipAttribute a = new HasManyAttribute("test");
69+
RelationshipAttribute b = new HasOneAttribute("test");
70+
71+
Assert.NotEqual(a, b);
72+
Assert.NotEqual(b, a);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)