I have this following Controller spec example passing in my API-only application based on Rails 4.2.0 and Ruby 2.2.1
let!(:params) { { user_token: user_token } }
context "- and optional address and contact details params value are received as a nil values -" do
it "doesn't set the address and contact details and responds with 201 success", check: true do
params.merge!(
address_street: nil, address_other: nil, city: nil, state: nil,
zip_code: nil, phone: nil)
post :create, params
expect(response).to have_http_status(201)
saved_client_id = json_response["id"]
saved_client = Client.find_by(id: saved_client_id)
expect(saved_client.address_street).to be_nil
expect(saved_client.address_other).to be_nil
expect(saved_client.city).to be_nil
expect(saved_client.state).to be_nil
expect(saved_client.zip_code).to be_nil
expect(saved_client.phone).to be_nil
end
end
However evaluating my application against Rails 5 (edge version) and Ruby 2.2.3 the same spec fails with following error:
1) Api::V1::ClientsController POST #create when receives valid client details - and optional address and contact details params value are received as nil values - doesn't set the address and contact details and responds with 201 success
Failure/Error: expect(saved_client.address_street).to be_nil
expected: nil
got: ""
# ./spec/controllers/api/v1/clients_controller_spec.rb:352:in `block (5 levels) in <top (required)>'
# ./spec/rails_helper.rb:61:in `block (3 levels) in <top (required)>'
# /home/jignesh/.rvm/gems/ruby-2.2.3@myapp-on-rails-5/gems/database_cleaner-1.5.1/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /home/jignesh/.rvm/gems/ruby-2.2.3@myapp-on-rails-5/gems/database_cleaner-1.5.1/lib/database_cleaner/base.rb:92:in `cleaning'
# /home/jignesh/.rvm/gems/ruby-2.2.3@myapp-on-rails-5/gems/database_cleaner-1.5.1/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# /home/jignesh/.rvm/gems/ruby-2.2.3@myapp-on-rails-5/gems/database_cleaner-1.5.1/lib/database_cleaner/configuration.rb:87:in `call'
# /home/jignesh/.rvm/gems/ruby-2.2.3@myapp-on-rails-5/gems/database_cleaner-1.5.1/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/rails_helper.rb:60:in `block (2 levels) in <top (required)>'
I did inspected the Rails source code at few points and found that the nil values are transformed to blank values before reaching the controller's targeted action logic.
This changed behavior is setting attributes to empty strings, when they are expected to be nil.
In my app's Gemfile (for using Rails 5) I have specified Rails using following code:
gem 'rails', git: 'https://github.com/rails/rails.git'
gem 'rack', :git => 'https://github.com/rack/rack.git'
gem 'arel', :git => 'https://github.com/rails/arel.git'
and in Gemfile.lock following can be seen (Gem and Dependencies portions truncated to cut it short):
GIT
remote: git://github.com/capistrano/rbenv.git
revision: 6f1216cfe0a6b4ac23ca4eaf8acf012e8165d247
specs:
capistrano-rbenv (2.0.3)
capistrano (~> 3.1)
sshkit (~> 1.3)
GIT
remote: https://github.com/rack/rack.git
revision: c393176b0edf3e5d06cabbb6eb9d9c7a07b2afa7
specs:
rack (2.0.0.alpha)
json
GIT
remote: https://github.com/rails/arel.git
revision: 3c429c5d86e9e2201c2a35d934ca6a8911c18e69
specs:
arel (7.0.0.alpha)
GIT
remote: https://github.com/rails/rails.git
revision: 58df2f4b4abcce0b698c2540da215a565c24cbc9
specs:
actionmailer (5.0.0.alpha)
actionpack (= 5.0.0.alpha)
actionview (= 5.0.0.alpha)
activejob (= 5.0.0.alpha)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (5.0.0.alpha)
actionview (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
rack (~> 2.x)
rack-test (~> 0.6.3)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.0.alpha)
activesupport (= 5.0.0.alpha)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.0.alpha)
activesupport (= 5.0.0.alpha)
globalid (>= 0.3.0)
activemodel (5.0.0.alpha)
activesupport (= 5.0.0.alpha)
builder (~> 3.1)
activerecord (5.0.0.alpha)
activemodel (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
arel (= 7.0.0.alpha)
activesupport (5.0.0.alpha)
concurrent-ruby (~> 1.0)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
method_source
minitest (~> 5.1)
tzinfo (~> 1.1)
rails (5.0.0.alpha)
actionmailer (= 5.0.0.alpha)
actionpack (= 5.0.0.alpha)
actionview (= 5.0.0.alpha)
activejob (= 5.0.0.alpha)
activemodel (= 5.0.0.alpha)
activerecord (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0.alpha)
sprockets-rails (>= 2.0.0)
railties (5.0.0.alpha)
actionpack (= 5.0.0.alpha)
activesupport (= 5.0.0.alpha)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
...
....
Can anybody please let me know what changed caused this? I guess it has something to do with change in Rails 5 or the latest Rack. Is this some-kind of bug which shall be fixed in the final release version or this is deliberate change.