yosiat / panko_serializer

High Performance JSON Serialization for ActiveRecord & Ruby Objects
https://panko.dev
MIT License
592 stars 36 forks source link

Issue with boolean types #29

Closed SnowyLeopard closed 6 years ago

SnowyLeopard commented 6 years ago

Hello,

I started using your serializer gem and am running into a weird issue.

Whenever the model attribute is of type boolean i'm getting a '/panko_serializer-0.4.0/lib/panko/serializer.rb:92: [BUG] Segmentation fault at 0x00000000000003' error and the entire ruby/rails process crashes.

Whenever I remove the boolean attribute from the serializer it is working fine. Also when I use a custom method in the serializer like the following example it actually works (in the example 'admin' is a boolean):

class UserSerializer < Panko::Serializer
  attributes :id, :email, :name, :admin

  def admin
    object.admin
  end
end

So when I remove the admin method in the above example it produces the error.

Is this a known issue or am I doing something wrong?

I'm calling the serializer with:

UserSerializer.new.serialize_to_json(User.first)

More info: Database; MySQL Rails: 5.0.6 Ruby: 2.5.1

With kind regards, Snowy Leopard

yosiat commented 6 years ago

Hi @SnowyLeopard !

Couldn't reproduce this segmentation fault.

Is admin method on User or this is column ?

Can you get this code to reproduce your segfault?

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.0.6"
  gem "panko_serializer"
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.boolean :admin
  end
end

class User < ActiveRecord::Base
end

class UserSerializer < Panko::Serializer
  attributes :admin

  def admin
    object.admin
  end
end

class BugTest < Minitest::Test
  def test_association_stuff
        User.create!(admin: false)
        puts UserSerializer.new.serialize_to_json(User.first)
  end
end
SnowyLeopard commented 6 years ago

Hello @yosiat,

I'm unable to reproduce the error with the above example, and to answer your question: Yes, 'admin' is a column on User.

I am however able to reproduce it using the following example:

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.0.6"
  gem "panko_serializer"
  gem "mysql2", '~> 0.4.0'
end

require "active_record"
require "minitest/autorun"
require "logger"

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection( adapter: "mysql2", database: "db_name", username: 'username', password: 'password', socket: '/var/run/mysqld/mysqld.sock')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.boolean :admin
  end
end

class User < ActiveRecord::Base
end

class UserSerializer < Panko::Serializer
  attributes :admin
end

class BugTest < Minitest::Test
  def test_association_stuff
        User.create!(admin: false)
        puts UserSerializer.new.serialize_to_json(User.first)
  end
end

Note that I switched to mysql and removed the admin method in the serializer. Make me believe it has something to do with mysql.

Hope this helps!

yosiat commented 6 years ago

@SnowyLeopard Thanks!

I actually didn't test the code with MySQL only Postgres & SQLite. Will look later today and update.

yosiat commented 6 years ago

@SnowyLeopard Found the bug see - https://github.com/yosiat/panko_serializer/commit/907b5da53f04b1414e6013465829ff8c64f3a3e4

Will release 0.4.1 version once CI is green

SnowyLeopard commented 6 years ago

Hey @yosiat!

Tested the github version in my app, can confirm this fixes the issue. Thanks!

regards, Snowy Leopard

yosiat commented 6 years ago

@SnowyLeopard released 0.4.2 with a bug fix