Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'rack'

module ForestAdminRpcAgent
module Routes
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading