Skip to content

Instantly share code, notes, and snippets.

@trliner
Last active April 20, 2018 04:26
Show Gist options
  • Save trliner/8e10e326095437618a6dce132299d0ce to your computer and use it in GitHub Desktop.
Save trliner/8e10e326095437618a6dce132299d0ce to your computer and use it in GitHub Desktop.
Test case for expected behavior of to_json's :only option.
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" }
gem "rails", github: "rails/rails"
end
require "active_support"
require "active_support/core_ext/object/blank"
require "minitest/autorun"
class BugTest < Minitest::Test
def test_nested_hash_json_key_filtering
nested_hash = { hash: { key: 'value' }, array: [{ key: 'value' }] }
assert_equal nested_hash.as_json(only: [:hash]), { hash: { key: 'value' } }
assert_equal nested_hash.as_json(only: [:array]), { array: [{ key: 'value' }] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment