Skip to content

Commit

Permalink
Disable pagination on beneficiary stats resources (#1699)
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang authored Jan 15, 2025
1 parent 81f7853 commit 81a3b20
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
2 changes: 0 additions & 2 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module API
module V1
class BaseController < ActionController::API
include Rails::Pagination

self.responder = JSONAPIResponder
respond_to :json

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/beneficiaries/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def serializer_options
{
input_params: request.query_parameters,
decorator_class: nil,
pagination_options: {
sort_direction: :asc
serializer_options: {
pagination: false
}
}
end
Expand Down
11 changes: 7 additions & 4 deletions lib/jsonapi_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ def display(resource, given_options = {})
end

if resource_is_collection?(resource)
pagination = JSONAPIPagination.new(resource, request.original_url, pagination_options:)
links = serializer_options.fetch(:links, {})
serializer_options[:links] = pagination.links.merge(links)
resource = pagination.paginated_collection
if serializer_options.fetch(:pagination, true) != false
pagination = JSONAPIPagination.new(resource, request.original_url, pagination_options:)
links = serializer_options.fetch(:links, {})
serializer_options[:links] = pagination.links.merge(links)
resource = pagination.paginated_collection
end

resource = resource.map(&:decorated) if decorator_class.present?
else
resource = resource.decorated
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/open_ews_api/v1/beneficiaries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
)

expect(response_status).to eq(200)
expect(response_body).to match_jsonapi_resource_collection_schema("stat")
expect(response_body).to match_jsonapi_resource_collection_schema("stat", pagination: false)
results = json_response.fetch("data").map { |data| data.dig("attributes", "result") }

expect(results).to match_array(
Expand Down Expand Up @@ -398,10 +398,10 @@
do_request(group_by: [ "gender" ])

expect(response_status).to eq(200)
expect(response_body).to match_jsonapi_resource_collection_schema("stat")
expect(response_body).to match_jsonapi_resource_collection_schema("stat", pagination: false)
results = json_response.fetch("data").map { |data| data.dig("attributes", "result") }

expect(results).to eq(
expect(results).to match_array(
[
{
"gender" => "M",
Expand Down
27 changes: 22 additions & 5 deletions spec/support/api_response_schema_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
end
end

RSpec::Matchers.define :match_jsonapi_resource_collection_schema do |schema_name|
RSpec::Matchers.define :match_jsonapi_resource_collection_schema do |schema_name, options = {}|
match do |response_body|
@validator = JSONAPIResourceSchemaValidator.new(response_body, schema_name)
@validator.valid_collection?
@validator.valid_collection?(**options)
end

failure_message do
Expand All @@ -59,7 +59,7 @@ def valid_response?
end

def valid_collection?(**options)
schema = options.fetch(:pagination, true) ? define_collection_schema : define_collection_schema_with_no_pagination
schema = options.fetch(:pagination, true) == false ? define_collection_schema_with_no_pagination : define_collection_schema

validate_schema(schema)
end
Expand Down Expand Up @@ -115,8 +115,15 @@ def valid_resource?
validate_schema(define_resource_schema)
end

def valid_collection?
validate_schema(define_collection_schema)
def valid_collection?(**options)
return unless super

if options[:pagination] == false
json_response = JSON.parse(data)
raise "Collection have pagination links" if json_response["links"].present?
end

true
end

private
Expand All @@ -143,4 +150,14 @@ def define_collection_schema
end
end
end

def define_collection_schema_with_no_pagination
__schema__ = schema

Dry::Schema.JSON do
required(:data).value(:array).each do
schema(__schema__)
end
end
end
end

0 comments on commit 81a3b20

Please sign in to comment.