Skip to content

Commit 91e1def

Browse files
authored
DefaultValue attribute on an input object doesn't get applied (#6897) (#6922)
1 parent 24c8be8 commit 91e1def

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using HotChocolate.Execution;
2+
using HotChocolate.Types;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Newtonsoft.Json.Linq;
5+
using Xunit;
6+
7+
namespace HotChocolate.AspNetCore;
8+
9+
public class DefaultValueTests
10+
{
11+
public class MyInputObjectOut
12+
{
13+
public int Result { get; set; }
14+
}
15+
16+
public class MyInputObject
17+
{
18+
[DefaultValue(500)]
19+
public Optional<int> ValuesToRetrieveInBatch { get; set; }
20+
21+
}
22+
23+
public class Queries
24+
{
25+
public string Hello() => "Hello World";
26+
}
27+
28+
public class Mutations
29+
{
30+
public MyInputObjectOut DoSomething(MyInputObject input) =>
31+
new MyInputObjectOut() { Result = input.ValuesToRetrieveInBatch.Value };
32+
}
33+
34+
35+
[Fact]
36+
public void DefaultValueTests_Simple()
37+
{
38+
// Arrange
39+
var services = new ServiceCollection();
40+
services
41+
.AddGraphQLServer()
42+
.AddQueryType<Queries>()
43+
.AddMutationType<Mutations>();
44+
45+
ServiceProvider serviceProvider = services.BuildServiceProvider();
46+
IRequestExecutorResolver executorResolver = serviceProvider.GetRequiredService<IRequestExecutorResolver>();
47+
IRequestExecutor executor = executorResolver.GetRequestExecutorAsync().Result;
48+
49+
// Act
50+
IExecutionResult result = executor.ExecuteAsync("mutation{ doSomething(input: { }) { result } }").Result;
51+
52+
// Extract the data from the result
53+
var jsonResult = result.ToJson();
54+
55+
// Parse the JSON result and extract the 'result' value
56+
var jObject = JObject.Parse(jsonResult);
57+
var actualResult = jObject["data"]!["doSomething"]!["result"]!.Value<int>();
58+
59+
// Assert
60+
Assert.Equal(500, actualResult);
61+
}
62+
}

src/HotChocolate/Core/src/Abstractions/Optional.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ public static Optional<T> Empty(T? defaultValue = default)
164164
/// </summary>
165165
public static Optional<T> From(IOptional optional)
166166
{
167-
if (optional.HasValue)
167+
if (optional.HasValue || optional.Value != default)
168168
{
169-
return new Optional<T>((T?)optional.Value);
169+
return new Optional<T>((T?)optional.Value, optional.HasValue);
170170
}
171171

172172
return new Optional<T>();

src/HotChocolate/Core/test/Abstractions.Tests/OptionalTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,16 @@ public void Optional_From_Struct_Is_Not_Set()
187187
Assert.False(fromEmptyOptional.HasValue);
188188
Assert.True(fromEmptyOptional.IsEmpty);
189189
}
190-
}
190+
191+
[Fact]
192+
public void Optional_From_DefaultValueAttribute_Provided()
193+
{
194+
const int defaultValue = 500;
195+
Optional<int> a = Optional<int>.Empty(defaultValue);
196+
var b = Optional<int>.From(a);
197+
198+
Assert.False(a.HasValue);
199+
Assert.False(b.HasValue);
200+
Assert.Equal(defaultValue, b.Value);
201+
}
202+
}

0 commit comments

Comments
 (0)