Skip to content

String event data is JSON-decoded: typed text, dropdown keys and radio values change type ("123" becomes 123, "null" becomes nil) #96

Closed

Description

@AdamMusa

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 } raises NoMethodError for Float after the user types 2.50.
  • A Dropdown on_select with string keys "1"/"2" gets e.data == 2, so e.data == "2" is false.
  • A RadioGroup with values "10"/"20" ends up with props["value"] == 20.
  • A later re-render or re-add sends the decoded value back to the field, so "2.50" is shown as 2.5.
  • An ERB-to-native form (Ruflet::Rails.erb_to_native) posts {"zip"=>12345, "price"=>19.9, "note"=>""} after the user types 12345, 19.90 and null. Rails params should be the strings that were typed.
  • The embedded VM's JSON (ruby_runtime/vm/stdlib/json.rb) also decodes "007" as 7, 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.dart onChanged/onFieldSubmitted
  • material_dropdown.dart onSelected
  • radio_group.dart onChanged
  • material_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_data
  • packages/ruflet_core/lib/ruflet_ui/ruflet/page.rb: Page#dispatch_event, which calls apply_event_value_to_control
  • packages/ruflet_core/lib/ruflet_ui/ruflet/ui/controls/shared/form_control.rb: form_event rebuilds the event from raw_data
  • Consumed by packages/ruflet_rails/lib/ruflet/rails/html_dsl/transformer.rb (event_value, then field_changed)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions