faraday-openapi 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a818a9606167a2dfa048c8b08cd0edccadff4f84a0dd0c7816ec14fe20f94bde
4
- data.tar.gz: d50087aa63b994f18538fd0ee9d9dd42acb6707545161bb34c5b3158ffa370c3
3
+ metadata.gz: 571ee2451eee11277b3645a3851a4738791b94272326bbe36a320a4bf75d79d1
4
+ data.tar.gz: 77cf157de1852b404718d1f6cd482a13e2512133faa606fa4200040151e296a5
5
5
  SHA512:
6
- metadata.gz: 868f9a70c9866a8dac1cebcd8524fcb37e55875e7e4f9e7d33fd1fef4fe469815c01d3c5570e0decc9fd753c837a008c919d4dd1788306c8be9c1b12c3ff6e59
7
- data.tar.gz: 8e34e1829547253d5635cd6d28516fffcd177209b85280a69604fda5d6233d55e5f6febd46803497749aa7854c262847d17d48e9d179caadab1106a26e1b9514
6
+ metadata.gz: a450a0755f226635c42101561ed97a9363133701b28e60d004101941280cf66f0fe262551b46659a676b9acb0d341f95015759101557cb49330e1f4db86ecb6d
7
+ data.tar.gz: c2eb79c33c7449b07bc369bf9c13e94ef4acec4b02e2d0bd5d7e2318a117a70a40335ead528f784b5619cb4f6896080174936fd107996ffa6b89d968f8bcdacf
data/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.6.0
6
+
7
+ - Added `allow_partial_responses` option to allow skipping required properties in response body validation:
8
+ ```ruby
9
+ Faraday::Openapi.register('openapi.yaml', allow_partial_responses: true)
10
+ ```
11
+
12
+ This can be useful if you would have to mock very large response bodies, but your consumer just needs a few fields.
13
+
5
14
  ## 0.5.0
6
15
 
7
16
  - Update openapi_first from version 2 to 3
data/README.md CHANGED
@@ -39,9 +39,6 @@ register your API description (OAD) globally and reference it via a Symbol in yo
39
39
 
40
40
  require 'faraday/openapi'
41
41
  Faraday::Openapi.register 'dice-openapi.yaml', as: :dice_api
42
-
43
- # Only activate in test env
44
- Faraday::Openapi.enabled = ENV['RACK_ENV'] == 'test'
45
42
  ```
46
43
 
47
44
  ```ruby
@@ -63,7 +60,15 @@ conn = Faraday.new do |f|
63
60
  end
64
61
  ```
65
62
 
66
- You can disable all middlewares in this gem globally, which you probably want to do on production.
63
+ ## Configuration
64
+
65
+ Allow partial response bodies via `allow_partial_responses: true` (default: false). This can be useful if you would have to mock very large response bodies in tests where your consumer just uses a few fields.
66
+
67
+ ```ruby
68
+ Faraday::Openapi.register 'dice-openapi.yaml', as: :dice_api, allow_partial_responses: true
69
+ ```
70
+
71
+ You can disable all middlewares in this gem globally, which you want to do on production.
67
72
 
68
73
  ```ruby
69
74
  Faraday::Openapi.enabled = false
@@ -14,6 +14,7 @@ module Faraday
14
14
  return unless Openapi.enabled
15
15
 
16
16
  @oad = path.is_a?(Symbol) ? Faraday::Openapi[path] : OpenapiFirst.load(path)
17
+ @configuration = Faraday::Openapi.configurations[path]
17
18
  end
18
19
 
19
20
  def call(env)
@@ -50,11 +51,23 @@ module Faraday
50
51
  def on_complete(env)
51
52
  request = Request.from_env(env)
52
53
  response = Response.from_env(env)
53
- @oad.validate_response(request, response, raise_error: true)
54
- rescue OpenapiFirst::ResponseInvalidError, OpenapiFirst::ResponseNotFoundError => e
55
- return if e.is_a?(OpenapiFirst::ResponseNotFoundError) && (response.status >= 401)
54
+ validated_response = @oad.validate_response(request, response, raise_error: false)
56
55
 
57
- raise ResponseInvalidError, e.message
56
+ return if validated_response.valid?
57
+ return if !validated_response.known? && (validated_response.status >= 401)
58
+ return if ignore_error?(validated_response)
59
+
60
+ raise ResponseInvalidError, validated_response.error.message
61
+ end
62
+
63
+ private
64
+
65
+ def ignore_error?(validated_response)
66
+ partial_mocking_enabled? && validated_response.error.errors.all? { |err| err.type == 'required' }
67
+ end
68
+
69
+ def partial_mocking_enabled?
70
+ @configuration && @configuration[:allow_partial_responses]
58
71
  end
59
72
  end
60
73
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Openapi
5
- VERSION = '0.5.0'
5
+ VERSION = '0.6.0'
6
6
  end
7
7
  end
@@ -20,18 +20,20 @@ module Faraday
20
20
  Faraday::Response.register_middleware(openapi: Faraday::Openapi::ResponseMiddleware)
21
21
 
22
22
  @registry = {}
23
+ @configurations = {}
23
24
  @enabled = true
24
25
 
25
26
  class << self
26
- attr_reader :registry
27
+ attr_reader :registry, :configurations
27
28
  attr_accessor :enabled
28
29
  end
29
30
 
30
- def self.register(filepath, as: :default)
31
+ def self.register(filepath, as: :default, allow_partial_responses: false)
31
32
  raise AlreadyRegisteredError, "API description #{as} is already registered" if registry.key?(as)
32
33
 
33
34
  oad = filepath.is_a?(Hash) ? OpenapiFirst.parse(filepath) : OpenapiFirst.load(filepath)
34
35
  registry[as] = oad
36
+ configurations[as] = { allow_partial_responses: }
35
37
  rescue OpenapiFirst::FileNotFoundError => e
36
38
  raise Faraday::Openapi::FileNotFoundError, e.message
37
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
@@ -93,7 +93,7 @@ licenses:
93
93
  metadata:
94
94
  bug_tracker_uri: https://codeberg.org/ahx/faraday-openapi/issues
95
95
  changelog_uri: https://codeberg.org/ahx/faraday-openapi/src/branch/main/CHANGELOG.md
96
- documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.5.0
96
+ documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.6.0
97
97
  homepage_uri: https://codeberg.org/ahx/faraday-openapi
98
98
  rubygems_mfa_required: 'true'
99
99
  source_code_uri: https://codeberg.org/ahx/faraday-openapi