-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Exceptions are thrown when using a custom PropertyHandler and a Where Expression to process an Enum value (that has NULL or invalid Enum Id); the Get/Set is never invoked. #991
Comments
#991 - Added the Integration Tests to guard the scenario before fixing.
@cajuncoding - we have created an Integration Test to guard this scenario, but on such Integration Test, this scenario is not failing. We (me personally) can able to intercept the Property Handlers even the values of the field from the database is We will create a small project that would simulate this using the latest releases at Nuget. We will share it here afterwards. |
@cajuncoding - here, we have simulated your scenarios and the issues are not occuring here. Would you be able to test it from here? For it to run, create a local DB named The following will be covered.
The model in topic has 2 nullable properties that is linked to a one enumeration. Both are targetting the Please do reshare this small solution if you can able to replicate your issue here. Thanks! |
BTW, we used the latest deployed version of RepoDB on the smal project we created - not the main branch. |
Ok, I was finally able to re-factor the repro solution, you provided, to match my own repro poc (with minimal changes I believe).... and finally got it to fail as I was expecting (initially I thought i tmight be that I'm on .Net v4.8 Framework, etc. but now it's failing the same way in the repro attached. I WAS able to get it working when using One of the key differences is that our Legacy code Models did not allow I'll def. keep an eye on the thread and/or Gitter in case you need anything else! Source Code Attached: WOOPS, looks like I left my Temp DB Connection String in the project; it's already been reset now so that connection is invalid. |
Thanks @cajuncoding - we missed that the exception is being triggered on the Expression parsing, and not on the hydration of the model. That would hopefully simplify the fix. We will give you an update on this soon. This is our priority. |
#991 Fixes to the issues and an additional Integration Tests.
The fix is now available at RepoDb v1.12.10-beta2 and RepoDb.SqlServer v1.1.5-beta2. Please do not hesitate to revert if the issue still persists on the said version. |
@mikependon Thanks for pushing the Fix. Good news is that the exceptions are definitely resolved, and the I wasn't seeing expected results from our database because the where expression (using a raw int that is out of range): Is being translated into the raw SQL: Why would and expression testing for Zero result in an IS Null in the raw SQL? If LinkType was nullable then this would make sense and I'd expect the Expression So currently the wrong results may occur for the out-of-range So it is working correctly to generate the expected SQL for in-range Enum values such as: ✅
However, the expression is translated to
|
We do understand this as we purposely return the null on the recent fix if it is out of bounds. This is an edge-case as the dirty records are persistent to the DB. This will never ever happen if RepoDB or EF is the ORM, I had supposed. We will fix this very soon. BTW, why not just add a new entry in your Enum for the value 0 as a recrification? |
Just FYI, here is the problem on the code as per your use-case. We will do issue you a new beta later. The fix for this would be below, in which it forces to utilize the private static object ToEnumValue(Type enumType,
object value) =>
(value != null ? ToEnumValue(enumType, Enum.GetName(enumType, value)) : null) ?? value; But, we need to capture the scenario by Integration Tests to guard it properly. |
@mikependon yeah, the current beta definitely unblocked us. So that was great! I just wanted to highlight that there are some odd behaviours on these edge cases. Especially since .Net behaves this way and allows and Enum to be cast to any We don't actually filter by the Enum in the business logic (that I'm aware of yet), but I had added some integration tests on our side that does as a validation of our migration to RepoDb. I'm pretty sure that we will add an Enum Thanks very much for the help! 👏👏👏 |
#991 Additional fixes for Enum Expression equals to 0.
We have push a new build that does contain a targeted fix on the issue you last mentioned here. Would you be able to update your local referenced packages to the following versions (RepoDb v1.12.10-beta3, RepoDb.SqlServer v1.1.5-beta3)? Please do not hesitate to revert if you still encountered the problems. |
@mikependon yes the new version has resolved all of our issues and my tests are passing as expected! All of the rendered raw sql looks good now for various Enum scenarios… I think this will likely help others using Enums eventually :-) Thanks again for the fast support…RepoDb is awesome and is my go to ORM! |
@cajuncoding - nice to know that the latest release has resolved all your problems. |
@cajuncoding - Enum is always a challenge to us (or maybe in general in ORM space), specially for Npgsql, in which they not handling the coercion to non-custom type in PGSQL. We also currently do the development to support this on the BulkOperations. 😃 |
Bug Description
When using a Property Handler and a Where Expression (Lambda filtering using the Model) to process an Enum value (that may be NULL or invalid/out-of-range Enum Id) the Get/Set are never called because an exception is raised while parsing the Enum; it appears this happens before the property handler is ever called. So the property Handler cannot be used (as expected) to override the RepoDb behvior and provide custom parsing/mapping logic to handle the edge cases.
This precludes the ability to use the Property Handler to correct the values and override the default parsing behavior of RepoDb.
Our use case specifically has an Enum
LinkType
that only has values1,2,3
(see below), but the DB has records with values ofNULL
and0
(Zero). Therefore these records fail to de-serialize into the Model; both with the same error message.In many cases this strict validation is great, and points to the the fact that we likely have bad data in our DB. But none-the-less for backwards compatibility there are many valid use-cases where it's critical to offer the flexibility for the consumer to control this.
And, ideally this would be done in a Property Handler whereby no additional validation from RepoDb is done. RepoDb should allow the
PropertyHandler
to completely control how a value from the DB is Mapped to/from the Model and the table without any exceptions being thrown outside of thePropertyHandler
.Exception Message:
Schema and Model:
Please share to us the schema of the table (not actual) that could help us replicate the issue if necessary.
And also the model that corresponds the schema.
Library Version:
Example: RepoDb v1.12.9 and RepoDb.SqlServer v1.1.4
The text was updated successfully, but these errors were encountered: