fix(shopify): update color filter handling in toFilter function - #1650
fix(shopify): update color filter handling in toFilter function#1650luanargolodev wants to merge 1 commit into
Conversation
Tagging OptionsShould a new tag be published when this PR is merged?
|
📝 WalkthroughWalkthrough
ChangesColor filter transformation
Estimated code review effort: 2 (Simple) | ~5 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with 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.
Inline comments:
In `@shopify/utils/transform.ts`:
- Around line 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.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 132bd84a-c18b-41dc-bf60-5a1db969617f
📒 Files selected for processing (1)
shopify/utils/transform.ts
| value: !isColorFilter | ||
| ? value.label | ||
| : JSON.parse(value.input).taxonomyMetafield.value, |
There was a problem hiding this comment.
🩺 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.
| 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.
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="shopify/utils/transform.ts">
<violation number="1" location="shopify/utils/transform.ts:337">
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.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| value: value.label, | ||
| value: !isColorFilter | ||
| ? value.label | ||
| : JSON.parse(value.input).taxonomyMetafield.value, |
There was a problem hiding this comment.
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>
What is this Contribution About?
Please provide a brief description of the changes or enhancements you are proposing in this pull request.
Issue Link
Please link to the relevant issue that this pull request addresses:
Loom Video
Demonstration Link
Summary by cubic
Fixes color filter handling in
toFilterby mappingfilter.v.t.shopify.color-patternvalues totaxonomyMetafield.valuefromvalue.inputinstead of the label. This ensures accurate filter values and URLs for color selections.Written for commit f3e024f. Summary will update on new commits.
Summary by CodeRabbit