Skip to content

Commit f2c48cb

Browse files
huysentruitwjoemcbride
authored andcommitted
Add support for float/long to double/decimal conversion (fixes graphql-dotnet#840) (graphql-dotnet#893)
1 parent c7b09e5 commit f2c48cb

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/GraphQL.Tests/ValueConverterFacts.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@ namespace GraphQL.Tests
77
{
88
public class ValueConverterFacts
99
{
10+
[Theory]
11+
[InlineData(1234L, 1234.0)]
12+
public void LongConversions(long source, object expected)
13+
{
14+
var actual = ValueConverter.ConvertTo(source, expected.GetType());
15+
16+
actual.ShouldBeOfType(expected.GetType());
17+
actual.ShouldBe(expected);
18+
}
19+
20+
[Theory]
21+
[InlineData(12.5f, 12.5)]
22+
public void FloatConversions(float source, object expected)
23+
{
24+
var actual = ValueConverter.ConvertTo(source, expected.GetType());
25+
26+
actual.ShouldBeOfType(expected.GetType());
27+
actual.ShouldBe(expected);
28+
}
29+
30+
[Theory]
31+
[InlineData(1234L, 1234.0)]
32+
[InlineData(12.5f, 12.5)]
33+
public void ToDecimalConversions(object source, object expected)
34+
{
35+
var actual = ValueConverter.ConvertTo(source, typeof(decimal));
36+
37+
actual.ShouldBeOfType(typeof(decimal));
38+
actual.ShouldBe(new decimal((double)expected));
39+
}
40+
1041
[Theory]
1142
[InlineData("100", "100")]
1243
[InlineData("100", 100)]

src/GraphQL/ValueConverter.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ static ValueConverter()
4545
Register(typeof(long), typeof(int), LongToInt);
4646
Register(typeof(long), typeof(uint), LongToUInt);
4747
Register(typeof(long), typeof(ulong), LongToULong);
48+
Register(typeof(long), typeof(double), LongToDouble);
49+
Register(typeof(long), typeof(decimal), LongToDecimal);
4850
Register(typeof(long), typeof(TimeSpan), LongToTimeSpan);
4951

52+
Register(typeof(float), typeof(double), FloatToDouble);
53+
Register(typeof(float), typeof(decimal), FloatToDecimal);
54+
5055
Register(typeof(double), typeof(decimal), DoubleToDecimal);
5156

5257
Register(typeof(string), typeof(Uri), ParseUri);
@@ -64,12 +69,36 @@ private static object IntToDecimal(object value)
6469
return Convert.ToDecimal(intValue, NumberFormatInfo.InvariantInfo);
6570
}
6671

72+
private static object FloatToDouble(object value)
73+
{
74+
var floatValue = (float)value;
75+
return (double)floatValue;
76+
}
77+
78+
private static object FloatToDecimal(object value)
79+
{
80+
var floatValue = (float)value;
81+
return Convert.ToDecimal(floatValue, NumberFormatInfo.InvariantInfo);
82+
}
83+
6784
private static object DoubleToDecimal(object value)
6885
{
6986
var doubleValue = (double)value;
7087
return Convert.ToDecimal(doubleValue, NumberFormatInfo.InvariantInfo);
7188
}
7289

90+
private static object LongToDouble(object value)
91+
{
92+
var longValue = (long)value;
93+
return (double)longValue;
94+
}
95+
96+
private static object LongToDecimal(object value)
97+
{
98+
var longValue = (long)value;
99+
return (decimal)longValue;
100+
}
101+
73102
private static object LongToInt(object value)
74103
{
75104
var longValue = (long)value;
@@ -210,8 +239,6 @@ private static object convertToUInt32<T>(T value) =>
210239
private static object convertToUInt64<T>(T value) =>
211240
Convert.ToUInt64(value, NumberFormatInfo.InvariantInfo);
212241

213-
214-
215242
public static object ConvertTo(object value, Type targetType)
216243
{
217244
if (value == null)

0 commit comments

Comments
 (0)