openapi_contracts 0.13.2 → 0.14.0

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: a6ead386a672bc6da357f0686b6de4fd01e9c7a7155b65e312b04975f138a76f
4
- data.tar.gz: cea44722316b8bb3d2bbb85f47a4794b8cdb3f143ff8cf8d65ad9a262d546127
3
+ metadata.gz: 6a860586af31ca0bcf61a9301dba60df4466d367a495efbb306498eb71496f09
4
+ data.tar.gz: d74042e38633c207a4410ba6e961e68ed206f4b9d3a0d1fe0ea8d8aa1f4f4982
5
5
  SHA512:
6
- metadata.gz: 13b0e8e63dfe4e1459d9fcca05bd82b017465ce180b4680b2a9f7464ffbef837e89810f3ba5be1b484f41619694a97bcac814787d81361be6a1c94bd96851728
7
- data.tar.gz: 2b20b1298db18a1846f677569e7a4eb257672a6f138c0d156dbd2caf7b70097469f4baf8c9a138bf07ff68355c25dd9e7547b86d24705259b2bd1ec4435d6982
6
+ metadata.gz: 0bdddf66b9418f3374179566ebccfa5d7abad647708557ce5ec176afe620cbfcf79102743ffff6d293503278eeaccd8465e0a8627ff03b646b44c2c0aeb7e8f4
7
+ data.tar.gz: 9c0e8a0ba61f44bb8bde129524531a7a60918c9ff5fc23072c3d547a23a70ae45ef1e1a0983dc48b0ff2f57c8caf4e5998b5c80088d08aa645c99ccc0ed9acf6
data/README.md CHANGED
@@ -62,6 +62,12 @@ it { is_expected.to match_openapi_doc($doc, path: '/messages/{id}').with_http_st
62
62
  it { is_expected.to match_openapi_doc($doc, request_body: true).with_http_status(:created) }
63
63
  ```
64
64
 
65
+ * `parameters` can be set to `true` to validate request parameters against the parameter definitions
66
+
67
+ ```ruby
68
+ it { is_expected.to match_openapi_doc($doc, parameters: true) }
69
+ ```
70
+
65
71
  Both options can as well be used simultaneously.
66
72
 
67
73
  ### Without RSpec
@@ -14,33 +14,34 @@ module OpenapiContracts
14
14
  @in == 'path'
15
15
  end
16
16
 
17
+ def in_query?
18
+ @in == 'query'
19
+ end
20
+
17
21
  def matches?(value)
18
- case @spec.dig('schema', 'type')
19
- when 'integer'
20
- integer_parameter_matches?(value)
21
- when 'number'
22
- number_parameter_matches?(value)
23
- else
24
- schemer.valid?(value)
25
- end
22
+ errors = schemer.validate(convert_value(value))
23
+ # debug errors.to_a here
24
+ errors.none?
26
25
  end
27
26
 
28
- private
27
+ def required?
28
+ @required == true
29
+ end
29
30
 
30
- def schemer
31
- @schemer ||= Validators::SchemaValidation.validation_schemer(@spec.navigate('schema'))
31
+ def schema_for_validation
32
+ @spec.navigate('schema')
32
33
  end
33
34
 
34
- def integer_parameter_matches?(value)
35
- return false unless /^-?\d+$/.match?(value)
35
+ private
36
36
 
37
- schemer.valid?(value.to_i)
37
+ def convert_value(original)
38
+ OpenapiParameters::Converter.convert(original, schema_for_validation)
39
+ rescue StandardError
40
+ original
38
41
  end
39
42
 
40
- def number_parameter_matches?(value)
41
- return false unless /^-?(\d+\.)?\d+$/.match?(value)
42
-
43
- schemer.valid?(value.to_f)
43
+ def schemer
44
+ @schemer ||= Validators::SchemaValidation.validation_schemer(schema_for_validation)
44
45
  end
45
46
  end
46
47
  end
@@ -1,6 +1,9 @@
1
1
  module OpenapiContracts
2
2
  class Match
3
- DEFAULT_OPTIONS = {request_body: false}.freeze
3
+ DEFAULT_OPTIONS = {
4
+ parameters: false,
5
+ request_body: false
6
+ }.freeze
4
7
  MIN_REQUEST_ANCESTORS = %w(Rack::Request::Env Rack::Request::Helpers).freeze
5
8
  MIN_RESPONSE_ANCESTORS = %w(Rack::Response::Helpers).freeze
6
9
 
@@ -42,6 +45,7 @@ module OpenapiContracts
42
45
  )
43
46
  validators = Validators::ALL.dup
44
47
  validators.delete(Validators::HttpStatus) unless @options[:status]
48
+ validators.delete(Validators::Parameters) unless @options[:parameters]
45
49
  validators.delete(Validators::RequestBody) unless @options[:request_body]
46
50
  validators.reverse
47
51
  .reduce(->(err) { err }) { |s, m| m.new(s, env) }
@@ -0,0 +1,21 @@
1
+ module OpenapiContracts::Validators
2
+ # Validates the input parameters, eg path/url parameters
3
+ class Parameters < Base
4
+ include SchemaValidation
5
+
6
+ private
7
+
8
+ def validate
9
+ operation.parameters.select(&:in_query?).each do |parameter|
10
+ if request.GET.key?(parameter.name)
11
+ value = request.GET[parameter.name]
12
+ unless parameter.matches?(value)
13
+ @errors << "#{value.inspect} is not a valid value for the query parameter #{parameter.name.inspect}"
14
+ end
15
+ elsif parameter.required?
16
+ @errors << "Missing query parameter #{parameter.name.inspect}"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,6 +4,7 @@ module OpenapiContracts
4
4
  autoload :Documented, 'openapi_contracts/validators/documented'
5
5
  autoload :Headers, 'openapi_contracts/validators/headers'
6
6
  autoload :HttpStatus, 'openapi_contracts/validators/http_status'
7
+ autoload :Parameters, 'openapi_contracts/validators/parameters'
7
8
  autoload :RequestBody, 'openapi_contracts/validators/request_body'
8
9
  autoload :ResponseBody, 'openapi_contracts/validators/response_body'
9
10
  autoload :SchemaValidation, 'openapi_contracts/validators/schema_validation'
@@ -12,6 +13,7 @@ module OpenapiContracts
12
13
  ALL = [
13
14
  Documented,
14
15
  HttpStatus,
16
+ Parameters,
15
17
  RequestBody,
16
18
  ResponseBody,
17
19
  Headers
@@ -7,6 +7,7 @@ require 'active_support/core_ext/string'
7
7
  require 'rubygems/version'
8
8
 
9
9
  require 'json_schemer'
10
+ require 'openapi_parameters'
10
11
  require 'rack'
11
12
  require 'yaml'
12
13
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_contracts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.2
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mkon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-17 00:00:00.000000000 Z
11
+ date: 2024-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -50,6 +50,26 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '2.2'
53
+ - !ruby/object:Gem::Dependency
54
+ name: openapi_parameters
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.3.3
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.4'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 0.3.3
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '0.4'
53
73
  - !ruby/object:Gem::Dependency
54
74
  name: rack
55
75
  requirement: !ruby/object:Gem::Requirement
@@ -98,28 +118,28 @@ dependencies:
98
118
  requirements:
99
119
  - - '='
100
120
  - !ruby/object:Gem::Version
101
- version: 1.60.2
121
+ version: 1.64.1
102
122
  type: :development
103
123
  prerelease: false
104
124
  version_requirements: !ruby/object:Gem::Requirement
105
125
  requirements:
106
126
  - - '='
107
127
  - !ruby/object:Gem::Version
108
- version: 1.60.2
128
+ version: 1.64.1
109
129
  - !ruby/object:Gem::Dependency
110
130
  name: rubocop-rspec
111
131
  requirement: !ruby/object:Gem::Requirement
112
132
  requirements:
113
133
  - - '='
114
134
  - !ruby/object:Gem::Version
115
- version: 2.26.1
135
+ version: 2.31.0
116
136
  type: :development
117
137
  prerelease: false
118
138
  version_requirements: !ruby/object:Gem::Requirement
119
139
  requirements:
120
140
  - - '='
121
141
  - !ruby/object:Gem::Version
122
- version: 2.26.1
142
+ version: 2.31.0
123
143
  - !ruby/object:Gem::Dependency
124
144
  name: simplecov
125
145
  requirement: !ruby/object:Gem::Requirement
@@ -170,6 +190,7 @@ files:
170
190
  - lib/openapi_contracts/validators/documented.rb
171
191
  - lib/openapi_contracts/validators/headers.rb
172
192
  - lib/openapi_contracts/validators/http_status.rb
193
+ - lib/openapi_contracts/validators/parameters.rb
173
194
  - lib/openapi_contracts/validators/request_body.rb
174
195
  - lib/openapi_contracts/validators/response_body.rb
175
196
  - lib/openapi_contracts/validators/schema_validation.rb
@@ -196,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
217
  - !ruby/object:Gem::Version
197
218
  version: '0'
198
219
  requirements: []
199
- rubygems_version: 3.4.6
220
+ rubygems_version: 3.5.11
200
221
  signing_key:
201
222
  specification_version: 4
202
223
  summary: Openapi schemas as API contracts