Skip to content

Commit

Permalink
Fixed enum serialization issue (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Aug 24, 2018
1 parent 8206934 commit 70147e3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/AspNetCore.Tests/QueryMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using HotChocolate.Types;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -64,6 +65,36 @@ public async Task HttpPost_EnumArgument()
Assert.Equal(Snapshot.Current(), Snapshot.New(result));
}

[Fact]
public async Task HttpPost_NestedEnumArgument()
{
// arrange
TestServer server = CreateTestServer();
QueryRequestDto request = new QueryRequestDto
{
Query = "query a($a: BarInput) { withNestedEnum(bar: $a) }",
Variables = JObject.FromObject(new Dictionary<string, object>
{
{ "a", new Dictionary<string, object>
{
{ "a", "B" }
}
}
})
};

// act
HttpResponseMessage message = await server.SendRequestAsync(request);

// assert
Assert.Equal(HttpStatusCode.OK, message.StatusCode);

string json = await message.Content.ReadAsStringAsync();
QueryResultDto result = JsonConvert.DeserializeObject<QueryResultDto>(json);
Assert.Null(result.Errors);
Assert.Equal(Snapshot.Current(), Snapshot.New(result));
}

[Fact]
public async Task HttpGet_BasicTest()
{
Expand Down Expand Up @@ -222,7 +253,11 @@ public async Task HttpPost_WithHttpContext()
private TestServer CreateTestServer()
{
return TestServerFactory.Create(
c => c.RegisterQueryType<QueryType>(), null);
c =>
{
c.RegisterQueryType<QueryType>();
c.RegisterType<InputObjectType<Bar>>();
}, null);
}
}
}
5 changes: 5 additions & 0 deletions src/AspNetCore.Tests/Schema/Foo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ public class Foo
public string B { get; set; }
public int C { get; set; }
}

public class Bar
{
public TestEnum A { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/AspNetCore.Tests/Schema/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ public bool GetWithEnum(TestEnum test)
{
return true;
}

public TestEnum GetWithNestedEnum(Bar bar)
{
return bar.A;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Data": {
"withNestedEnum": "B"
},
"Errors": null
}
7 changes: 7 additions & 0 deletions src/Types/Types/EnumType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public object ParseLiteral(IValueNode literal)
return ev.Value;
}

// TODO : This fixes a deserialisation issue when an input object is deserialized from a json string. We should however fix this in the aspnet middleware.
if (literal is StringValueNode svn
&& _nameToValues.TryGetValue(svn.Value, out ev))
{
return ev.Value;
}

if (literal is NullValueNode)
{
return null;
Expand Down

0 comments on commit 70147e3

Please sign in to comment.