activegraph icon indicating copy to clipboard operation
activegraph copied to clipboard

update_attributes coerces strings into ASCII-8BIT encoding.

Open phyllisstein opened this issue 5 years ago • 0 comments

:wave: Hey folks! I wanted to flag a kind of gnarly issue with update_attributes: it seems to temporarily reencode strings as Encoding::ASCII_8BIT, until the updated model is refreshed. The strings seem to go into the database as UTF-8, and the next time the model is queried they come out that way too, but while they're in memory they look to Ruby like ASCII.

I stumbled over this because graphql-ruby started throwing 500s with encoding errors when trying to serialize Neo4j.rb models for returning to my frontend. But it's pretty straightforward to reproduce in a REPL; see below.

Thanks in advance for looking into this, and for any insight you can offer!

Additional information which could be helpful if relevant to your issue:

Code example (inline, gist, or repo)

Loading development environment (Rails 5.2.0)
[3] pry(main)> params = { title: 'Je suis string' }
=> {:title=>"Je suis string"}
[4] pry(main)> params[:title].encoding
=> #<Encoding:UTF-8>
[5] pry(main)> p = Content::Post.first
 BOLT REQUEST: 1ms
 BOLT REQUEST: 5ms
 BOLT REQUEST: 1ms
 BOLT REQUEST: 1ms
 BOLT REQUEST: 5ms
 BOLT REQUEST: 1ms
 BOLT REQUEST: 1ms
 CYPHER
  MATCH (n:`Content::Post`)
  RETURN n
  ORDER BY n.uuid
  LIMIT {limit_1} | {:limit_1=>1}
 BOLT REQUEST: 3ms
 BOLT REQUEST: 1ms
=> #<Content::Post uuid: "13d5c3ff-c8f3-47b0-88ea-23604bb68abf", created_at: Fri, 27 Jul 2018 07:56:59 +0000, excerpt: "She's a girl, I need to teach her how to be a woman. Within her lies a queen. Let me out that queen. ", hero_src: {"id"=>"content/post/13d5c3ff-c8f3-47b0-88ea-23604bb68abf/hero/68104cdd3d1363db0237a2178f4e1fe8.jpg", "storage"=>"store", "metadata"=>{"filename"=>"image_processing20180727-61670-1v1h0sp20180727-61670-11svlym.jpg", "size"=>140169, "mime_type"=>"image/jpeg", "host"=>"https://ass.ignota.fyi", "width"=>1440, "height"=>900}}, published: true, published_at: Sat, 28 Jul 2018 06:52:08 +0000, slug: "69b87a67-sample-s-demo", title: "Sample’s Demo", updated_at: Tue, 31 Jul 2018 09:09:46 +0000>
[6] pry(main)> p.update_attributes(params)
 BOLT REQUEST: 1ms
 CYPHER
  MATCH (result_contentpost:`Content::Post`)
  WHERE (result_contentpost.slug = {result_contentpost_slug})
  RETURN ID(result_contentpost) AS proof_of_life LIMIT 1 | {:result_contentpost_slug=>"7e9e5cb9-je-suis-string"}
 BOLT REQUEST: 5ms
 BOLT REQUEST: 6ms
 BOLT REQUEST: 6ms
 Content::Post#author
  MATCH (content_post896)
  WHERE (ID(content_post896) = {ID_content_post896})
  MATCH (content_post896)-[rel1:`AUTHORED_BY`]->(result_author:`Users::Author`)
  RETURN result_author | {:ID_content_post896=>896}
 BOLT REQUEST: 3ms
 CYPHER
  MATCH (n)
  WHERE (ID(n) = {ID_n})
  SET n.`updated_at` = {setter_n_updated_at},
    n.`slug` = {setter_n_slug},
    n.`title` = {setter_n_title} | {:ID_n=>896, :setter_n_updated_at=>1533028347, :setter_n_slug=>"7e9e5cb9-je-suis-string", :setter_n_title=>"Je suis string"}
 BOLT REQUEST: 2ms
 BOLT REQUEST: 11ms
=> true
[7] pry(main)> params[:title].encoding
=> #<Encoding:ASCII-8BIT>
[8] pry(main)> p.title.encoding
=> #<Encoding:ASCII-8BIT>
[9] pry(main)> p.reload
 BOLT REQUEST: 1ms
 CYPHER
  MATCH (n)
  WHERE (ID(n) = {ID_n})
  RETURN n | {:ID_n=>896}
 BOLT REQUEST: 3ms
 BOLT REQUEST: 1ms
=> #<Content::Post uuid: "13d5c3ff-c8f3-47b0-88ea-23604bb68abf", created_at: Fri, 27 Jul 2018 07:56:59 +0000, excerpt: "She's a girl, I need to teach her how to be a woman. Within her lies a queen. Let me out that queen. ", hero_src: {"id"=>"content/post/13d5c3ff-c8f3-47b0-88ea-23604bb68abf/hero/68104cdd3d1363db0237a2178f4e1fe8.jpg", "storage"=>"store", "metadata"=>{"filename"=>"image_processing20180727-61670-1v1h0sp20180727-61670-11svlym.jpg", "size"=>140169, "mime_type"=>"image/jpeg", "host"=>"https://ass.ignota.fyi", "width"=>1440, "height"=>900}}, published: true, published_at: Sat, 28 Jul 2018 06:52:08 +0000, slug: "7e9e5cb9-je-suis-string", title: "Je suis string", updated_at: Tue, 31 Jul 2018 09:12:27 +0000>
[10] pry(main)> p.title.encoding
=> #<Encoding:UTF-8>

Runtime information:

Neo4j database version: 3.3.4 neo4j gem version: 9.3.0 neo4j-core gem version: 8.1.4

phyllisstein avatar Jul 31 '18 09:07 phyllisstein