Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Enums do not deserialise correctly #218

Merged
merged 2 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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