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

Fix issue JSON scalar crashes on JsonElement inputs #6023 #6029

Merged
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
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();

michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
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