Skip to content

Commit

Permalink
Fixed nullref issue with the result map (#2489)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Oct 27, 2020
1 parent 91dab88 commit 8cfda38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
Expand Up @@ -165,21 +165,21 @@ public IQueryResult BuildResult()

while (parent != null)
{
if (parent is ResultMap map &&
path is NamePathSegment nameSegment)
if (parent is ResultMap map && path is NamePathSegment nameSegment)
{
ResultValue value = map.GetValue(nameSegment.Name.Value, out int index);
ResultValue value = map.GetValue(nameSegment.Name.Value, out var index);

if (value.IsNullable)
{
map.SetValue(index, value.Name, null, true);
map.SetValue(index, value.Name, value: null, isNullable: true);
break;
}

if (index != -1)
{
map.RemoveValue(index);
}

path = path.Parent;
parent = parent.Parent;

Expand All @@ -193,7 +193,6 @@ public IQueryResult BuildResult()
else if (parent is ResultMapList mapList &&
path is IndexerPathSegment mapListIndexSegment)
{

if (mapList.IsNullable)
{
mapList[mapListIndexSegment.Index] = null;
Expand Down
5 changes: 4 additions & 1 deletion src/HotChocolate/Core/src/Execution/Processing/ResultMap.cs
Expand Up @@ -34,7 +34,7 @@ public ResultMap()
{
get
{
ResultValue value = GetValue(key, out int index);
ResultValue value = GetValue(key, out var index);
if (index == -1)
{
throw new KeyNotFoundException(key);
Expand Down Expand Up @@ -101,6 +101,8 @@ public ResultValue GetValue(string name, out int index)
var length = _capacity;
ref ResultValue searchSpace = ref MemoryMarshal.GetReference(_buffer.AsSpan());

// TODO : There is sometimes an issue with the last item of a batch
/*
while (length >= 8)
{
length -= 8;
Expand Down Expand Up @@ -136,6 +138,7 @@ public ResultValue GetValue(string name, out int index)
i += 4;
}
*/

while (length > 0)
{
Expand Down
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Xunit;

Expand Down Expand Up @@ -54,15 +55,15 @@ public void GetValue_ValueIsFound(int capacity)
var resultMap = new ResultMap();
resultMap.EnsureCapacity(capacity);
resultMap.SetValue(0, "abc", "def");
resultMap.SetValue(1, "def", "def");
resultMap.SetValue(2, "ghi", "def");
resultMap.SetValue(capacity / 2, "def", "def");
resultMap.SetValue(capacity - 1, "ghi", "def");

// act
ResultValue value = resultMap.GetValue("def", out int index);
ResultValue value = resultMap.GetValue("def", out var index);

// assert
Assert.Equal("def", value.Name);
Assert.Equal(1, index);
Assert.Equal(capacity / 2, index);
}

[Fact]
Expand Down

0 comments on commit 8cfda38

Please sign in to comment.