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

DefaultValue attribute on an input object doesn't get applied. #6958

Closed
wants to merge 1 commit into from
Closed
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
@@ -0,0 +1,62 @@
using HotChocolate.Execution;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using Xunit;

namespace HotChocolate.AspNetCore;

public class DefaultValueTests
{
public class MyInputObjectOut
{
public int Result { get; set; }
}

public class MyInputObject
{
[DefaultValue(500)]
public Optional<int> ValuesToRetrieveInBatch { get; set; }

}

public class Queries
{
public string Hello() => "Hello World";
}

public class Mutations
{
public MyInputObjectOut DoSomething(MyInputObject input) =>
new MyInputObjectOut() { Result = input.ValuesToRetrieveInBatch.Value };
}


[Fact]
public void DefaultValueTests_Simple()
{
// Arrange
var services = new ServiceCollection();
services
.AddGraphQLServer()
.AddQueryType<Queries>()
.AddMutationType<Mutations>();

ServiceProvider serviceProvider = services.BuildServiceProvider();
IRequestExecutorResolver executorResolver = serviceProvider.GetRequiredService<IRequestExecutorResolver>();
IRequestExecutor executor = executorResolver.GetRequestExecutorAsync().Result;

// Act
IExecutionResult result = executor.ExecuteAsync("mutation{ doSomething(input: { }) { result } }").Result;

// Extract the data from the result
var jsonResult = result.ToJson();

// Parse the JSON result and extract the 'result' value
var jObject = JObject.Parse(jsonResult);
var actualResult = jObject["data"]!["doSomething"]!["result"]!.Value<int>();

// Assert
Assert.Equal(500, actualResult);
}
}
4 changes: 2 additions & 2 deletions src/HotChocolate/Core/src/Abstractions/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ public static Optional<T> Empty(T? defaultValue = default)
/// </summary>
public static Optional<T> From(IOptional optional)
{
if (optional.HasValue)
if (optional.HasValue || optional.Value != default)
{
return new Optional<T>((T?)optional.Value);
return new Optional<T>((T?)optional.Value, optional.HasValue);
}

return new Optional<T>();
Expand Down
14 changes: 13 additions & 1 deletion src/HotChocolate/Core/test/Abstractions.Tests/OptionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,16 @@ public void Optional_From_Struct_Is_Not_Set()
Assert.False(fromEmptyOptional.HasValue);
Assert.True(fromEmptyOptional.IsEmpty);
}
}

[Fact]
public void Optional_From_DefaultValueAttribute_Provided()
{
const int defaultValue = 500;
Optional<int> a = Optional<int>.Empty(defaultValue);
var b = Optional<int>.From(a);

Assert.False(a.HasValue);
Assert.False(b.HasValue);
Assert.Equal(defaultValue, b.Value);
}
}
Loading