diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml index 4ecd3f3..285ae15 100644 --- a/.github/workflows/build-and-deploy.yml +++ b/.github/workflows/build-and-deploy.yml @@ -8,7 +8,7 @@ on: - master env: APP_NAME: DynamicExpression - VERSION: 10.0.1 + VERSION: 10.0.2 NUGET_HOST: https://api.nuget.org/v3/index.json NUGET_APIKEY: ${{ secrets.NUGET_APIKEY }} jobs: diff --git a/DynamicExpression/DynamicExpression.csproj b/DynamicExpression/DynamicExpression.csproj index c2eb8b1..306dc47 100644 --- a/DynamicExpression/DynamicExpression.csproj +++ b/DynamicExpression/DynamicExpression.csproj @@ -15,8 +15,7 @@ Lampda, Expression, Linq, Dynamic, Criteria, Query, Paging, Pagination, Sort, Sorting README.md - - Updated NuGets. - - Added Geometry converter for query model binder. + - Fixed The QueryModelBinder for enum values in querystring criteria. https://raw.githubusercontent.com/vivet/DynamicExpression/master/LICENSE LICENSE diff --git a/DynamicExpression/ModelBinders/QueryModelBinder.cs b/DynamicExpression/ModelBinders/QueryModelBinder.cs index d8adf67..b5a3c36 100644 --- a/DynamicExpression/ModelBinders/QueryModelBinder.cs +++ b/DynamicExpression/ModelBinders/QueryModelBinder.cs @@ -182,61 +182,81 @@ protected virtual TCriteria GetCriteria(HttpRequest httpRequest, Query + var propertyInfos = typeof(TCriteria) + .GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance); + + foreach (var propertyInfo in propertyInfos) + { + var bodyValue = propertyInfo + .GetValue(query.Criteria); + + if (bodyValue != null) + { + propertyInfo + .SetValue(criteria, bodyValue); + + continue; + } + + var success = httpRequest.Query.TryGetValue($"{nameof(Criteria)}.{propertyInfo.Name}", out var values); + + if (!success) + { + continue; + } + + var value = values + .FirstOrDefault(); + + if (value == null) + { + continue; + } + + if (propertyInfo.PropertyType == typeof(TimeSpan) || propertyInfo.PropertyType == typeof(TimeSpan?)) + { + propertyInfo + .SetValue(criteria, TimeSpan.Parse(value)); + } + else if (propertyInfo.PropertyType == typeof(TimeOnly) || propertyInfo.PropertyType == typeof(TimeOnly?)) + { + propertyInfo + .SetValue(criteria, TimeOnly.Parse(value)); + } + else if (propertyInfo.PropertyType == typeof(DateOnly) || propertyInfo.PropertyType == typeof(DateOnly?)) + { + propertyInfo + .SetValue(criteria, DateOnly.Parse(value)); + } + else if (propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?)) + { + propertyInfo + .SetValue(criteria, DateTime.Parse(value)); + } + else if (propertyInfo.PropertyType == typeof(DateTimeOffset) || propertyInfo.PropertyType == typeof(DateTimeOffset?)) { - var bodyValue = x.GetValue(query.Criteria); - - if (bodyValue != null) - { - x.SetValue(criteria, bodyValue); - - return; - } - - var success = httpRequest.Query.TryGetValue($"{nameof(Criteria)}.{x.Name}", out var values); - if (!success) - { - return; - } - - var value = values.FirstOrDefault(); - if (value == null) - { - return; - } - - if (x.PropertyType == typeof(TimeSpan) || x.PropertyType == typeof(TimeSpan?)) - { - x.SetValue(criteria, TimeSpan.Parse(value)); - } - else if (x.PropertyType == typeof(TimeOnly) || x.PropertyType == typeof(TimeOnly?)) - { - x.SetValue(criteria, TimeOnly.Parse(value)); - } - else if (x.PropertyType == typeof(DateOnly) || x.PropertyType == typeof(DateOnly?)) - { - x.SetValue(criteria, DateOnly.Parse(value)); - } - else if (x.PropertyType == typeof(DateTime) || x.PropertyType == typeof(DateTime?)) - { - x.SetValue(criteria, DateTime.Parse(value)); - } - else if (x.PropertyType == typeof(DateTimeOffset) || x.PropertyType == typeof(DateTimeOffset?)) - { - x.SetValue(criteria, DateTimeOffset.Parse(value)); - } - else if (x.PropertyType == typeof(Guid) || x.PropertyType == typeof(Guid?)) - { - x.SetValue(criteria, Guid.Parse(value)); - } - else - { - x.SetValue(criteria, value); - } - }); + propertyInfo + .SetValue(criteria, DateTimeOffset.Parse(value)); + } + else if (propertyInfo.PropertyType == typeof(Guid) || propertyInfo.PropertyType == typeof(Guid?)) + { + propertyInfo + .SetValue(criteria, Guid.Parse(value)); + } + else if (propertyInfo.PropertyType.IsEnum || (Nullable.GetUnderlyingType(propertyInfo.PropertyType)?.IsEnum ?? false)) + { + var enumType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType; + var enumValue = Enum.Parse(enumType, value, ignoreCase: true); + + propertyInfo + .SetValue(criteria, enumValue); + } + else + { + propertyInfo + .SetValue(criteria, value); + } + } return criteria; }