openapi_first 1.3.6 → 1.4.1

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: 221362a78a788c449a8c11af9c40fc91bf307c82ee79d0931c94f39bb22901bc
4
- data.tar.gz: d9b806ed56371199552b4081d2b6b220279b925b9fdbdaa90ce47623c86e4bff
3
+ metadata.gz: 5e143a15321c0af7dfde5539a31ab7909255e9efdf50fcb7d59bde406d3aabb2
4
+ data.tar.gz: 84304a9fd090b4fa25c56e515d5502e8b40195da17a0f26a0ecde972ae2107e1
5
5
  SHA512:
6
- metadata.gz: 5c5ceac8c85f7d074983e5d237febe8ecf9ec3414a5ecb1a5bd8af43259eb2c83621ac867ca9b1d5420a3dda7518e131ed2ac1f08b86d71fa6fdfccbf76e1885
7
- data.tar.gz: 92c44316bf96d31841a959898efcd20e08bc223f233e26af253cc3575dbf29680b13e27ab628eb9bb9544371c08c0d7aea42b5aaf8bef75d369299067e6eb0ca
6
+ metadata.gz: a17ab4fa7b30355f0396f96601c3da19ec863b4a2f895d47e0841dcb776442eeb22da979fbce006354f27ca2bf09658d64883c8b4293f53402eb2b330231b0c4
7
+ data.tar.gz: ad332622204451191ba668c237812c3e1b62328130ad76b7caf0229c000dd935497c5ea7b9df8762f8dbd4a3456a5583cfde2078daf713b4fcacca21e697c0a3
data/CHANGELOG.md CHANGED
@@ -2,9 +2,24 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.4.1
6
+
7
+ - Fixed: Don't call deprecated methods in middlewares
8
+
9
+ ## 1.4.0
10
+
11
+ ### Changed
12
+
13
+ Some redundant methods to validate or inspect requests/responses will be removed in 2.0. So this release deprecates these methods.
14
+
15
+ - Deprecate `OpenapiFirst::RuntimeRequest#validate`, `#validate!`, `#validate_response`, `#response`.
16
+ Use `OpenapiFirst.load('openapi.yaml').validate_request(rack_request, raise_error: true/false)` instead
17
+ - Deprecate `OpenapiFirst::RuntimeResponse#validate`.
18
+ Use `OpenapiFirst.load('openapi.yaml').validate_response(rack_request, rack_response, raise_error: true/false)` instead.
19
+
5
20
  ## 1.3.6
6
21
 
