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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

// Look for the target menu item in the hierarchy.
var menuItem = FindMenuItem((JsonObject)menu.Content, menuItemId);
var menuItem = FindMenuItem(menuContentAsJson, menuItemId);

// Couldn't find targeted menu item.
if (menuItem == null)
{
return NotFound();
}

menu.Content.Remove(menuItemId);
var menuItems = menuContentAsJson[nameof(MenuItemsListPart)]["MenuItems"] as JsonArray;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
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).


await _contentManager.SaveDraftAsync(menu);

Expand Down
Loading