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

[utils] Fix isPlainObject to work across realm #39981

Merged
merged 5 commits into from
Jan 8, 2024

Conversation

brijeshb42
Copy link
Contributor

@brijeshb42 brijeshb42 commented Nov 24, 2023

This update adds support for checking plain object in other runtimes as well (ie vercel edge-runtime)

The implementation has been borrowed from https://github.com/sindresorhus/is-plain-obj to support our bare-minimum use-case.

Other simpler way would be to also add check for edge runtime but it will only cover Vercel's runtime and will be a short-term fix.

Closes #23739
Closes #39338
Closes #38177

@brijeshb42 brijeshb42 added the package: utils (private) Specific to the private @mui/utils package label Nov 24, 2023
@brijeshb42 brijeshb42 requested a review from a team November 24, 2023 13:27
@mui-bot
Copy link

mui-bot commented Nov 24, 2023

Netlify deploy preview

https://deploy-preview-39981--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against e5d68d9

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

@brijeshb42 I need more context. Is this the only function that does not work with edge runtime? How do I test locally that the change is not working?

@brijeshb42
Copy link
Contributor Author

@siriwatknp You can use a local next.js project with edge runtime in dev mode. I followed the instructions mentioned #39338. This is the repo.
In edge runtime, obj.constructor is not equal to Object. That was the main reason createTheme was just returning the theme created from @mui/system's createTheme.
createTheme (from @mui/material) internally uses deepmerge which in turn has the check isPlainObject which will always return false in case of edge runtime. This will not merge the two objects passed to deepmerge which will then just return the first object passed to it.

This was resulting in critical tokens being undefined and throwing errors.

@oliviertassinari
Copy link
Member

Does it relate to #23739 (comment)?

@oliviertassinari oliviertassinari added the PR: needs test The pull request can't be merged label Nov 27, 2023
@brijeshb42
Copy link
Contributor Author

brijeshb42 commented Nov 27, 2023

Yes. Seems root cause is same for both the issues.

Edit: The core issue is similar but in this particular, it's not about checking objects across realms. Instead, here, the realm itself is different.

@oliviertassinari
Copy link
Member

oliviertassinari commented Nov 27, 2023

Ok, then I think to bring the regression test in, and to consider if the implementation can't be simplified to improve performance. I suspect lodash tests for cases we don't need to support.

@oliviertassinari oliviertassinari added the bug 🐛 Something doesn't work label Nov 27, 2023
@oliviertassinari oliviertassinari changed the title [utils] Update isPlainObject implementation [utils] Fix isPlainObject sur work across realm Nov 27, 2023
@oliviertassinari oliviertassinari changed the title [utils] Fix isPlainObject sur work across realm [utils] Fix isPlainObject to work across realm Nov 27, 2023
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 5, 2023
@juniorbotelho
Copy link

When will this pull request be merged into the master branch?

@oliviertassinari oliviertassinari removed the PR: needs test The pull request can't be merged label Jan 7, 2024
This update adds support for checking plain object in non-v8/standard
runtimes as well (ie vercel edge-runtime)
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 7, 2024
@oliviertassinari
Copy link
Member

I rebased on HEAD.

Comment on lines 7 to 16
deepmerge({}, JSON.parse('{ "myProperty": "a", "__proto__" : { "isAdmin" : true } }'), {
clone: false,
});
const result = deepmerge(
{},
JSON.parse('{ "myProperty": "a", "__proto__" : { "isAdmin" : true } }'),
{
clone: false,
},
);

expect(result).not.to.have.property('isAdmin');
Copy link
Member

Choose a reason for hiding this comment

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

This change is missing the point of the test. Reverted in fd12e42.

@oliviertassinari
Copy link
Member

I pushed a new commit to remove vm-browserify. It feels like we don't need to run the test in a new iframe, that the node vm module is enough, keeping the stack simpler.

@brijeshb42 brijeshb42 merged commit 841c7e8 into mui:master Jan 8, 2024
19 checks passed
@brijeshb42 brijeshb42 deleted the fix/isPlainObject branch January 8, 2024 04:48
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work nextjs package: utils (private) Specific to the private @mui/utils package
Projects
None yet
5 participants