Skip to content
Closed
Changes from all commits
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
6 changes: 5 additions & 1 deletion shopify/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ const isSelectedFilter = (filterValue: FilterValue, url: URL) => {

export const toFilter = (filter: FilterShopify, url: URL): Filter => {
if (!filter.type.includes("RANGE")) {
const isColorFilter = filter.id === "filter.v.t.shopify.color-pattern";

return {
"@type": "FilterToggle",
label: filter.label,
Expand All @@ -330,7 +332,9 @@ export const toFilter = (filter: FilterShopify, url: URL): Filter => {
return {
quantity: value.count,
label: value.label,
value: value.label,
value: !isColorFilter
? value.label
: JSON.parse(value.input).taxonomyMetafield.value,
Comment on lines +335 to +337

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Avoid unguarded parsing of color-filter input.

JSON.parse(value.input).taxonomyMetafield.value throws when an option has malformed JSON or a missing taxonomyMetafield, causing toFilter—and therefore the ProductListingPage filter mapping—to fail. Reuse getFilterValue(value) or add equivalent guarded parsing with a label fallback.

Proposed fix
-          value: !isColorFilter
-            ? value.label
-            : JSON.parse(value.input).taxonomyMetafield.value,
+          value: !isColorFilter ? value.label : getFilterValue(value),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
value: !isColorFilter
? value.label
: JSON.parse(value.input).taxonomyMetafield.value,
value: !isColorFilter ? value.label : getFilterValue(value),
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@shopify/utils/transform.ts` around lines 335 - 337, Update the color-filter
branch in the filter value mapping to use the existing getFilterValue(value)
helper, or equivalent guarded parsing, instead of directly accessing
JSON.parse(value.input).taxonomyMetafield.value. Preserve the label fallback for
malformed JSON or missing taxonomyMetafield so toFilter continues returning a
valid filter.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1: Unsafe JSON.parse(value.input) without error handling. If value.input contains malformed JSON — or if the parsed result has a null/undefined taxonomyMetafield — this line will throw a runtime exception that crashes the entire toFilter function, breaking the collection page for any shopify category with color-pattern filters.

Notice that the existing getFilterValue function in the same file (line ~370) follows a robust try/catch pattern for the exact same operation, including null-safe property traversal. This new code diverges from that established safety pattern.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At shopify/utils/transform.ts, line 337:

<comment>Unsafe `JSON.parse(value.input)` without error handling. If `value.input` contains malformed JSON — or if the parsed result has a null/undefined `taxonomyMetafield` — this line will throw a runtime exception that crashes the entire `toFilter` function, breaking the collection page for any shopify category with color-pattern filters.

Notice that the existing `getFilterValue` function in the same file (line ~370) follows a robust try/catch pattern for the exact same operation, including null-safe property traversal. This new code diverges from that established safety pattern.</comment>

<file context>
@@ -330,7 +332,9 @@ export const toFilter = (filter: FilterShopify, url: URL): Filter => {
-          value: value.label,
+          value: !isColorFilter
+            ? value.label
+            : JSON.parse(value.input).taxonomyMetafield.value,
           selected: isSelectedFilter(value, url),
           url: filtersURL(filter, value, url),
</file context>

selected: isSelectedFilter(value, url),
url: filtersURL(filter, value, url),
};
Expand Down
Loading