diff --git a/lib/apia/definitions/error.rb b/lib/apia/definitions/error.rb index b213c55..9ad2067 100644 --- a/lib/apia/definitions/error.rb +++ b/lib/apia/definitions/error.rb @@ -37,7 +37,7 @@ def http_status_code # API. # # @param errors [Apia::ManifestErrors] - # @reeturn [void] + # @return [void] def validate(errors) unless code.is_a?(Symbol) errors.add self, 'InvalidCode', 'Code must be a symbol' diff --git a/lib/apia/response.rb b/lib/apia/response.rb index dc94aaa..5ac6632 100644 --- a/lib/apia/response.rb +++ b/lib/apia/response.rb @@ -70,11 +70,14 @@ def body # # @return [Array] def rack_triplet - case @type - when JSON - Rack.json_triplet(body, headers: headers, status: status) - when PLAIN + # Errors will always be sent as a hash intended for JSON encoding, + # even if the endpoint specifies a plain text response, so only + # send a pain response if the type is plaintext _and_ the body is + # a string + if @type == PLAIN && body.is_a?(String) Rack.plain_triplet(body, headers: headers, status: status) + else + Rack.json_triplet(body, headers: headers, status: status) end end diff --git a/spec/specs/apia/response_spec.rb b/spec/specs/apia/response_spec.rb index 828ef3f..467f84e 100644 --- a/spec/specs/apia/response_spec.rb +++ b/spec/specs/apia/response_spec.rb @@ -135,6 +135,14 @@ expect(response.rack_triplet[2][0]).to eq 'hello world' expect(response.rack_triplet[1]['content-length']).to eq '11' end + + it 'should return JSON if the body is not a string' do + response = Apia::Response.new(request, endpoint) + response.body = { hello: 'world' } + expect(response.rack_triplet[1]['content-type']).to eq 'application/json' + expect(response.rack_triplet[1]['content-length']).to eq '17' + expect(response.rack_triplet[2][0]).to eq '{"hello":"world"}' + end end context 'with a legacy plain text response' do