diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 12ef99c5c..039ee6982 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -1,8 +1,6 @@ module API module V1 class BaseController < ActionController::API - include Rails::Pagination - self.responder = JSONAPIResponder respond_to :json diff --git a/app/controllers/api/v1/beneficiaries/stats_controller.rb b/app/controllers/api/v1/beneficiaries/stats_controller.rb index d1a92153a..6f700380f 100644 --- a/app/controllers/api/v1/beneficiaries/stats_controller.rb +++ b/app/controllers/api/v1/beneficiaries/stats_controller.rb @@ -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 diff --git a/lib/jsonapi_responder.rb b/lib/jsonapi_responder.rb index f71a0e5d7..e97f3df3a 100644 --- a/lib/jsonapi_responder.rb +++ b/lib/jsonapi_responder.rb @@ -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 diff --git a/spec/requests/open_ews_api/v1/beneficiaries_spec.rb b/spec/requests/open_ews_api/v1/beneficiaries_spec.rb index 62120d430..9effb291e 100644 --- a/spec/requests/open_ews_api/v1/beneficiaries_spec.rb +++ b/spec/requests/open_ews_api/v1/beneficiaries_spec.rb @@ -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( @@ -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", diff --git a/spec/support/api_response_schema_matchers.rb b/spec/support/api_response_schema_matchers.rb index d3ccb9ceb..81cc50a45 100644 --- a/spec/support/api_response_schema_matchers.rb +++ b/spec/support/api_response_schema_matchers.rb @@ -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 @@ -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 @@ -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 @@ -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