From d6e21b86a559b3ccccdbac133a5f424c4121e907 Mon Sep 17 00:00:00 2001 From: Ruben Bisharyan Date: Wed, 24 Apr 2024 09:49:24 +0400 Subject: [PATCH] Added BaseConverter supported characters check before conversion --- src/EFCoreQueryMagic/EFCoreQueryMagic.csproj | 2 +- .../Helpers/PropertyHelper.cs | 11 ++++++++++ .../FilterTests/LongTests.cs | 22 +++++++++++++++++++ .../Infrastructure/TestDbContext.cs | 2 +- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/EFCoreQueryMagic/EFCoreQueryMagic.csproj b/src/EFCoreQueryMagic/EFCoreQueryMagic.csproj index 2f7ef44..1e1a550 100644 --- a/src/EFCoreQueryMagic/EFCoreQueryMagic.csproj +++ b/src/EFCoreQueryMagic/EFCoreQueryMagic.csproj @@ -8,7 +8,7 @@ Readme.md PandaTech MIT - 1.0.4 + 1.0.5 Pandatech.EFCoreQueryMagic Pandatech.EFCoreQueryMagic: Dynamic Query and Filter Generator for EF Core Unlock the full potential of your Entity Framework Core applications with Pandatech.EFCoreQueryMagic. This innovative package empowers developers to seamlessly create dynamic, complex queries and filters for SQL tables without diving deep into the intricacies of LINQ or manual query construction. Designed to enhance productivity and maintainability, EFCoreQueryMagic automates the translation of front-end filter requests into optimized, ready-to-execute EF Core queries. Embrace the magic of streamlined data retrieval and manipulation, and elevate your applications to new heights of efficiency and performance. diff --git a/src/EFCoreQueryMagic/Helpers/PropertyHelper.cs b/src/EFCoreQueryMagic/Helpers/PropertyHelper.cs index 540a06d..d480285 100644 --- a/src/EFCoreQueryMagic/Helpers/PropertyHelper.cs +++ b/src/EFCoreQueryMagic/Helpers/PropertyHelper.cs @@ -1,5 +1,6 @@ using System.Linq.Expressions; using System.Text.Json; +using BaseConverter; using EFCoreQueryMagic.Attributes; using EFCoreQueryMagic.Converters; using EFCoreQueryMagic.Dto; @@ -52,6 +53,16 @@ public static List GetValues(this FilterDto filter, MappedToPropertyAttrib typeof(PropertyHelper).GetMethod("FromJsonElement")!.MakeGenericMethod(fromJsonElementType); var val = fromJsonElementMethod.Invoke(null, [value, propertyAttribute])!; + // Check supported characters for Base Converter + if (converter is FilterPandaBaseConverter) + { + var base36Chars = PandaBaseConverter.Base36Chars; + if (!base36Chars.Contains(val.ToString()!)) + { + throw new UnsupportedValueException($"Property {filter.PropertyName} has unsupported value"); + } + } + var valConverted = method.Invoke(converter, [val])!; list.Add((T)valConverted); diff --git a/test/EFCoreQueryMagic.Test/FilterTests/LongTests.cs b/test/EFCoreQueryMagic.Test/FilterTests/LongTests.cs index fc21760..9bdb7a6 100644 --- a/test/EFCoreQueryMagic.Test/FilterTests/LongTests.cs +++ b/test/EFCoreQueryMagic.Test/FilterTests/LongTests.cs @@ -1,6 +1,7 @@ using BaseConverter; using EFCoreQueryMagic.Dto; using EFCoreQueryMagic.Enums; +using EFCoreQueryMagic.Exceptions; using EFCoreQueryMagic.Extensions; using EFCoreQueryMagic.Test.EntityFilters; using EFCoreQueryMagic.Test.Infrastructure; @@ -38,4 +39,25 @@ public void TestEmptyValues() query.Should().Equal(result); } + + [Fact] + public void TestBaseConverterWithWrongCharacter() + { + var set = _context.Orders; + + var qString = new GetDataRequest + { + Filters = + [ + new FilterDto + { + Values = ["ա"], + ComparisonType = ComparisonType.Equal, + PropertyName = nameof(OrderFilter.Id) + } + ] + }; + + Assert.Throws(() => set.ApplyFilters(qString.Filters)); + } } \ No newline at end of file diff --git a/test/EFCoreQueryMagic.Test/Infrastructure/TestDbContext.cs b/test/EFCoreQueryMagic.Test/Infrastructure/TestDbContext.cs index 25dd0f9..b383577 100644 --- a/test/EFCoreQueryMagic.Test/Infrastructure/TestDbContext.cs +++ b/test/EFCoreQueryMagic.Test/Infrastructure/TestDbContext.cs @@ -15,7 +15,7 @@ public class TestDbContext(DbContextOptions options) public static TestDbContext CreateNewInMemoryContext() { var options = new DbContextOptionsBuilder() - .UseNpgsql("Host=localhost;Database=filter_tests;Username=postgres;Password=root") + .UseNpgsql("Host=localhost;Database=filter_tests;Username=test;Password=test") // .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) // Use a unique name for the database to avoid conflicts between tests .Options;