-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix run value converters that convert nulls for JSON columns #37984
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
adcc266
Fix run value converters that convert nulls for JSON columns
JoasE 8399c8d
Add tests
JoasE fe913b6
Fix RelationalJsonUtilities
JoasE b6cdfc5
Fix tests
JoasE 4fcee53
Clean
JoasE 84dae2f
Workaround 1
JoasE 7a2e95b
Merge branch 'main' of https://github.com/dotnet/efcore into fix/#37983
JoasE becc2ce
Add HandlesNulls to JsonValueReaderWriter
JoasE c7fada1
Clean
JoasE ab4749b
Rename to HandlesNullWrites
JoasE 1b592cf
Clean
JoasE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3188,6 +3188,56 @@ private Expression CreateReadJsonPropertyValueExpression( | |
| resultExpression = Convert(resultExpression, property.ClrType); | ||
| } | ||
|
|
||
| var converter = property.GetTypeMapping().Converter; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is copied over from above for |
||
| Expression nullExpression; | ||
| if (converter?.ConvertsNulls == true) | ||
| { | ||
| var typeMappingExpression = Call( | ||
| Convert( | ||
| _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant( | ||
| property, | ||
| LiftableConstantExpressionHelpers.BuildMemberAccessLambdaForProperty(property), | ||
| property.Name + "Property", | ||
| typeof(IPropertyBase)), | ||
| typeof(IReadOnlyProperty)), | ||
| PropertyGetTypeMappingMethod); | ||
|
|
||
| var converterExpression = (Expression)Property(typeMappingExpression, nameof(CoreTypeMapping.Converter)); | ||
|
|
||
| var converterType = converter.GetType(); | ||
| var typedConverterType = converterType.GetGenericTypeImplementations(typeof(ValueConverter<,>)).FirstOrDefault(); | ||
| if (typedConverterType != null) | ||
| { | ||
| if (converterExpression.Type != converter.GetType()) | ||
| { | ||
| converterExpression = Convert(converterExpression, converter.GetType()); | ||
| } | ||
|
|
||
| nullExpression = Invoke( | ||
| Property( | ||
| converterExpression, | ||
| nameof(ValueConverter<object, object>.ConvertFromProviderTyped)), | ||
| Default(converter.ProviderClrType)); | ||
| } | ||
| else | ||
| { | ||
| nullExpression = Invoke( | ||
| Property( | ||
| converterExpression, | ||
| nameof(ValueConverter.ConvertFromProvider)), | ||
| Default(typeof(object))); | ||
| } | ||
|
|
||
| if (nullExpression.Type != property.ClrType) | ||
| { | ||
| nullExpression = Convert(nullExpression, property.ClrType); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| nullExpression = Default(property.ClrType); | ||
| } | ||
|
|
||
| resultExpression = Condition( | ||
| Equal( | ||
| Property( | ||
|
|
@@ -3196,7 +3246,7 @@ private Expression CreateReadJsonPropertyValueExpression( | |
| Utf8JsonReaderManagerCurrentReaderField), | ||
| Utf8JsonReaderTokenTypeProperty), | ||
| Constant(JsonTokenType.Null)), | ||
| Default(property.ClrType), | ||
| nullExpression, | ||
| resultExpression); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
propertyValue can be null here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s actually intentional here, since this change allows propertyValue to be null if the converter has indicated it can handle null values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then you should change the signature of
ToJsonto allow nullsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndriySvyryd That's a good point! I had some reservations about that but didn't really give it much thought. I've looked at it more now and FWIG
JsonValueReaderWritercurrently doesn't allow null input by design, and I'm not sure changing that would make sense and/or would require a different setup all together.If I change JsonValueReaderWriter to allow null argument for object value, I can't just pass it to
ToJsonTyped. I would have to makeToJsonTypedvalue argument nullable aswell, but I don't think that would be desired. AFAIK The typedJsonValueReaderWriter<T>should generally not have to worry about null values. I've considered havingJsonConvertedValueReaderWriterinherit from non-typedJsonValueReaderWriterto solve this. Then I could add a null check and write in theJsonValueReaderWriter<T>.ToJsonimplementation and also theJsonConvertedValueReaderWriter.ToJsonimplementation. However, materialization works with the typedJsonValueReaderWriter<T>.FromJsonTyped, so theJsonConvertedValueReaderWriterwouldn't be usable in materialization. I think that if we want to makeToJsonaccept null and do the null write for the value, the only option that makes sense would be unsealingJsonValueReaderWriter<T>.ToJsonso that I can override it inJsonConvertedValueReaderWriter. However, I'm not sure that is desirable either.Some workarounds could be:
IJsonConvertedValueReaderWriterthat allows null input, then doing the ConvertsNulls check and null write thereIJsonConvertedValueReaderWriter.ValueConverterandICompositeJsonValueReaderWriter.InnerReaderWriterto circumventJsonConvertedValueReaderWriter.ToJson. Invoking theValueConvertermanually and checking if it converted to null (seems cumbersome)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note that I ran into this when working on:
https://github.com/dotnet/efcore/pull/38024/changes#diff-62e5c8b19c66714cf0310ceff8fb90312c039fb64dc9e7af31241e8956bb412dR103-R116
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this exposed the reason I find this suppression smelly: it's relying on implementation details. You could add
JsonValueReaderWriter.HandlesNulls, make the value nullable and throw if it's null andHandlesNullsisfalseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndriySvyryd That makes a lot of sense! I've added JsonValueReaderWriter.HandlesNulls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering now how this should (not) affect FromJson
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can have FromJson stay unaffected, but would recommend renaming to
HandlesNullWritesOtherwhise a similar signature change for FromJson would be needed, but materialization uses FromJsonTyped