Skip to content

Commit db80ac6

Browse files
authored
fix: For issue #431 - decorated properties of PathsResultBoltRelationship and PathsResultBoltNode with JsonProperty to ensure correct serialization/deserialization when using CamelCasePropertyNamesContractResolver (#433)
1 parent edd16ff commit db80ac6

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

Neo4jClient.Tests/BoltGraphClientTests/Cypher/ExecuteGetCypherResultsTests.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Neo4j.Driver;
99
using Neo4jClient.ApiModels.Cypher;
1010
using Neo4jClient.Cypher;
11+
using Newtonsoft.Json.Serialization;
1112
using Xunit;
1213

1314
namespace Neo4jClient.Tests.BoltGraphClientTests.Cypher
@@ -89,7 +90,7 @@ public bool Equals(INode other)
8990

9091
#region Implementation of INode
9192

92-
public IReadOnlyList<string> Labels { get; }
93+
public IReadOnlyList<string> Labels { get; set; }
9394

9495
#endregion
9596
}
@@ -718,5 +719,59 @@ public async Task ShouldDeserializePathsResultAsSetBased()
718719
results.First().Start.Id.Should().Be(2);
719720
}
720721
}
722+
723+
[Fact]
724+
public async Task ShouldDeserializePathsResultWhenUsingCamelCaseResolver()
725+
{
726+
// Arrange
727+
const string queryText = @"MATCH (start:Node {Id:$p0}),(end:Node {Id: $p1}), p = shortestPath((start)-[*..5]->(end)) RETURN p";
728+
729+
var parameters = new Dictionary<string, object>
730+
{
731+
{"p0", 215},
732+
{"p1", 219}
733+
};
734+
735+
var cypherQuery = new CypherQuery(queryText, parameters, CypherResultMode.Set, CypherResultFormat.Rest, "neo4j");
736+
737+
using (var testHarness = new BoltTestHarness())
738+
{
739+
var recordMock = new Mock<IRecord>();
740+
recordMock
741+
.Setup(r => r["p"])
742+
.Returns(new TestPath
743+
{
744+
End = new TestNode{Id = 1, Labels = new []{"Node"}},
745+
Start = new TestNode{Id=2, Labels = new []{"Node"}},
746+
Relationships = new List<IRelationship> {new TestRelationship{Id=3, StartNodeId = 2, EndNodeId = 1, Type = "Foo"}},
747+
Nodes = new List<INode> {new TestNode(), new TestNode() }
748+
});
749+
recordMock
750+
.Setup(r => r.Keys)
751+
.Returns(new[] {"p"});
752+
753+
var testStatementResult = new TestStatementResult(new[] {"p"}, recordMock.Object);
754+
testHarness.SetupCypherRequestResponse(cypherQuery.QueryText, cypherQuery.QueryParameters, testStatementResult);
755+
756+
var graphClient = await testHarness.CreateAndConnectBoltGraphClient();
757+
graphClient.JsonContractResolver = new CamelCasePropertyNamesContractResolver();
758+
var results = (await graphClient.ExecuteGetCypherResultsAsync<PathsResultBolt>(cypherQuery)).ToArray();
759+
760+
//Assert
761+
Assert.IsAssignableFrom<IEnumerable<PathsResultBolt>>(results);
762+
var resultPath = results.First();
763+
Assert.Equal(1, resultPath.Length);
764+
resultPath.Nodes.Count.Should().Be(2);
765+
resultPath.End.Id.Should().Be(1);
766+
resultPath.End.Labels.Should().BeEquivalentTo("Node");
767+
resultPath.Start.Id.Should().Be(2);
768+
resultPath.Start.Labels.Should().BeEquivalentTo("Node");
769+
resultPath.Relationships.Count.Should().Be(1);
770+
resultPath.Relationships[0].Id.Should().Be(3);
771+
resultPath.Relationships[0].StartNodeId.Should().Be(2);
772+
resultPath.Relationships[0].EndNodeId.Should().Be(1);
773+
resultPath.Relationships[0].Type.Should().Be("Foo");
774+
}
775+
}
721776
}
722777
}

Neo4jClient/ApiModels/Cypher/PathsResultBolt.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ internal PathsResultBolt(IPath path)
3939

4040
public class PathsResultBoltRelationship
4141
{
42+
[JsonProperty("Id")]
4243
public long Id { get; set; }
44+
[JsonProperty("Type")]
4345
public string Type { get; set; }
46+
[JsonProperty("StartNodeId")]
4447
public long StartNodeId { get; set; }
48+
[JsonProperty("EndNodeId")]
4549
public long EndNodeId { get; set; }
4650

4751
public object this[string key] => Properties[key];
4852

53+
[JsonProperty("Properties")]
4954
public Dictionary<string, object> Properties { get; set; }
5055

5156
public PathsResultBoltRelationship() { Properties = new Dictionary<string, object>(); }
@@ -90,9 +95,12 @@ public override int GetHashCode()
9095

9196
public class PathsResultBoltNode
9297
{
98+
[JsonProperty("Id")]
9399
public long Id { get; set; }
100+
[JsonProperty("Labels")]
94101
public List<string> Labels { get; set; }
95102
public object this[string key] => Properties[key];
103+
[JsonProperty("Properties")]
96104
public Dictionary<string, object> Properties { get; set; }
97105

98106
public PathsResultBoltNode() { Properties = new Dictionary<string, object>(); }

0 commit comments

Comments
 (0)