Skip to content

Commit 09a7c51

Browse files
authored
Fixed ignored invalid field when enum is present (#8026)
1 parent 981f833 commit 09a7c51

7 files changed

+85
-9
lines changed

src/HotChocolate/Core/src/Execution/Processing/VariableCoercionHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ private static ObjectValueNode Rewrite(
173173
{
174174
// if we do not find a field on the type we also skip this error and let
175175
// the deserialization produce a proper error on this.
176+
fields?.Add(current);
176177
continue;
177178
}
178179

src/HotChocolate/Core/test/Execution.Tests/Integration/Components/VariableCoercionIntegrationTests.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public async Task Nullables_And_NonNullables_Are_Set()
1414

1515
var user = new ObjectValueNode(
1616
new ObjectFieldNode("name", "Oliver"),
17-
new ObjectFieldNode("surname", "Smith"));
17+
new ObjectFieldNode("surname", "Smith"),
18+
new ObjectFieldNode("gender", "MALE"));
1819

1920
var request =
2021
OperationRequestBuilder
@@ -51,7 +52,8 @@ public async Task Nullables_Are_Set_And_NonNullables_Are_Set_To_Null()
5152

5253
var user = new ObjectValueNode(
5354
new ObjectFieldNode("name", NullValueNode.Default),
54-
new ObjectFieldNode("surname", "Smith"));
55+
new ObjectFieldNode("surname", "Smith"),
56+
new ObjectFieldNode("gender", "MALE"));
5557

5658
var request =
5759
OperationRequestBuilder
@@ -69,7 +71,8 @@ public async Task Nullables_Are_Set_And_NonNullables_Not_Are_Set()
6971
var executor = await CreateSchemaAsync();
7072

7173
var user = new ObjectValueNode(
72-
new ObjectFieldNode("surname", "Smith"));
74+
new ObjectFieldNode("surname", "Smith"),
75+
new ObjectFieldNode("gender", "MALE"));
7376

7477
var request =
7578
OperationRequestBuilder
@@ -147,6 +150,46 @@ public async Task Invalid_Field_Provided()
147150
await executor.ExecuteAsync(request).MatchSnapshotAsync();
148151
}
149152

153+
[Fact]
154+
public async Task Invalid_Field_Provided_When_Enum_Is_Present()
155+
{
156+
var executor = await CreateSchemaAsync();
157+
158+
var user = new ObjectValueNode(
159+
new ObjectFieldNode("name", "Oliver"),
160+
new ObjectFieldNode("surname", "Smith"),
161+
new ObjectFieldNode("gender", "MALE"),
162+
new ObjectFieldNode("foo", "bar"));
163+
164+
var request =
165+
OperationRequestBuilder
166+
.New()
167+
.SetDocument("mutation($user: UserInput!) { addUser(user: $user) }")
168+
.SetVariableValues(new Dictionary<string, object?> { {"user", user }, })
169+
.Build();
170+
171+
await executor.ExecuteAsync(request).MatchSnapshotAsync();
172+
}
173+
174+
[Fact]
175+
public async Task Invalid_Enum_Value_Provided()
176+
{
177+
var executor = await CreateSchemaAsync();
178+
179+
var user = new ObjectValueNode(
180+
new ObjectFieldNode("name", "Oliver"),
181+
new ObjectFieldNode("gender", "FOO"));
182+
183+
var request =
184+
OperationRequestBuilder
185+
.New()
186+
.SetDocument("mutation($user: UserInput!) { addUser(user: $user) }")
187+
.SetVariableValues(new Dictionary<string, object?> { {"user", user }, })
188+
.Build();
189+
190+
await executor.ExecuteAsync(request).MatchSnapshotAsync();
191+
}
192+
150193
private static async Task<IRequestExecutor> CreateSchemaAsync()
151194
{
152195
return await new ServiceCollection()
@@ -160,7 +203,7 @@ public class UserMutation
160203
{
161204
public string AddUser(User user)
162205
{
163-
return user.Name + " " + user.Surname + " was added!";
206+
return $"{user.Name} {user.Surname} ({user.Gender}) was added!";
164207
}
165208
}
166209

@@ -181,8 +224,12 @@ public class User(string name)
181224
public string Name { get; set; } = name;
182225

183226
public string? Surname { get; set; }
227+
228+
public GenderEnum? Gender { get; set; }
184229
}
185230

231+
public enum GenderEnum { Male, Female }
232+
186233
public class UserInputType : InputObjectType<User>
187234
{
188235
protected override void Configure(IInputObjectTypeDescriptor<User> descriptor)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "GenderEnum cannot parse the given literal of type `EnumValueNode`.",
5+
"path": [
6+
"user",
7+
"gender"
8+
],
9+
"extensions": {
10+
"fieldCoordinate": "UserInput.gender",
11+
"fieldType": "GenderEnum"
12+
}
13+
}
14+
]
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "The field `foo` does not exist on the type `UserInput`.",
5+
"path": [
6+
"user"
7+
],
8+
"extensions": {
9+
"type": "UserInput"
10+
}
11+
}
12+
]
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{
1+
{
22
"data": {
3-
"addUser": "Oliver Smith was added!"
3+
"addUser": "Oliver Smith (Male) was added!"
44
}
55
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{
1+
{
22
"data": {
3-
"addUser": "Oliver was added!"
3+
"addUser": "Oliver () was added!"
44
}
55
}

src/HotChocolate/Core/test/Execution.Tests/Integration/Components/__snapshots__/VariableCoercionIntegrationTests.Nullables_Are_Set_And_NonNullables_Are_Set_To_Null.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"errors": [
33
{
44
"message": "Cannot accept null for non-nullable input.",

0 commit comments

Comments
 (0)