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
NullReferenceException when property is null #2152
Comments
Hi
This isn't the case - the expression compiles successfully. However when it's invoked you'll get a NullReferenceException if the property is null, in the same way if you were to access it directly outside of a rule definition. When you work with nested properties you should always include an appropriate null check, in the same way you would with regular code outside of fluentvalidation too.
Could you provide an example of the output so I can see what difference this makes when debugging? Thanks |
Yes you are correct. The compilation does not fail but the invokation. Still this is totally fine for me but I just wanna know where it's failing not that it doesn't fail at all. I wanna know what rule/expression fails.
Variables available in the scope where the exception is thrown (automatically provided by Rider when debugging/decompiling the lib): |
Could you run the FluentValidation.Benchmarks project both with and without this change to see if it makes any meaningful difference to performance? If it doesn't then I'd be fine with including it |
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19045 Before
After
As you can see times & allocations drastically increased. This was expected for me. So implementing it this way is a no go. Do you have any other idea? |
Yes with such a drastic difference this is a no-go. The only alternative I can think of is to catch NullReferenceExceptions and then re-throw them with additional detail such as the current rule expression |
But from what I can see this is not possible as the context (member info) is not captured. The exception is thrown in the compiled expression which has no information about the member which it was compiled for 🤔 |
It doesn't need to. The exception can be caught by the rule instance that invokes the expression (which does have that context available), and then re-throw it. |
Ah. If you are able to implement it that way I would greatly appreciate it :) |
I don't personally have the time to do that at the moment, but I'd be happy to review a PR. Otherwise I can look at it when I get back to working on 12.x, but I don't know when that'll be yet. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Merged for 11.9 |
FluentValidation version
latest
ASP.NET version
No response
Summary
When you validate an object that may have nullable properties:
and you validate their property without checking for null first
It throws a generic
NullReferenceException
because the rule (Expression
/Func
) cannot be compiled. From what I read so far this is by design. However this is very hard to debug and to find out what property exactly is failing. I forked this repo and modified theCreate
inPropertyRule
to include the expression in the closure. When you run you app in debug mode this massively helps you find out what property is null.However this obviously has some downsides to:
See repo for details. I even added a test to showcase this behaviour.
Is this the only way to debug this behaviour or is there another way?
Steps to Reproduce
See repo:
main...MeikelLP:FluentValidation:fix-null-debuggability
The text was updated successfully, but these errors were encountered: