From 70147e3ffff6ac2c50575af682d32d6ee30423ec Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Fri, 24 Aug 2018 12:19:01 +0200 Subject: [PATCH] Fixed enum serialization issue (#218) --- src/AspNetCore.Tests/QueryMiddlewareTests.cs | 37 ++++++++++++++++++- src/AspNetCore.Tests/Schema/Foo.cs | 5 +++ src/AspNetCore.Tests/Schema/Query.cs | 5 +++ .../HttpPost_NestedEnumArgument.json | 6 +++ src/Types/Types/EnumType.cs | 7 ++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/AspNetCore.Tests/__snapshots__/HttpPost_NestedEnumArgument.json diff --git a/src/AspNetCore.Tests/QueryMiddlewareTests.cs b/src/AspNetCore.Tests/QueryMiddlewareTests.cs index ea46b919b53..612f282b947 100644 --- a/src/AspNetCore.Tests/QueryMiddlewareTests.cs +++ b/src/AspNetCore.Tests/QueryMiddlewareTests.cs @@ -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; @@ -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 + { + { "a", new Dictionary + { + { "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(json); + Assert.Null(result.Errors); + Assert.Equal(Snapshot.Current(), Snapshot.New(result)); + } + [Fact] public async Task HttpGet_BasicTest() { @@ -222,7 +253,11 @@ public async Task HttpPost_WithHttpContext() private TestServer CreateTestServer() { return TestServerFactory.Create( - c => c.RegisterQueryType(), null); + c => + { + c.RegisterQueryType(); + c.RegisterType>(); + }, null); } } } diff --git a/src/AspNetCore.Tests/Schema/Foo.cs b/src/AspNetCore.Tests/Schema/Foo.cs index 77b095bf78c..3c6f79ecd63 100644 --- a/src/AspNetCore.Tests/Schema/Foo.cs +++ b/src/AspNetCore.Tests/Schema/Foo.cs @@ -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; } + } } diff --git a/src/AspNetCore.Tests/Schema/Query.cs b/src/AspNetCore.Tests/Schema/Query.cs index 27dc6792b5c..5e5e3dff697 100644 --- a/src/AspNetCore.Tests/Schema/Query.cs +++ b/src/AspNetCore.Tests/Schema/Query.cs @@ -71,5 +71,10 @@ public bool GetWithEnum(TestEnum test) { return true; } + + public TestEnum GetWithNestedEnum(Bar bar) + { + return bar.A; + } } } diff --git a/src/AspNetCore.Tests/__snapshots__/HttpPost_NestedEnumArgument.json b/src/AspNetCore.Tests/__snapshots__/HttpPost_NestedEnumArgument.json new file mode 100644 index 00000000000..0389bfc11b6 --- /dev/null +++ b/src/AspNetCore.Tests/__snapshots__/HttpPost_NestedEnumArgument.json @@ -0,0 +1,6 @@ +{ + "Data": { + "withNestedEnum": "B" + }, + "Errors": null +} diff --git a/src/Types/Types/EnumType.cs b/src/Types/Types/EnumType.cs index 939591ee107..66735daaff3 100644 --- a/src/Types/Types/EnumType.cs +++ b/src/Types/Types/EnumType.cs @@ -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;