Skip to content

Commit c5a6573

Browse files
authored
fix(product, utils): handle metadata key deletion on product update (medusajs#12478)
1 parent 4e49ceb commit c5a6573

File tree

5 files changed

+18
-54
lines changed

5 files changed

+18
-54
lines changed

.changeset/tall-beans-shave.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/product": patch
3+
"@medusajs/utils": patch
4+
---
5+
6+
fix(product, utils): handle metadata key deletion on product update

integration-tests/http/__tests__/product/admin/product.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ medusaIntegrationTestRunner({
19121912

19131913
const payload = {
19141914
metadata: {
1915-
"test-key": "",
1915+
"test-key": "", // item is deleted by setting to empty string
19161916
"test-key-2": null,
19171917
},
19181918
}
@@ -1924,13 +1924,11 @@ medusaIntegrationTestRunner({
19241924
)
19251925

19261926
expect(response.status).toEqual(200)
1927-
expect(response.data.product.metadata).toEqual(
1928-
// BREAKING: Metadata updates are all-or-nothing in v2
1929-
{
1930-
"test-key": "",
1931-
"test-key-2": null,
1932-
}
1933-
)
1927+
expect(response.data.product.metadata).toEqual({
1928+
// "test-key" is deleted
1929+
"test-key-2": null, // updated
1930+
"test-key-3": "test-value-3", // preserved
1931+
})
19341932
})
19351933

19361934
it("updates products sales channels", async () => {

packages/core/utils/src/common/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export * from "./resolve-exports"
7272
export * from "./rules"
7373
export * from "./selector-constraints-to-string"
7474
export * from "./serialize-error"
75-
export * from "./set-metadata"
7675
export * from "./simple-hash"
7776
export * from "./string-to-select-relation-object"
7877
export * from "./stringify-circular"

packages/core/utils/src/common/set-metadata.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

packages/modules/product/src/repositories/product.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
buildQuery,
77
DALUtils,
88
MedusaError,
9+
isPresent,
10+
mergeMetadata,
911
} from "@medusajs/framework/utils"
1012
import { SqlEntityManager, wrap } from "@mikro-orm/postgresql"
1113

@@ -148,6 +150,10 @@ export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
148150
)
149151
}
150152

153+
if (isPresent(productToUpdate.metadata)) {
154+
productToUpdate.metadata = mergeMetadata(product.metadata ?? {}, productToUpdate.metadata)
155+
}
156+
151157
wrappedProduct.assign(productToUpdate)
152158
}
153159

0 commit comments

Comments
 (0)