diff --git a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb index 022d95ad3..d4b20e8c1 100644 --- a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb +++ b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb @@ -45,15 +45,6 @@ def mark_collections_as_rpc(*names) self end - # Returns the cached schema for the /rpc-schema route - # Falls back to building schema from datasource if not cached - def rpc_schema - return @cached_schema if @cached_schema - - build_and_cache_rpc_schema_from_datasource - @cached_schema - end - # Check if provided hash matches the cached schema hash def schema_hash_matches?(provided_hash) return false unless @cached_schema_hash && provided_hash @@ -84,13 +75,6 @@ def write_schema_file_for_reference ) end - def build_and_cache_rpc_schema_from_datasource - datasource = @container.resolve(:datasource) - - @cached_schema = build_rpc_schema_from_datasource(datasource) - compute_and_cache_hash - end - def build_rpc_schema_from_datasource(datasource) schema = customizer.schema @@ -106,27 +90,20 @@ def build_rpc_schema_from_datasource(datasource) # Serialize collection schema, converting field objects to plain hashes def serialize_collection_schema(collection) - schema = collection.schema.dup + schema = collection.schema schema[:name] = collection.name - schema[:fields] = schema[:fields].transform_values { |field| object_to_hash(field) } + schema[:fields] = schema[:fields].transform_values { |field| serialize_field(field) } schema end - # Convert any object to a hash using its instance variables - def object_to_hash(obj) - return obj if obj.is_a?(Hash) || obj.is_a?(Array) || obj.is_a?(String) || obj.is_a?(Numeric) || obj.nil? - - hash = obj.instance_variables.to_h do |var| - [var.to_s.delete('@').to_sym, obj.instance_variable_get(var)] - end - - if hash[:filter_operators].is_a?(Array) - hash[:filter_operators] = ForestAdminAgent::Utils::Schema::FrontendFilterable.sort_operators( - hash[:filter_operators] + def serialize_field(field) + if (field.type == 'Column' && field.filter_operators) + field.filter_operators = ForestAdminAgent::Utils::Schema::FrontendFilterable.sort_operators( + field.filter_operators ) end - hash + field end def compute_and_cache_hash diff --git a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/schema.rb b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/schema.rb index ee1b3c3ca..1bf4a7141 100644 --- a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/schema.rb +++ b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/schema.rb @@ -27,7 +27,7 @@ def handle_request(args) end # Get schema from cache (or build from datasource if not cached) - schema = agent.rpc_schema + schema = agent.cached_schema etag = agent.cached_schema_hash # Return schema with ETag header diff --git a/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/schema_spec.rb b/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/schema_spec.rb index d502daf61..eddd125e5 100644 --- a/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/schema_spec.rb +++ b/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/schema_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'rack' module ForestAdminRpcAgent module Routes @@ -26,7 +27,7 @@ module Routes allow(ForestAdminRpcAgent::Agent).to receive(:instance).and_return(agent) allow(ForestAdminRpcAgent::Facades::Container).to receive(:logger).and_return(logger) allow(logger).to receive(:log) - allow(agent).to receive_messages(rpc_schema: cached_schema, + allow(agent).to receive_messages(cached_schema: cached_schema, cached_schema_hash: cached_hash, schema_hash_matches?: false) end @@ -42,7 +43,7 @@ module Routes context 'when client provides matching If-None-Match header' do let(:mock_request) do - instance_double(Rack::Request, get_header: %("#{cached_hash}")) + instance_double(::Rack::Request, get_header: %("#{cached_hash}")) end before do @@ -66,7 +67,7 @@ module Routes context 'when client provides non-matching If-None-Match header' do let(:mock_request) do - instance_double(Rack::Request, get_header: '"different_hash"') + instance_double(::Rack::Request, get_header: '"different_hash"') end it 'returns the full schema with ETag header' do @@ -80,7 +81,7 @@ module Routes context 'when client does not provide If-None-Match header' do let(:mock_request) do - instance_double(Rack::Request, get_header: nil) + instance_double(::Rack::Request, get_header: nil) end it 'returns the full schema' do