Repository navigation
Description
Summary
Ruflet::Event runs JSON.parse on every String event payload. The client sends a TextField's typed text, a Dropdown's option key and a RadioGroup's radio value as plain MessagePack strings. When that text happens to be valid JSON, the handler gets a different value with a different type:
| typed or picked | e.data / e.value today |
|---|---|
"123" |
123 (Integer) |
"2.50" |
2.5 (Float) |
" 42" |
42 |
"1e3" |
1000.0 |
"true" |
true |
"null" |
nil |
"[1]" |
[1] |
"\"q\"" |
"q" |
"007" |
"007" (stays a String) |
For change events, Page#apply_event_value_to_control then writes the decoded value over control.props["value"]. That replaces the String that the client's update_control had just stored.
Minimal repro
The repro sends what textfield.dart, material_dropdown.dart and radio_group.dart send: update_control {value: s}, then the event with s as data.
require "ruflet"
page = Ruflet::Page.new(session_id: "s1", client_details: { "route" => "/" }, sender: ->(*) {})
seen = []
field = Ruflet.text_field(on_change: ->(e) { seen << [e.data, e.control.props["value"]] })
group = Ruflet.radio_group(value: "10", on_change: ->(e) { seen << [e.data, e.control.props["value"]] },
content: Ruflet.column(controls: [Ruflet.radio(value: "10"), Ruflet.radio(value: "20")]))
page.add(Ruflet.column(controls: [field, group]))
page.apply_client_update(field.wire_id, { "value" => "2.50" })
page.dispatch_event(target: field.wire_id, name: "change", data: "2.50")
page.apply_client_update(group.wire_id, { "value" => "20" })
page.dispatch_event(target: group.wire_id, name: "change", data: "20")
p seen
# today: [[2.5, 2.5], [20, 20]]
# expected: [["2.50", "2.50"], ["20", "20"]]It behaves the same through Ruflet::Server#handle_message (ruflet run and the embedded runtime) and through a Ruflet::ConnectionProtocol host (ruflet_rails). Gem 0.0.23 gives the same results.
What this breaks
- A handler that calls String methods raises. For example, a Form
validator: ->(v) { v.strip }raisesNoMethodErrorfor Float after the user types2.50. - A Dropdown
on_selectwith string keys"1"/"2"getse.data == 2, soe.data == "2"is false. - A RadioGroup with values
"10"/"20"ends up withprops["value"] == 20. - A later re-render or re-add sends the decoded value back to the field, so
"2.50"is shown as2.5. - An ERB-to-native form (
Ruflet::Rails.erb_to_native) posts{"zip"=>12345, "price"=>19.9, "note"=>""}after the user types12345,19.90andnull. Rails params should be the strings that were typed. - The embedded VM's JSON (
ruby_runtime/vm/stdlib/json.rb) also decodes"007"as7, so the VM and CRuby disagree.
Expected
Handlers get the string the user typed or picked, and control.props["value"] keeps what update_control stored. The engine passes these values as Dart Strings and never JSON-encodes event data:
textfield.dartonChanged/onFieldSubmittedmaterial_dropdown.dartonSelectedradio_group.dartonChangedmaterial_search_bar.dart
The docs (events_and_state.md) promise only that "JSON strings become Ruby arrays or hashes".
Apps that declare Integer option keys or radio values get e.data == 2 today. The renderer stringifies the key and the decode happens to turn it back into a number. A fix must keep that working.
Location
packages/ruflet_core/lib/ruflet_ui/ruflet/event.rb:Event#parse_datapackages/ruflet_core/lib/ruflet_ui/ruflet/page.rb:Page#dispatch_event, which callsapply_event_value_to_controlpackages/ruflet_core/lib/ruflet_ui/ruflet/ui/controls/shared/form_control.rb:form_eventrebuilds the event fromraw_data- Consumed by
packages/ruflet_rails/lib/ruflet/rails/html_dsl/transformer.rb(event_value, thenfield_changed)
Activity