7
- - Fix Rack 2 / Rails 6 compatibility ([#246](https://github.com/ahx/openapi_first/issues/246)
22
+ - FIx Rack 2 / Rails 6 compatibility ([#246](https://github.com/ahx/openapi_first/issues/246)
8
23
 
9
24
  ## 1.3.5
10
25
 
@@ -24,9 +24,12 @@ module OpenapiFirst
24
24
  # @param raise_error [Boolean] Whether to raise an error if validation fails.
25
25
  # @return [RuntimeRequest] The validated request object.
26
26
  def validate_request(rack_request, raise_error: false)
27
- validated = request(rack_request).tap(&:validate)
28
- validated.error&.raise! if raise_error
29
- validated
27
+ runtime_request = request(rack_request)
28
+ validator = RequestValidation::Validator.new(runtime_request.operation)
29
+ validation_error = validator.validate(runtime_request)
30
+ validation_error.raise! if validation_error && raise_error
31
+ runtime_request.error = validation_error
32
+ runtime_request
30
33
  end
31
34
 
32
35
  # Validates the response against the API description.
@@ -35,7 +38,12 @@ module OpenapiFirst
35
38
  # @param raise_error [Boolean] Whether to raise an error if validation fails.
36
39
  # @return [RuntimeResponse] The validated response object.
37
40
  def validate_response(rack_request, rack_response, raise_error: false)
38
- request(rack_request).validate_response(rack_response, raise_error:)
41
+ runtime_response = response(rack_request, rack_response)
42
+ validator = ResponseValidation::Validator.new(runtime_response.operation)
43
+ validation_error = validator.validate(runtime_response)
44
+ validation_error.raise! if validation_error && raise_error
45
+ runtime_response.error = validation_error
46
+ runtime_response
39
47
  end
40
48
 
41
49
  # Builds a RuntimeRequest object based on the Rack request.
@@ -57,7 +65,8 @@ module OpenapiFirst
57
65
  # @param rack_response [Rack::Response] The Rack response object.
58
66
  # @return [RuntimeResponse] The RuntimeResponse object.
59
67
  def response(rack_request, rack_response)
60
- request(rack_request).response(rack_response)
68
+ runtime_request = request(rack_request)
69
+ RuntimeResponse.new(runtime_request.operation, rack_response)
61
70
  end
62
71
 
63
72
  # Gets all the operations defined in the API description.
@@ -26,11 +26,9 @@ module OpenapiFirst
26
26
  attr_reader :app
27
27
 
28
28
  def call(env)
29
- request = find_request(env)
30
- return @app.call(env) unless request
31
-
32
- failure = request.validate
33
- failure.raise! if failure && @raise
29
+ validated = @definition.validate_request(Rack::Request.new(env), raise_error: @raise)
30
+ env[REQUEST] ||= validated
31
+ failure = validated.error
34
32
  return @error_response_class.new(failure:).render if failure
35
33
 
36
34
  @app.call(env)
@@ -38,10 +36,6 @@ module OpenapiFirst
38
36
 
39
37
  private
40
38
 
41
- def find_request(env)
42
- env[REQUEST] ||= @definition.request(Rack::Request.new(env))
43
- end
44
-
45
39
  def error_response(mod)
46
40
  return OpenapiFirst.find_plugin(mod)::ErrorResponse if mod.is_a?(Symbol)
47
41
 
@@ -22,10 +22,10 @@ module OpenapiFirst
22
22
  attr_reader :app
23
23
 
24
24
  def call(env)
25
- request = find_request(env)
26
25
  status, headers, body = @app.call(env)
27
26
  body = read_body(body)
28
- request.validate_response(Rack::Response[status, headers, body], raise_error: true)
27
+ @definition.validate_response(Rack::Request.new(env), Rack::Response[status, headers, body], raise_error: true)
28
+ env[REQUEST] ||= @definition.request(Rack::Request.new(env))
29
29
  [status, headers, body]
30
30
  end
31
31
 
@@ -34,13 +34,7 @@ module OpenapiFirst
34
34
  def read_body(body)
35
35
  return body.to_ary if body.respond_to?(:to_ary)
36
36
 
37
- result = []
38
- body.each { |part| result << part }
39
- result
40
- end
41
-
42
- def find_request(env)
43
- env[REQUEST] ||= @definition.request(Rack::Request.new(env))
37
+ body.map { |part| part }
44
38
  end
45
39
  end
46
40
  end
@@ -16,7 +16,6 @@ module OpenapiFirst
16
16
  @path_item = path_item
17
17
  @operation = operation
18
18
  @original_path_params = path_params
19
- @error = nil
20
19
  @validated = false
21
20
  end
22
21
 
@@ -34,12 +33,12 @@ module OpenapiFirst
34
33
 
35
34
  # Returns the error object if validation failed.
36
35
  # @return [Failure, nil]
37
- attr_reader :error
36
+ attr_accessor :error
38
37
 
39
38
  # Checks if the request is valid.
40
39
  # @return [Boolean] true if the request is valid, false otherwise.
41
40
  def valid?
42
- validate unless @validated
41
+ validate unless validated?
43
42
  error.nil?
44
43
  end
45
44
 
@@ -120,13 +119,17 @@ module OpenapiFirst
120
119
 
121
120
  # Validates the request.
122
121
  # @return [Failure, nil] The Failure object if validation failed.
122
+ # @deprecated Please use {Definition#validate_request} instead
123
123
  def validate
124
- @validated = true
124
+ warn '[DEPRECATION] `validate` is deprecated. Please use ' \
125
+ "`OpenapiFirst.load('openapi.yaml').validate_request(rack_request)` instead."
125
126
  @error = RequestValidation::Validator.new(operation).validate(self)
126
127
  end
127
128
 
128
129
  # Validates the request and raises an error if validation fails.
129
130
  def validate!
131
+ warn '[DEPRECATION] `validate!` is deprecated. Please use ' \
132
+ "`OpenapiFirst.load('openapi.yaml').validate_request(rack_request, raise_error: true)` instead."
130
133
  error = validate
131
134
  error&.raise!
132
135
  end
@@ -136,6 +139,8 @@ module OpenapiFirst
136
139
  # @param raise_error [Boolean] Whether to raise an error if validation fails.
137
140
  # @return [RuntimeResponse] The validated response object.
138
141
  def validate_response(rack_response, raise_error: false)
142
+ warn '[DEPRECATION] `validate_response!` is deprecated. Please use ' \
143
+ "`OpenapiFirst.load('openapi.yaml').validate_response(request, response, raise_error: false)` instead."
139
144
  validated = response(rack_response).tap(&:validate)
140
145
  validated.error&.raise! if raise_error
141
146
  validated
@@ -145,11 +150,17 @@ module OpenapiFirst
145
150
  # @param rack_response [Rack::Response] The rack response object.
146
151
  # @return [RuntimeResponse] The RuntimeResponse object.
147
152
  def response(rack_response)
153
+ warn '[DEPRECATION] `response` is deprecated. Please use ' \
154
+ "`OpenapiFirst.load('openapi.yaml').validate_response(request, response, raise_error: false)` instead."
148
155
  RuntimeResponse.new(operation, rack_response)
149
156
  end
150
157
 
151
158
  private
152
159
 
160
+ def validated?
161
+ defined?(@error)
162
+ end
163
+
153
164
  attr_reader :request
154
165
  end
155
166
  end
@@ -12,11 +12,13 @@ module OpenapiFirst
12
12
  def initialize(operation, rack_response)
13
13
  @operation = operation
14
14
  @rack_response = rack_response
15
- @error = nil
16
15
  end
17
16
 
18
17
  # @return [Failure, nil] Error object if validation failed.
19
- attr_reader :error
18
+ attr_accessor :error
19
+
20
+ # @visibility private
21
+ attr_accessor :operation
20
22
 
21
23
  # @attr_reader [Integer] status The HTTP status code of this response.
22
24
  # @attr_reader [String] content_type The content_type of the Rack::Response.
@@ -30,7 +32,7 @@ module OpenapiFirst
30
32
  # Checks if the response is valid. Runs the validation unless it has been run before.
31
33
  # @return [Boolean]
32
34
  def valid?
33
- validate unless @validated
35
+ validate unless validated?
34
36
  @error.nil?
35
37
  end
36
38
 
@@ -67,8 +69,10 @@ module OpenapiFirst
67
69
 
68
70
  # Validates the response.
69
71
  # @return [Failure, nil] Returns the validation error, or nil if the response is valid.
72
+ # @deprecated Please use {Definition#validate_response} instead
70
73
  def validate
71
- @validated = true
74
+ warn '[DEPRECATION] `validate` is deprecated. ' \
75
+ "Please use `OpenapiFirst.load('openapi.yaml').validate_response(rack_request, rack_response)` instead."
72
76
  @error = ResponseValidation::Validator.new(@operation).validate(self)
73
77
  end
74
78
 
@@ -87,6 +91,10 @@ module OpenapiFirst
87
91
 
88
92
  private
89
93
 
94
+ def validated?
95
+ defined?(@error)
96
+ end
97
+
90
98
  # Usually the body responds to #each, but when using manual response validation without the middleware
91
99
  # in Rails request specs the body is a String. So this code handles both cases.
92
100
  def original_body
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
- VERSION = '1.3.6'
4
+ VERSION = '1.4.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_first
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.6
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-22 00:00:00.000000000 Z
11
+ date: 2024-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hana