From fc9cc412e182e1ea025aee434612fbe790112185 Mon Sep 17 00:00:00 2001 From: Sam Morgan Date: Wed, 28 Feb 2018 16:29:24 +0000 Subject: [PATCH] 91 allow includes strings (#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 #97) * This is solved by mapping the strings to symbols in the ObjectSerializer initializer * No real impact on performance here --- lib/fast_jsonapi/object_serializer.rb | 12 +++++++++++- spec/lib/object_serializer_performance_spec.rb | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/fast_jsonapi/object_serializer.rb b/lib/fast_jsonapi/object_serializer.rb index 4dea074f..b9713dee 100644 --- a/lib/fast_jsonapi/object_serializer.rb +++ b/lib/fast_jsonapi/object_serializer.rb @@ -77,7 +77,17 @@ def process_options(options) if options[:include].present? @includes = options[:include].delete_if(&:blank?).map(&:to_sym) - self.class.validate_includes!(@includes) + validate_includes!(@includes) + end + end + + def validate_includes!(includes) + return if includes.blank? + + existing_relationships = self.class.relationships_to_serialize.keys.to_set + + unless existing_relationships.superset?(includes.to_set) + raise ArgumentError, "One of keys from #{includes} is not specified as a relationship on the serializer" end end diff --git a/spec/lib/object_serializer_performance_spec.rb b/spec/lib/object_serializer_performance_spec.rb index 9119ce3c..41141c03 100644 --- a/spec/lib/object_serializer_performance_spec.rb +++ b/spec/lib/object_serializer_performance_spec.rb @@ -33,6 +33,9 @@ } } + before(:all) { GC.disable } + after(:all) { GC.enable } + context 'when testing performance of serialization' do it 'should create a hash of 1000 records in less than 50 ms' do movies = 1000.times.map { |_i| movie }