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 @@ -611,6 +611,49 @@ public void Single_Value_Can_Be_Coerced_Into_List_Variable()
""");
}

[Fact]
public void Error_When_Single_Value_Type_Does_Not_Match_List_Element_Type()
{
// arrange
var schema = SchemaBuilder.New().AddStarWarsTypes().Create();

var variableDefinitions = new List<VariableDefinitionNode>
{
new(null,
new VariableNode("abc"),
description: null,
new ListTypeNode(new NamedTypeNode("Int")),
null,
Array.Empty<DirectiveNode>())
};

var variableValues = JsonDocument.Parse("""{"abc": "xyz"}""");
var coercedValues = new Dictionary<string, VariableValue>();
var featureProvider = new MockFeatureProvider();
var helper = new VariableCoercionHelper(new());

// act
void Action() => helper.CoerceVariableValues(
schema, variableDefinitions, variableValues.RootElement, coercedValues, featureProvider);

// assert
Assert.Throws<LeafCoercionException>(Action)
.Errors.Select(t => t.WithException(null))
.ToList()
.MatchInlineSnapshot(
"""
"errors": [
{
"message": "Int cannot coerce the given value JSON element of type `String` to a runtime value.",
"path": [
"abc",
0
]
}
]
""");
}

[Fact]
public void StringValue_Representing_EnumValue_In_Single_Object_For_List_ShouldBe_Rewritten()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,27 @@ private bool TryParseAndValidate(
// Handle List types
if (type.Kind is TypeKind.List)
{
var elementType = (IInputType)type.ListType().ElementType;

if (element.ValueKind is not JsonValueKind.Array)
{
value = null;
error = ErrorBuilder.New()
.SetMessage("The value is not a list value.")
.SetExtension("variable", $"{path}")
.Build();
return false;
if (!TryParseAndValidate(
elementType,
element,
path,
depth + 1,
out var itemValue,
out error))
{
value = null;
return false;
}

value = new ListValueNode(itemValue);
error = null;
return true;
}

var elementType = (IInputType)type.ListType().ElementType;
var buffer = ArrayPool<IValueNode>.Shared.Rent(64);
var count = 0;

Comment thread
tobias-tengler marked this conversation as resolved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,138 @@ public void Coerce_String_WithEscapedQuotes_IsUnescaped()
Assert.Equal("tag:\"type_portable-lamp\"", stringValue.Value);
}

[Fact]
public void Single_Value_Can_Be_Coerced_Into_List_Variable()
{
// arrange
var schema = CreateCompositeSchema();

var variableDefinitions = new List<VariableDefinitionNode>
{
new(null,
new VariableNode("abc"),
description: null,
new ListTypeNode(new NamedTypeNode("String")),
null,
Array.Empty<DirectiveNode>())
};

using var variableValues = JsonDocument.Parse("""{"abc": "xyz"}""");

// act
var success = VariableCoercionHelper.TryCoerceVariableValues(
new MockFeatureProvider(),
schema,
variableDefinitions,
variableValues.RootElement,
out var coercedVariableValues,
out var error);

// assert
Assert.True(success, error?.Message);
Assert.Null(error);
Assert.NotNull(coercedVariableValues);

var list = Assert.IsType<ListValueNode>(coercedVariableValues["abc"].Value);
var item = Assert.Single(list.Items);
var stringValue = Assert.IsType<StringValueNode>(item);
Assert.Equal("xyz", stringValue.Value);
}

[Fact]
public void Error_When_Single_Value_Type_Does_Not_Match_List_Element_Type()
{
// arrange
var schema = CreateCompositeSchema();

var variableDefinitions = new List<VariableDefinitionNode>
{
new(null,
new VariableNode("abc"),
description: null,
new ListTypeNode(new NamedTypeNode("Int")),
null,
Array.Empty<DirectiveNode>())
};

using var variableValues = JsonDocument.Parse("""{"abc": "xyz"}""");

// act
var success = VariableCoercionHelper.TryCoerceVariableValues(
new MockFeatureProvider(),
schema,
variableDefinitions,
variableValues.RootElement,
out var coercedVariableValues,
out var error);

// assert
Assert.False(success);
Assert.Null(coercedVariableValues);
Assert.NotNull(error);

new[] { error.WithException(null) }
.ToList()
.MatchInlineSnapshot(
"""
"errors": [
{
"message": "The value `\"xyz\"` is not a valid value for the scalar type `Int`.",
"extensions": {
"variable": "abc"
}
}
]
""");
Comment thread
tobias-tengler marked this conversation as resolved.
}

[Fact]
public void Error_When_List_Element_Type_Does_Not_Match()
{
// arrange
var schema = CreateCompositeSchema();

var variableDefinitions = new List<VariableDefinitionNode>
{
new(null,
new VariableNode("abc"),
description: null,
new ListTypeNode(new NamedTypeNode("Int")),
null,
Array.Empty<DirectiveNode>())
};

using var variableValues = JsonDocument.Parse("""{"abc": [1, "xyz", 3]}""");

// act
var success = VariableCoercionHelper.TryCoerceVariableValues(
new MockFeatureProvider(),
schema,
variableDefinitions,
variableValues.RootElement,
out var coercedVariableValues,
out var error);

// assert
Assert.False(success);
Assert.Null(coercedVariableValues);
Assert.NotNull(error);

new[] { error.WithException(null) }
.ToList()
.MatchInlineSnapshot(
"""
"errors": [
{
"message": "The value `\"xyz\"` is not a valid value for the scalar type `Int`.",
"extensions": {
"variable": "abc[1]"
}
}
]
""");
}

private sealed class MockFeatureProvider : IFeatureProvider
{
public IFeatureCollection Features { get; } = new FeatureCollection();
Expand Down
Loading