diff --git a/src/Core/Core/Execution/JsonQueryResultSerializer.cs b/src/Core/Core/Execution/JsonQueryResultSerializer.cs index 789451543a0..4e307cc552a 100644 --- a/src/Core/Core/Execution/JsonQueryResultSerializer.cs +++ b/src/Core/Core/Execution/JsonQueryResultSerializer.cs @@ -128,7 +128,7 @@ private static void WriteLocations(Utf8JsonWriter writer, IReadOnlyList path) writer.WriteStartArray(); - for (int i = 0; i < path.Count; i++) + for (var i = 0; i < path.Count; i++) { switch (path[i]) { @@ -212,7 +212,7 @@ private static void WritePath(Utf8JsonWriter writer, IReadOnlyList path) { writer.WriteStartArray(); - for (int i = 0; i < list.Count; i++) + for (var i = 0; i < list.Count; i++) { WriteFieldValue(writer, list[i]); } @@ -220,9 +220,7 @@ private static void WritePath(Utf8JsonWriter writer, IReadOnlyList path) writer.WriteEndArray(); } - private static void WriteFieldValue( - Utf8JsonWriter writer, - object value) + private static void WriteFieldValue(Utf8JsonWriter writer, object value) { if (value is null) { @@ -292,10 +290,13 @@ private static void WritePath(Utf8JsonWriter writer, IReadOnlyList path) writer.WriteStringValue(n.Value); break; + case Uri u: + writer.WriteStringValue(u.ToString()); + break; + default: - throw new NotSupportedException( - $"The specified type `{value.GetType().FullName}` " + - "is not supported by the result serializer."); + writer.WriteStringValue(value.ToString()); + break; } } } diff --git a/src/Core/Types/Types/Scalars/UrlType.cs b/src/Core/Types/Types/Scalars/UrlType.cs index 08a8ad35279..83ea0eb57d9 100644 --- a/src/Core/Types/Types/Scalars/UrlType.cs +++ b/src/Core/Types/Types/Scalars/UrlType.cs @@ -84,7 +84,7 @@ public override object Serialize(object value) if (value is Uri uri) { - return uri; + return uri.ToString(); } throw new ScalarSerializationException( diff --git a/src/Server/AspNetCore.Tests/PostMiddlewareTests.cs b/src/Server/AspNetCore.Tests/PostMiddlewareTests.cs index 4b11f7624b6..5d981181626 100644 --- a/src/Server/AspNetCore.Tests/PostMiddlewareTests.cs +++ b/src/Server/AspNetCore.Tests/PostMiddlewareTests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Net; using System.Net.Http; @@ -897,6 +898,38 @@ public async Task HttpPost_Ensure_Scoped_Services_Work() result.MatchSnapshot(); } + [Fact] + public async Task JsonSerializer_Handles_Serialization_Correctly() + { + // arrange + TestServer server = ServerFactory.Create( + services => + { + services.AddScoped(); + services.AddGraphQL( + SchemaBuilder.New() + .AddQueryType()); + }, + app => + app.UseGraphQL()); + + var request = + @" + { + validUri + } + "; + var contentType = "application/graphql"; + + // act + HttpResponseMessage message = + await server.SendPostRequestAsync(request, contentType, null); + + // assert + ClientQueryResult result = await DeserializeAsync(message); + result.MatchSnapshot(); + } + public class ScopedService { public int Count { get; private set; } @@ -906,5 +939,10 @@ public void Increase() Count += 1; } } + + public class Query + { + public Uri ValidUri() => new Uri("https://github.com/ChilliCream/hotchocolate"); + } } } diff --git a/src/Server/AspNetCore.Tests/__snapshots__/PostMiddlewareTests.JsonSerializer_Handles_Serialization_Correctly.snap b/src/Server/AspNetCore.Tests/__snapshots__/PostMiddlewareTests.JsonSerializer_Handles_Serialization_Correctly.snap new file mode 100644 index 00000000000..cc7667ed065 --- /dev/null +++ b/src/Server/AspNetCore.Tests/__snapshots__/PostMiddlewareTests.JsonSerializer_Handles_Serialization_Correctly.snap @@ -0,0 +1,7 @@ +{ + "Data": { + "validUri": "https://github.com/ChilliCream/hotchocolate" + }, + "Errors": null, + "Extensions": null +}