openapi_first 1.4.0 → 1.4.1

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: 1a462473bc1c585914983760dc3023fda2a375276f2703712ec1f8e7d8ef4950
4
- data.tar.gz: 46c6cb68fc175a55525595ed90a4cf599af36d117a80dff8078fecf24aca79d7
3
+ metadata.gz: 5e143a15321c0af7dfde5539a31ab7909255e9efdf50fcb7d59bde406d3aabb2
4
+ data.tar.gz: 84304a9fd090b4fa25c56e515d5502e8b40195da17a0f26a0ecde972ae2107e1
5
5
  SHA512:
6
- metadata.gz: c51b30bf959533eeee109cea59fdce2c1f0057e1c40eec6a961b0360000665e940ff1366125db655b1d2026f7bf8767ba62cd8358b6a31b9ac7128f4f129c497
7
- data.tar.gz: d639e00db05a01a1e2f8bdb57c1e95c72ee71c5bcc317d53b1130a915124075d0b8e922cb0c3340ba02b709a1c7e06142024f64d1de264abaa631caf7522b563
6
+ metadata.gz: a17ab4fa7b30355f0396f96601c3da19ec863b4a2f895d47e0841dcb776442eeb22da979fbce006354f27ca2bf09658d64883c8b4293f53402eb2b330231b0c4
7
+ data.tar.gz: ad332622204451191ba668c237812c3e1b62328130ad76b7caf0229c000dd935497c5ea7b9df8762f8dbd4a3456a5583cfde2078daf713b4fcacca21e697c0a3
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.4.1
6
+
7
+ - Fixed: Don't call deprecated methods in middlewares
8
+
5
9
  ## 1.4.0
6
10
 
7
11
  ### Changed
@@ -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.
@@ -34,9 +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
37
+ body.map { |part| part }
40
38
  end
41
39
  end
42
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
 
@@ -124,7 +123,6 @@ module OpenapiFirst
124
123
  def validate
125
124
  warn '[DEPRECATION] `validate` is deprecated. Please use ' \
126
125
  "`OpenapiFirst.load('openapi.yaml').validate_request(rack_request)` instead."
127
- @validated = true
128
126
  @error = RequestValidation::Validator.new(operation).validate(self)
129
127
  end
130
128
 
@@ -159,6 +157,10 @@ module OpenapiFirst
159
157
 
160
158
  private
161
159
 
160
+ def validated?
161
+ defined?(@error)
162
+ end
163
+
162
164
  attr_reader :request
163
165
  end
164
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
 
@@ -71,7 +73,6 @@ module OpenapiFirst
71
73
  def validate
72
74
  warn '[DEPRECATION] `validate` is deprecated. ' \
73
75
  "Please use `OpenapiFirst.load('openapi.yaml').validate_response(rack_request, rack_response)` instead."
74
- @validated = true
75
76
  @error = ResponseValidation::Validator.new(@operation).validate(self)
76
77
  end
77
78
 
@@ -90,6 +91,10 @@ module OpenapiFirst
90
91
 
91
92
  private
92
93
 
94
+ def validated?
95
+ defined?(@error)
96
+ end
97
+
93
98
  # Usually the body responds to #each, but when using manual response validation without the middleware
94
99
  # in Rails request specs the body is a String. So this code handles both cases.
95
100
  def original_body
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
- VERSION = '1.4.0'
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.4.0
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-04-07 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