Skip to content

Commit

Permalink
Fixed issue JSON scalar crashes on JsonElement inputs #6023 (#6029)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
onionhammer and michaelstaib authored Apr 12, 2023
1 parent 3fa265d commit f49078e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/HotChocolate/Core/src/Types/Types/Scalars/JsonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ public static JsonElement Format(IValueNode node)
using var bufferWriter = new ArrayWriter();
using var jsonWriter = new Utf8JsonWriter(bufferWriter);
_visitor.Visit(node, new JsonFormatterContext(jsonWriter));
jsonWriter.Flush();

var jsonReader = new Utf8JsonReader(bufferWriter.GetSpan());
var jsonReader = new Utf8JsonReader(bufferWriter.Body.Span);
return JsonElement.ParseValue(ref jsonReader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ public async Task SimpleMutationExtension_Inferred_Execute()
result.MatchSnapshot();
}

[Fact]
public async Task SimpleJsonMutationExtension_Inferred_Execute()
{
var result =
await new ServiceCollection()
.AddGraphQL()
.AddMutationType()
.AddTypeExtension<SimpleJsonMutationExtension>()
.AddMutationConventions(
new MutationConventionOptions { ApplyToAllMutations = true })
.ModifyOptions(o => o.StrictValidation = false)
.ExecuteRequestAsync(
@"mutation {
doSomething(input: { something: 10 }) {
string
}
}");

result.MatchSnapshot();
}

[Fact]
public async Task Ensure_That_Directive_Middleware_Play_Nice()
{
Expand Down Expand Up @@ -983,6 +1004,13 @@ public string DoSomething(string something)
=> something;
}

[ExtendObjectType("Mutation")]
public class SimpleJsonMutationExtension
{
public string DoSomething(System.Text.Json.JsonElement something)
=> "Done";
}

public class SimpleMutationAttribute
{
[UseMutationConvention(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data": {
"doSomething": {
"string": "Done"
}
}
}
106 changes: 106 additions & 0 deletions src/HotChocolate/Core/test/Types.Tests/Types/JsonTypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Numerics;
using System.Text.Json;
using System.Threading.Tasks;
using CookieCrumble;
Expand Down Expand Up @@ -124,6 +125,111 @@ public async Task Input_Json_Object_Literal()
""");
}

[Theory]
[InlineData(0)]
[InlineData(-15)]
[InlineData(-10.5)]
[InlineData(1.5)]
[InlineData(1e15)]
public async Task Input_Json_Number_Literal(decimal value)
{
var result =
await new ServiceCollection()
.AddGraphQLServer()
.AddQueryType<Query>()
.ExecuteRequestAsync(
$$"""
{
inputJson(input: {{value}})
}
""");

result.MatchInlineSnapshot(
$$"""
{
"data": {
"inputJson": {{value}}
}
}
""");
}

[Fact]
public async Task Input_Json_BigInt_Literal()
{
var value = BigInteger.Parse("100000000000000000000000050");

var result =
await new ServiceCollection()
.AddGraphQLServer()
.AddQueryType<Query>()
.ExecuteRequestAsync(
$$"""
{
inputJson(input: {{value}})
}
""");

result.MatchInlineSnapshot(
$$"""
{
"data": {
"inputJson": {{value}}
}
}
""");
}

[Fact]
public async Task Input_Json_Exponent_Literal()
{
var result =
await new ServiceCollection()
.AddGraphQLServer()
.AddQueryType<Query>()
.ExecuteRequestAsync(
"""
{
inputJson(input: 1e1345)
}
""");

result.MatchInlineSnapshot(
"""
{
"data": {
"inputJson": 1e1345
}
}
""");
}

[Theory]
[InlineData("true")]
[InlineData("false")]
public async Task Input_Json_Bool_Literal(string value)
{
var result =
await new ServiceCollection()
.AddGraphQLServer()
.AddQueryType<Query>()
.ExecuteRequestAsync(
$$"""
{
inputJson(input: {{value}})
}
""");

result.MatchInlineSnapshot(
$$"""
{
"data": {
"inputJson": {{value}}
}
}
""");
}

[Fact]
public async Task Input_Json_Object_List()
{
Expand Down

0 comments on commit f49078e

Please sign in to comment.