[ExpressionLanguage][FEATURE] Add support for Nullsafe syntax #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a long-time-lasted feature for the
ExpressionLanguage
component. I've been waiting for the support ofNullsafe operator
in expressions dealing with mutable objects, until I finally decided to work on it once for all 👍The lack of nullsafety feature has been repeatedly reported as a BUG several time (e.g #45411 & #21691) when it is actually a missing feature.
Currently, expressions like
foo.bar
assumes that the propertybar
"always" exists on the objectfoo
and if doesn't the parser throws aRuntimeException
. Although, sometimes, that's exactly the behavior we need, some other times we may work with mutable objects with uncontrolled structure, thus, such assumption is error-prone and will force adding extra checks making the expression uglier and less readable.The proposed work, introduces the support for the
?.
syntax alongside with the usual.
syntax to help working with objects with dynamic structure. The two notations works identically in all normal cases. The difference occurs when trying to access non-existant properties and/or methods where the.
notation will throw aRuntimeException
as usual and the?.
notation will returnnull
instead and no errors nor exceptions will be thrown. Hence the name "Null-Safe".PS: This work account ONLY for accessing object's properties and methods. It does not account for non-existant array items which is a seperate problem that can be addressed by introducing the null coalescing operator. Another feature that I'm currently working on as well 💯