Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public static Snapshot Add(
IExecutionResult result,
string? name = null)
{

if (result.ContextData is null)
{
snapshot.Add(result.ToJson(), name);
Expand Down
15 changes: 12 additions & 3 deletions src/HotChocolate/Core/src/Types/Types/InputParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,17 +522,26 @@ private object DeserializeObject(object resultValue, InputObjectType type, Path

private object? CreateDefaultValue(InputField field, Path path, int stack)
{
object? value;

if (field.DefaultValue is null || field.DefaultValue.Kind == SyntaxKind.NullValue)
{
if (field.Type.Kind == TypeKind.NonNull)
{
throw RequiredInputFieldIsMissing(field, path);
}

return field.IsOptional ? new Optional(null, false) : null;
}
value = null;

object? value;
// if the type is nullable but the runtime type is a non-nullable value
// we will create a default instance and assign that instead.
if (field.RuntimeType.IsValueType)
{
value = Activator.CreateInstance(field.RuntimeType);
}

return field.IsOptional ? new Optional(value, false) : value;
}

try
{
Expand Down
31 changes: 31 additions & 0 deletions src/HotChocolate/Core/test/Types.Tests/Types/InputParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,30 @@ public void OneOf_only_B_has_Value()
runtimeValue.MatchSnapshot();
}

[Fact]
public void Force_NonNull_Struct_To_Be_Optional()
{
// arrange
var schema = SchemaBuilder.New()
.AddInputObjectType<Test4Input>(d => d.Field(t => t.Field2).Type<IntType>())
.ModifyOptions(o => o.StrictValidation = false)
.Create();

var type = schema.GetType<InputObjectType>("Test4Input");

var fieldData = new Dictionary<string, object?>
{
{ "field1", "abc" }
};

// act
var parser = new InputParser(new DefaultTypeConverter());
var runtimeValue = parser.ParseResult(fieldData, type, Path.Root);

// assert
Assert.IsType<Test4Input>(runtimeValue).MatchSnapshot();
}

public class TestInput
{
public string? Field1 { get; set; }
Expand Down Expand Up @@ -506,4 +530,11 @@ public class OneOfInput

public string? C { get; set; }
}

public class Test4Input
{
public string Field1 { get; set; } = default!;

public int Field2 { get; set; } = default!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Field1": "abc",
"Field2": 0
}