Fix decode/1 crash on non-JSON strings from search input#133
Open
mcass19 wants to merge 2 commits intomaxmarcon:mainfrom
Open
Fix decode/1 crash on non-JSON strings from search input#133mcass19 wants to merge 2 commits intomaxmarcon:mainfrom
mcass19 wants to merge 2 commits intomaxmarcon:mainfrom
Conversation
Covers the case where raw search text (e.g. "Glon") is passed to decode/1 before a selection is made. Currently crashes with Jason.DecodeError.
Rescue decode errors and return the raw value as-is, mirroring how encode/1 already passes simple types through without encoding.
Owner
|
Thanks for this. I will look into this as soon as I have some time |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi, it's me again :). Keep the nice work and library!!!
Problem
LiveSelect.decode/1 crashes with a Jason.DecodeError when the value passed to it is a plain string that isn't valid JSON.
This happens when a form with phx-change fires while the user is typing in the LiveSelect text input but hasn't made a selection yet. The raw search text (e.g., "Glon") arrives as the field value, and decode/1 crashes because unquoted text is not valid JSON.
How to reproduce
params = update_in(params, ~w(my_form my_field), &LiveSelect.decode/1)The raw search text arrives as the field value, and decode/1 crashes.
Root cause
There is an asymmetry between encode and decode.
In component.ex, encode/1 has a fast path that skips JSON encoding for simple types. But decode/1 in live_select.ex always calls json.decode! on any non-nil, non-empty value.
This means values that were never JSON-encoded (simple strings, raw text from typing) get json.decode! called on them, which crashes.
Fix
Rescue decode errors and return the raw value as-is, mirroring how encode/1 passes simple types through without encoding.
This way, valid JSON is decoded as before, and non-JSON strings pass through to the changeset which handles validation normally.
There's a test added that fails without the fix.