Skip to content

Commit 6678d87

Browse files
committed
Fixed: incoming string representation should not be part of the semantic expression value; fix potential NullReferenceException; use culture-insensitive conversion when no string specified
1 parent 012cf4e commit 6678d87

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/JsonApiDotNetCore/Queries/Expressions/LiteralConstantExpression.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using JetBrains.Annotations;
23

34
namespace JsonApiDotNetCore.Queries.Expressions;
@@ -8,12 +9,13 @@ namespace JsonApiDotNetCore.Queries.Expressions;
89
[PublicAPI]
910
public class LiteralConstantExpression : IdentifierExpression
1011
{
12+
// Only used to show the original input, in case expression parse failed. Not part of the semantic expression value.
1113
private readonly string _stringValue;
1214

1315
public object TypedValue { get; }
1416

1517
public LiteralConstantExpression(object typedValue)
16-
: this(typedValue, typedValue.ToString()!)
18+
: this(typedValue, GetStringValue(typedValue)!)
1719
{
1820
}
1921

@@ -26,6 +28,13 @@ public LiteralConstantExpression(object typedValue, string stringValue)
2628
_stringValue = stringValue;
2729
}
2830

31+
private static string? GetStringValue(object typedValue)
32+
{
33+
ArgumentGuard.NotNull(typedValue);
34+
35+
return typedValue is IFormattable cultureAwareValue ? cultureAwareValue.ToString(null, CultureInfo.InvariantCulture) : typedValue.ToString();
36+
}
37+
2938
public override TResult Accept<TArgument, TResult>(QueryExpressionVisitor<TArgument, TResult> visitor, TArgument argument)
3039
{
3140
return visitor.VisitLiteralConstant(this, argument);
@@ -56,11 +65,11 @@ public override bool Equals(object? obj)
5665

5766
var other = (LiteralConstantExpression)obj;
5867

59-
return Equals(TypedValue, other.TypedValue) && _stringValue == other._stringValue;
68+
return TypedValue.Equals(other.TypedValue);
6069
}
6170

6271
public override int GetHashCode()
6372
{
64-
return HashCode.Combine(TypedValue, _stringValue);
73+
return TypedValue.GetHashCode();
6574
}
6675
}

0 commit comments

Comments
 (0)