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(core): Keep order of nested relations during hydration (#2864) #2865

Conversation

jnugh
Copy link
Contributor

@jnugh jnugh commented May 23, 2024

Description

Relations could get out of sync during entity hydration. See #2864 for a detailed description and example.

I moved mergeDeep out of the service to make testing easier. I think adding an e2e Test for this does not make sense as it would be very hard to control the result order of the hydration database query. So I decided to unit test mergeDeep instead.

The only actual change is:

                if (Array.isArray(value)) {
                    (a as any)[key] = value.map((v, index) =>
                        this.mergeDeep(a?.[key]?.[index], b[key][index]),
                    );
                } else if (isObject(value)) {
                    (a as any)[key] = this.mergeDeep(a?.[key], b[key]);
                }

changed to:

            if (Array.isArray(value) || isObject(value)) {
                (a as any)[key] = mergeDeep(a?.[key], b[key]);
            }

I don't see a case where calling mergeDeep would not work because mergeDeep still executes the same logic inside the recursion by iterating using for (const [key, value] of Object.entries(b)) and assigning the values using (a as any)[key] = b[key];, event if a and b do not contain entities.

Breaking changes

None

Checklist

📌 Always:

  • I have set a clear title
  • My PR is small and contains a single feature
  • I have checked my own PR

👍 Most of the time:

  • I have added or updated test cases
  • I have updated the README if needed

Copy link

netlify bot commented May 23, 2024

Deploy Preview for effervescent-donut-4977b2 failed.

Name Link
🔨 Latest commit e98dc61
🔍 Latest deploy log https://app.netlify.com/sites/effervescent-donut-4977b2/deploys/664f00834341200008b3d11e

for (const [key, value] of Object.entries(b)) {
if (Object.getOwnPropertyDescriptor(b, key)?.writable) {
if (Array.isArray(value) || isObject(value)) {
(a as any)[key] = mergeDeep(a?.[key], b[key]);

Check warning

Code scanning / CodeQL

Prototype-polluting function Medium

Properties are copied from
b
to
here
without guarding against prototype pollution.
if (Array.isArray(value) || isObject(value)) {
(a as any)[key] = mergeDeep(a?.[key], b[key]);
} else {
(a as any)[key] = b[key];

Check warning

Code scanning / CodeQL

Prototype-polluting function Medium

Properties are copied from
b
to
here
without guarding against prototype pollution.
@michaelbromley michaelbromley merged commit b325a83 into vendure-ecommerce:master May 31, 2024
12 of 16 checks passed
@michaelbromley
Copy link
Member

Thanks for the detailed write-up & fix! 👍

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

Successfully merging this pull request may close these issues.

2 participants