Skip to content
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

Fix menu item deletion #15500

Merged
merged 7 commits into from
Mar 21, 2024
Merged

Fix menu item deletion #15500

merged 7 commits into from
Mar 21, 2024

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Mar 13, 2024

Fixes #15497

/cc @sarahelsaig

@hyzx86
Copy link
Contributor

hyzx86 commented Mar 13, 2024

A search for the keyword 'Content.Remove' throughout the solution reveals many similar operations.

I guess they're broken, too

image

@Piedone
Copy link
Member

Piedone commented Mar 13, 2024

@sarahelsaig @MikeAlhayek could you please check?

@hyzx86
Copy link
Contributor

hyzx86 commented Mar 13, 2024

It is best to find all references to ContentElement.Content and make sure they are used correctly.

@@ -270,16 +270,18 @@ public async Task<IActionResult> Delete(string menuContentItemId, string menuIte
return NotFound();
}

var menuContentAsJson = (JsonObject)menu.Content;
Copy link
Contributor

@hyzx86 hyzx86 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ContentElement.Content maybe is a instance of JsonDynamicObject

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, it is
image

Copy link
Contributor

@sarahelsaig sarahelsaig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, it works now. I had some notes.

Comment on lines 283 to 284
var menuItems = menuContentAsJson[nameof(MenuItemsListPart)]["MenuItems"] as JsonArray;
menuItems.Remove(menuItem);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the nitpick, but why did you use as JsonArray here? Is it possible that this won't be here or won't be the right type? Because right now it will just cause an NRE on the next line in that scenario. I suggest either doing null-forgiving on the Remove as well (menuItems?.Remove(menuItem);) or throwing an explicit exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I may have another problem

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sarahelsaig updated , at #15509

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the nitpick, but why did you use as JsonArray here? Is it possible that this won't be here or won't be the right type? Because right now it will just cause an NRE on the next line in that scenario. I suggest either doing null-forgiving on the Remove as well (menuItems?.Remove(menuItem);) or throwing an explicit exception.

Because there was a logical error in the last change here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate?

Copy link
Contributor

@hyzx86 hyzx86 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my English is not good. You can debug the original code, which attempts to remove a menuId directly from a jsonObject

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the nitpick, but why did you use as JsonArray here? Is it possible that this won't be here or won't be the right type? Because right now it will just cause an NRE on the next line in that scenario. I suggest either doing null-forgiving on the Remove as well (menuItems?.Remove(menuItem);) or throwing an explicit exception.

The MenuItems is always an array

Copy link
Contributor

@sarahelsaig sarahelsaig Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MenuItems is always an array

Thanks for the clarification.

You can debug the original code, which attempts to remove a menuId directly from a jsonObject

I've tried understanding JsonDynamicObject. It's the same as the inner _jsonObject, except it also has a _dictionary. This _dictionary field seems like a cache to me. So this could be fixed by emptying it after the json object is updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyzx86 I've fixed it using the additional "Remove" methods from your branch. Please check out my PR (hyzx86#1003).

Copy link
Contributor

@hyzx86 hyzx86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does fix the problem, let's merge it, and if there are other problems, let's continue in other PR

@hishamco
Copy link
Member Author

@sarahelsaig could you please approve this PR if you don't have any objection

@hishamco hishamco merged commit 28bd184 into main Mar 21, 2024
5 checks passed
@hishamco hishamco deleted the hishamco/menu-item branch March 21, 2024 16:47
@hishamco
Copy link
Member Author

@sarahelsaig @hyzx86 can we continue merging related PRs if they are ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't delete menu items on the latest preview
4 participants