-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Unpublishing and editing content item with an uninitialized ContentPickerField breaks (Lombiq Technologies: OCORE-92) #11528
Conversation
@@ -0,0 +1,36 @@ | |||
using System.Collections.Generic; | |||
using Newtonsoft.Json.Linq; |
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.
Just some thought here. Could we do the same but without using Newtonsoft.Json and replace it with System.Text.Json while we are at it?
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.
The ContentItem.Content
points to OrchardCore.ContentManagement.ContentElement.Data
which is a Newtonsoft JObject
. This class doesn't do any serializing, just access this existing property so trying to use System.Text.Json here would require conversion. Better table it until ContentElement
itself is ported to STJ, I think.
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.
Ok, forget it for now then.
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.
Will Linq be concern here for allocation and performance?
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.
We pass on the enumerables without using an intermediary list like before, so that should improve allocation. I'm not sure about the performance, but we don't do anything complex so it should be fine. Also these providers are only called when you update a content item's field; that's relatively rare so you shouldn't be concerned.
Why not just replacing Hmm, maybe from the beginning would have been better to use the ContentElement extensions already using So for now can you try the above suggestion and see if it fixes the issue. |
where TField : ContentField => | ||
contentItem.Content[fieldDefinition.PartDefinition.Name] is JObject jPart && | ||
jPart[fieldDefinition.Name] is JObject jField && | ||
jField.ToObject<TField>() is { } field |
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.
does is { }
mean != null
. If so then != null
is easier to comprehend.
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.
It's a combined operation for pattern matching introduced in C# 8. It does != null
check and assignment to the field
variable. So it makes the code more compact and lowers the cognitive complexity (once you are used to it anyway). Back in C# 7 we'd use jField.ToObject<TField>() is var field
for the same, but starting C# 8 this empty object pattern is the norm.
If I replaced this with != null
then it would look like this:
jField.ToObject<TField>() != null
? jField.ToObject<TField>()
: null;
I think that's less efficient and visually noisier. Please comment if you want C# 8 style, C# 7 style, or the old fashioned null check and explicit assignment.
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.
and lowers the cognitive complexity (once you are used to it anyway)
Like pain, after a while you don't realize it's there, so pain isn't real actually.
We had a meeting today and nobody could explain what it does. Everyone could explain != null
though.
Please keep the old style, and add more lines if necessary for readability or prevent two calls to ToObject.
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.
It's not like pain, more like learning a new technique. First it's weird and awkward, but it you give it a chance you start seeing the utility in practice and it grows on you. Change is good.
(also we use this pattern matching in Lombiq everywhere)
But it's you guys' repo, so I abide by your decision and have rewrote the method.
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.
May be you can use jField is not null
it’s more readable
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.
Ok. I have already eliminated the null check here, but I applied your suggestion to the null check in the other method in this file.
However this is starting to get a bit confusing. Does the OC repo have a style guide? I couldn't find it. If not, do you have plans to make one?
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.
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 links to ASP.Net Core's engineering guidelines. If you can edit that, it would be good to add something about null checking conventions based on this discussion. If not, an OC-specific guide would still be valuable to keep a record of such decisions. Just a thought.
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.
No need to add documentation about this. We are just giving comments here based on our coding preferences because it is easier to read. Basically, adding documentation will not prevent this from happening so we give our advice on every pull request. Getting code merged into source code can take more time than we think at first. You will get used to it over time. Everyone has comments and suggestions, that is why pull requests exist.
Apart from the new c# |
Do I need to do anything else or is this ready to be merged? |
As said I would have just replaced the
Also, @sebastienros why not making the ContentElement
Or at least do a cast on the
But this PR LGTM so I will approve. Hmm, as suggested above, maybe still add a cast on the
Or directly use But yes all the above is not crucial as here we are updating items, but making the So as said I will still approve this PR ;) |
I agree with the |
If possible I'd prefer to keep using
Good point, I have applied the suggested cast.
If this comment gets two thumbs ups I will do it :) |
@DAud-IcI Okay, all good then for me, thanks for the change. |
Fixes #11527