Skip to content

Commit 2529343

Browse files
sjmogandyjeffries
authored andcommitted
91 allow includes strings (Netflix#93)
* add hash benchmarking to performance tests * Add missing attribute in README example * Disable GC before doing performance test * Enable oj to AM for fair benchmark test * add information on performance methodology * add oss metadata * Make an error that demonstrates [Issue * Simple RSpec test that fails with a non-empty string but passes with a non-empty symbol * To run the test, rspec spec/lib/object_serializer_spec.rb * Map includes to symbols if they are provided as strings * Includes would fail with an ArgumentError unless they were explicitly provided as symbols (see Netflix#97) * This is solved by mapping the strings to symbols in the ObjectSerializer initializer * No real impact on performance here
1 parent 2dae7bf commit 2529343

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

lib/fast_jsonapi/object_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def process_options(options)
8484
@meta = options[:meta]
8585

8686
if options[:include].present?
87-
@includes = options[:include].delete_if(&:blank?)
87+
@includes = options[:include].delete_if(&:blank?).map(&:to_sym)
8888
validate_includes!(@includes)
8989
end
9090
end

spec/lib/object_serializer_performance_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
}
3434
}
3535

36+
before(:all) { GC.disable }
37+
after(:all) { GC.enable }
38+
3639
context 'when testing performance of serialization' do
3740
it 'should create a hash of 1000 records in less than 50 ms' do
3841
movies = 1000.times.map { |_i| movie }

spec/lib/object_serializer_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
8989
expect { MovieSerializer.new([movie, movie], options).serializable_hash }.to raise_error(ArgumentError)
9090
end
9191

92+
it 'does not throw an error with non-empty string array includes key' do
93+
options = {}
94+
options[:include] = ['actors']
95+
expect { MovieSerializer.new(movie, options) }.not_to raise_error
96+
end
97+
9298
it 'returns keys when serializing with empty string/nil array includes key' do
9399
options = {}
94100
options[:meta] = { total: 2 }

0 commit comments

Comments
 (0)