From de4458642e7b146e8efa9563b42a417fc5daed1c Mon Sep 17 00:00:00 2001 From: Juan Carlos Garcia Date: Mon, 6 Nov 2023 12:55:57 +0100 Subject: [PATCH] fix(#5): Prevent to change the response if the endpoint returns a hash --- CHANGELOG.md | 7 +++++++ lib/grape/idempotency.rb | 10 ++++++---- lib/grape/idempotency/version.rb | 2 +- spec/idempotent_spec.rb | 23 +++++++++++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d8a954..3753c3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All changes to `grape-idempotency` will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.1.2] - 2023-01-06 + +### Fix + +- Return correct original response when the endpoint returns a hash in the body + + ## [0.1.1] - 2023-01-06 ### Fix diff --git a/lib/grape/idempotency.rb b/lib/grape/idempotency.rb index 3a86560..132bed4 100644 --- a/lib/grape/idempotency.rb +++ b/lib/grape/idempotency.rb @@ -41,13 +41,11 @@ def idempotent(grape, &block) block.call end - if response.is_a?(Hash) - response = response[:message].to_json - end + response = response[:message].to_json if is_an_error?(response) original_request_id = get_request_id(grape.request.headers) grape.header(ORIGINAL_REQUEST_HEADER, original_request_id) - response + grape.body response ensure validate_config! unless cached_request @@ -107,6 +105,10 @@ def store_in_cache(idempotency_key, path, params, status, request_id, response) end end + def is_an_error?(response) + response.is_a?(Hash) && response.has_key?(:message) && response.has_key?(:headers) && response.has_key?(:status) + end + def key(idempotency_key) "grape:idempotency:#{idempotency_key}" end diff --git a/lib/grape/idempotency/version.rb b/lib/grape/idempotency/version.rb index 4895fb3..977da89 100644 --- a/lib/grape/idempotency/version.rb +++ b/lib/grape/idempotency/version.rb @@ -2,6 +2,6 @@ module Grape module Idempotency - VERSION = '0.1.1' + VERSION = '0.1.2' end end \ No newline at end of file diff --git a/spec/idempotent_spec.rb b/spec/idempotent_spec.rb index 2f78d6b..552e635 100644 --- a/spec/idempotent_spec.rb +++ b/spec/idempotent_spec.rb @@ -118,6 +118,29 @@ expect(last_response.headers).to include("original-request" => "req_123456") end end + + context 'when the endpoint returns a hash' do + it 'returns the original response' do + allow(SecureRandom).to receive(:random_number).and_return(1, 2) + + app.post('/payments') do + idempotent do + status 200 + { amount_to: SecureRandom.random_number } + end + end + + header "idempotency-key", idempotency_key + post 'payments?locale=es', { amount: 100_00 }.to_json + expect(last_response.status).to eq(200) + expect(last_response.body).to eq("{:amount_to=>1}") + + header "idempotency-key", idempotency_key + post 'payments?locale=es', { amount: 100_00 }.to_json + expect(last_response.status).to eq(200) + expect(last_response.body).to eq("{\"amount_to\"=>1}") + end + end end context 'but any of the provided parameters does NOT match with the original request' do