openapi_first 2.11.0 → 2.11.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: 7687c4d4ac32654b4a2fd8880eeaeb0ca98ab6fac19b8d9a336fe5d5bb94785a
4
- data.tar.gz: 6fb04355d69916cfc3d1dce1e21d0f163082878cf1348421a9b1006b070621f3
3
+ metadata.gz: 0c84ae6fc48433fbbe1845ff66ed2315b58a47ff3ba17ea6756bb6c276db5ed3
4
+ data.tar.gz: 3b584da97ce9b7ea712dc66ac55e39a16c1631254dd719551d27a4df9bc13e4c
5
5
  SHA512:
6
- metadata.gz: '0988025dd93f55354b646c8aeb3fe7d0febfd54925d368fd3aa5959067c1a0a948b193158130b5fa7fb91cf51ffe774f9f8f281e76ac3feb318e67ea1b079020'
7
- data.tar.gz: f70094cd6b531e42e06f931ed31155385bf9dd97588cad6bc977e961e7fdd497e057a971112fe10a0457d145b8f916cabdad3995c90bc29d78d31e59184211ac
6
+ metadata.gz: 863d5ea4124958e81d62e7a72efef17e6fe5326fe4db6d34bc6fe97e8ec863c09dd8d138eaf9b249ce94ce490dad937297cd9d15b98dc73a6c01f30c6f6da2f7
7
+ data.tar.gz: 7c5c264ce6f969788986bbaf71cb6060b998ae5819cf5551ddf4f82ab2069c87afda22e97fa9c8aa03ea91bb6133cbf579d63b35a12c0eb30a7e17a774508ec7
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 2.11.1
6
+
7
+ - OpenapiFirst can now route requests correctly for paths like `/stuffs` and `/stuffs{format}` (https://github.com/ahx/openapi_first/issues/386)
8
+
5
9
  ## 2.11.0
6
10
 
7
11
  - OpenapiFirst::Test.observe now works with `Rack::URLMap` (returned by `Rack::Builder.app`) and probably all objects that respond to `.call`
data/README.md CHANGED
@@ -168,7 +168,7 @@ Here is how to set it up:
168
168
  end
169
169
  ```
170
170
  2. Observe your application. You can do this in multiple ways:
171
- - Add an `app` method to your tests, which wraps your application with silent request / response validation. (✷1)
171
+ - Add an `app` method to your tests (which is called by rack-test) that wraps your application with silent request / response validation.
172
172
  ```ruby
173
173
  module RequestSpecHelpers
174
174
  def app
@@ -188,18 +188,26 @@ Here is how to set it up:
188
188
  config.include OpenapiFirst::Test::Methods[MyApp], type: :request
189
189
  end
190
190
  ```
191
- 4. Run your tests. The Coverage feature will tell you about missing or invalid requests/responses.
191
+ 4. Run your tests. The Coverage feature will tell you about missing or invalid requests/responses:
192
+ ```
193
+ ✓ GET /stations
194
+ ✓ 200(application/json)
195
+ ❌ 200(application/xml) – No responses tracked!
196
+ ❌ 400(application/problem+json) – No responses tracked!
197
+ ```
192
198
 
193
- (✷1): It does not matter what method of openapi_first you use to validate requests/responses. Instead of using `OpenapiFirstTest.app` to wrap your application, you could also use the [middlewares](#rack-middlewares) or [test assertion method](#test-assertions), but you would have to do that for all requests/responses defined in your API description to make coverage work.
199
+ Now add tests for all those "❌" to make them "✓" and you're green!
200
+
201
+ > [!NOTE]
202
+ > Check out [faraday-openapi](https://codeberg.org/ahx/faraday-openapi) to have your API _client_ validate request/responses against an OAD, which is useful to validate HTTP mocks during testing.
194
203
 
195
204
  ### Configure test coverage
196
205
 
197
- OpenapiFirst::Test raises an error when a request is not defined. You can deactivate this with:
206
+ OpenapiFirst::Test raises an error when a response status is not defined. You can deactivate this with:
198
207
 
199
208
  ```ruby
200
209
  OpenapiFirst::Test.setup do |test|
201
- #
202
- test.ignore_unknown_requests = true
210
+ [403, 401].each { test.ignored_unknown_status << it }
203
211
  end
204
212
  ```
205
213
 
@@ -225,6 +233,15 @@ OpenapiFirst::Test.setup do |test|
225
233
  end
226
234
  ```
227
235
 
236
+ OpenapiFirst::Test raises an error when a request is not defined. You can deactivate this with:
237
+
238
+ ```ruby
239
+ OpenapiFirst::Test.setup do |test|
240
+ # …
241
+ test.ignore_unknown_requests = true
242
+ end
243
+ ```
244
+
228
245
  ### Test assertions
229
246
 
230
247
  openapi_first ships with a simple but powerful Test method to run request and response validation in your tests without using the middlewares. This is designed to be used with rack-test or Ruby on Rails integration tests or request specs.
@@ -14,7 +14,7 @@ module OpenapiFirst
14
14
  @request_validation_error_response = OpenapiFirst.find_error_response(:default)
15
15
  @request_validation_raise_error = false
16
16
  @response_validation_raise_error = true
17
- @hooks = (HOOKS.map { [_1, Set.new] }).to_h
17
+ @hooks = HOOKS.to_h { [_1, Set.new] }
18
18
  @path = nil
19
19
  end
20
20
 
@@ -11,7 +11,7 @@ module OpenapiFirst
11
11
  def self.call(responses, status, content_type, request_method:, path:)
12
12
  contents = find_status(responses, status)
13
13
  if contents.nil?
14
- message = "Status #{status} is not defined for #{request_method} #{path}. " \
14
+ message = "Status #{status} is not defined for #{request_method.upcase} #{path}. " \
15
15
  "Defined statuses are: #{responses.keys.join(', ')}."
16
16
  return Match.new(error: Failure.new(:response_not_found, message:), response: nil)
17
17
  end
@@ -97,10 +97,15 @@ module OpenapiFirst
97
97
  found = @static[request_path]
98
98
  return [found, {}] if found
99
99
 
100
- @dynamic.find do |_path, path_item|
100
+ matches = @dynamic.filter_map do |_path, path_item|
101
101
  params = path_item[:template].match(request_path)
102
- return [path_item, params] if params
102
+ next unless params
103
+
104
+ [path_item, params]
103
105
  end
106
+ return matches.first if matches.length == 1
107
+
108
+ matches&.min_by { |match| match[1].values.sum(&:length) }
104
109
  end
105
110
  end
106
111
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
- VERSION = '2.11.0'
4
+ VERSION = '2.11.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_first
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.11.0
4
+ version: 2.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller