diff options
author | raghuramn <ranadimi@microsoft.com> | 2012-10-05 19:51:09 +0400 |
---|---|---|
committer | raghuramn <ranadimi@microsoft.com> | 2012-10-09 01:16:14 +0400 |
commit | d5968944dd7eaac3a503a1f419aa45990b9ea5f6 (patch) | |
tree | 027a0912696005557b03fa0c704421f1a70915ae | |
parent | aef8ab9a13804e540435c6bcd3ec322446c7ae9b (diff) |
Issue 477: QueryableAttribute failed when working with UInt32 and UInt64
property
The root issue here is comparison of int? to int64 that
results in a cast that fails if the value of int? is null.
3 files changed, 37 insertions, 1 deletions
diff --git a/src/System.Web.Http.OData/OData/Query/Expressions/FilterBinder.cs b/src/System.Web.Http.OData/OData/Query/Expressions/FilterBinder.cs index 84256e32..ab8179d1 100644 --- a/src/System.Web.Http.OData/OData/Query/Expressions/FilterBinder.cs +++ b/src/System.Web.Http.OData/OData/Query/Expressions/FilterBinder.cs @@ -282,7 +282,21 @@ namespace System.Web.Http.OData.Query.Expressions } else { - return Expression.Convert(source, conversionType); + // if a cast is from Nullable<T> to Non-Nullable<T> we need to check if source is null + if (_querySettings.HandleNullPropagation == HandleNullPropagationOption.True + && IsNullable(source.Type) && !IsNullable(conversionType)) + { + // source == null ? null : source.Value + return + Expression.Condition( + test: CheckForNull(source), + ifTrue: Expression.Constant(null, ToNullable(conversionType)), + ifFalse: Expression.Convert(ExtractValueFromNullableExpression(source), ToNullable(conversionType))); + } + else + { + return Expression.Convert(source, conversionType); + } } } } diff --git a/test/System.Web.Http.OData.Test/OData/Query/Expressions/DataModel.cs b/test/System.Web.Http.OData.Test/OData/Query/Expressions/DataModel.cs index 6bb62e1a..dac21da8 100644 --- a/test/System.Web.Http.OData.Test/OData/Query/Expressions/DataModel.cs +++ b/test/System.Web.Http.OData.Test/OData/Query/Expressions/DataModel.cs @@ -67,12 +67,17 @@ namespace System.Web.Http.OData.Query.Expressions public float FloatProp { get; set; } public long LongProp { get; set; } public int IntProp { get; set; } + public string StringProp { get; set; } public ushort UShortProp { get; set; } public uint UIntProp { get; set; } public ulong ULongProp { get; set; } public char CharProp { get; set; } + public short? NullableShortProp { get; set; } + public int? NullableIntProp { get; set; } + public long? NullableLongProp { get; set; } + public ushort? NullableUShortProp { get; set; } public uint? NullableUIntProp { get; set; } public ulong? NullableULongProp { get; set; } diff --git a/test/System.Web.Http.OData.Test/OData/Query/Expressions/FilterBinderTests.cs b/test/System.Web.Http.OData.Test/OData/Query/Expressions/FilterBinderTests.cs index ad2aec17..20ca8d79 100644 --- a/test/System.Web.Http.OData.Test/OData/Query/Expressions/FilterBinderTests.cs +++ b/test/System.Web.Http.OData.Test/OData/Query/Expressions/FilterBinderTests.cs @@ -378,6 +378,23 @@ namespace System.Web.Http.OData.Query.Expressions } #endregion + // Issue: 477 + [Theory] + [InlineData("indexof('hello', StringProp) gt UIntProp")] + [InlineData("indexof('hello', StringProp) gt ULongProp")] + [InlineData("indexof('hello', StringProp) gt UShortProp")] + [InlineData("indexof('hello', StringProp) gt NullableUShortProp")] + [InlineData("indexof('hello', StringProp) gt NullableUIntProp")] + [InlineData("indexof('hello', StringProp) gt NullableULongProp")] + public void ComparisonsInvolvingCastsAndNullableValues(string filter) + { + var filters = VerifyQueryDeserialization<DataTypes>(filter); + + RunFilters(filters, + new DataTypes(), + new { WithNullPropagation = false, WithoutNullPropagation = typeof(ArgumentNullException) }); + } + [Theory] [PropertyData("LongInputs")] public void LongInputs_CauseRecursionLimitExceededException(string filter) |