flexirest 1.9.11 → 1.9.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 294f2e24981dde7af931d0bedd578e4eb260538f947f716739a3d4037cd2a6b8
4
- data.tar.gz: 5ab2e8babd0e312eea734257e6fdeeb1ccd1701143ec4a953a0ea90b102ed3f4
3
+ metadata.gz: 3320d5c425f13498cf558c361209bed466565f0b173ebcc2bd3f8e6205fd37dd
4
+ data.tar.gz: dd621beea4b50068147c70fbf079b7753d32e8a625e080a5803f4904cb757eea
5
5
  SHA512:
6
- metadata.gz: ce1125b17e4a41d11bc3cb3be10888d89604beaff46839fe3dbbce1fb663b4d69bbc58f3a8dab9463b2367daae9e182751393c8adc2ed5148e90138ce88ab167
7
- data.tar.gz: c4f992e9c9e70b2faabf3384512f6973b6e3f5fb8ebca7515faae48670ec61b630a2c951d908efbf95170506885f19a9e87454ce2bc0caac64a0a6f01a4f9770
6
+ metadata.gz: 8811975e7c20d9b6a69fcb9c11e68883c2950927d2444723872cd302c10cec7802fa732ed50fb7b088a56535beb23d9180d4e717d7884bd7a1901c5fe05295f1
7
+ data.tar.gz: 9a8db810620535a335d2d94822ba277b5a61fe0963ce19479d53c57c001567afd30c96fb40720d231142ec7fd2dbcc01c8423f0c83d44408f6ea4c231cf6591f
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.9.12
4
+
5
+ Bugfix:
6
+
7
+ - Prevent crash on JSONAPI invalid error response (thanks to François Ferrandis for the PR).
8
+
3
9
  ## 1.9.11
4
10
 
5
11
  Bugfix:
@@ -196,8 +196,10 @@ module Flexirest
196
196
  # Save resource class for building lazy association loaders
197
197
  save_resource_class(object)
198
198
 
199
- # try to return errors if their is no data
200
- return body.fetch("errors", {}) if body['data'].nil?
199
+ # According to the spec:
200
+ # "The members data and errors MUST NOT coexist in the same document."
201
+ # Thus, if the "errors" key is present, we can return it and ignore the "data" key.
202
+ return body['errors'] if body.include?('errors')
201
203
 
202
204
  # return early if data is an empty array
203
205
  return [] if body['data'] == []
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.9.11"
2
+ VERSION = "1.9.12"
3
3
  end
@@ -312,28 +312,46 @@ describe 'JSON API' do
312
312
  expect(subject.find_all).to be_an_instance_of(Flexirest::ResultIterator)
313
313
  end
314
314
 
315
- context 'when response has "errors" key and no "data" key' do
316
- let(:headers) { { "Content-Type" => "application/vnd.api+json" } }
317
- let(:response_body) do
318
- {
319
- errors: [
320
- {
321
- title: "Record not found",
322
- detail: "The record identified by 123456 could not be found",
323
- code: "not_found",
324
- status: "404",
325
- }
326
- ]
327
- }
328
- end
315
+ describe 'error responses' do
316
+ subject(:make_request) { JsonAPIExample::Article.real_find(123) }
329
317
 
330
- it 'should raise a Flexirest error' do
318
+ before do
319
+ headers = { "Content-Type" => "application/vnd.api+json" }
331
320
  expect_any_instance_of(Flexirest::Connection).
332
321
  to receive(:get).with("/articles/123", an_instance_of(Hash)).
333
322
  and_return(::FaradayResponseMock.new(OpenStruct.new(body: response_body.to_json, response_headers: headers, status: 404)))
323
+ end
324
+
325
+ context 'when no "data" key is present alongside the "errors" key' do
326
+ let(:response_body) do
327
+ {
328
+ errors: [
329
+ { detail: "The record identified by 123456 could not be found", }
330
+ ]
331
+ }
332
+ end
333
+
334
+ it 'should raise the relevant Flexirest error' do
335
+ expect(-> { make_request }).to raise_error(Flexirest::HTTPNotFoundClientException) do |exception|
336
+ expect(exception.result.first.detail).to eq("The record identified by 123456 could not be found")
337
+ end
338
+ end
339
+ end
340
+
341
+ context 'when a "data" key is present alongside the "errors" key (although this is forbidden by the spec)' do
342
+ let(:response_body) do
343
+ {
344
+ errors: [
345
+ { detail: "The record identified by 123456 could not be found", }
346
+ ],
347
+ data: {}
348
+ }
349
+ end
334
350
 
335
- expect(-> { JsonAPIExample::Article.real_find(123) }).to raise_error(Flexirest::HTTPNotFoundClientException) do |exception|
336
- expect(exception.result.first.title).to eq("Record not found")
351
+ it 'should ignore the "data" key and raise the relevant Flexirest error' do
352
+ expect(-> { make_request }).to raise_error(Flexirest::HTTPNotFoundClientException) do |exception|
353
+ expect(exception.result.first.detail).to eq("The record identified by 123456 could not be found")
354
+ end
337
355
  end
338
356
  end
339
357
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexirest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.11
4
+ version: 1.9.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-13 00:00:00.000000000 Z
11
+ date: 2020-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler