Skip to content

Commit 4c547a0

Browse files
committed
Update readme for 3.0
1 parent 1c38387 commit 4c547a0

1 file changed

Lines changed: 50 additions & 54 deletions

File tree

README.md

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ openapi_first is a Ruby gem for request / response validation and contract-testi
66

77
Configure
88
```ruby
9-
OpenapiFirst.configure do |config|
10-
config.register('openapi/openapi.yaml')
11-
end
9+
OpenapiFirst.register('openapi/openapi.yaml')
1210
```
1311

1412
Use an OAD to validate incoming requests:
@@ -20,9 +18,7 @@ Turn your request tests into [contract tests](#contract-testing) against an OAD:
2018
```ruby
2119
# spec_helper.rb
2220
require 'openapi_first'
23-
OpenapiFirst::Test.setup do |test|
24-
test.register('openapi/openapi.yaml')
25-
end
21+
OpenapiFirst::Test.setup
2622

2723
require 'my_app'
2824
RSpec.configure do |config|
@@ -34,14 +30,14 @@ end
3430

3531
<!-- TOC -->
3632

33+
- [Configuration](#configuration)
3734
- [Rack Middlewares](#rack-middlewares)
3835
- [Request validation](#request-validation)
3936
- [Response validation](#response-validation)
4037
- [Contract testing](#contract-testing)
4138
- [Test assertions](#test-assertions)
4239
- [Manual use](#manual-use)
4340
- [Framework integration](#framework-integration)
44-
- [Configuration](#configuration)
4541
- [Hooks](#hooks)
4642
- [Alternatives](#alternatives)
4743
- [Frequently Asked Questions](#frequently-asked-questions)
@@ -51,17 +47,44 @@ end
5147

5248
<!-- /TOC -->
5349

50+
## Configuration
51+
52+
You should register OADs globally so you don't have to load the file multiple times or to refernce them by Symbol (like :v1 in this example).
53+
```ruby
54+
OpenapiFirst.configure do |config|
55+
config.register('openapi/openapi.yaml') # :default
56+
config.register('openapi/v1.openapi.yaml', as: :v1)
57+
end
58+
```
59+
60+
You can configure default options globally:
61+
62+
```ruby
63+
OpenapiFirst.configure do |config|
64+
# Specify which plugin is used to render error responses returned by the request validation middleware (defaults to :default)
65+
config.request_validation_error_response = :jsonapi
66+
end
67+
```
68+
69+
or configure per instance:
70+
71+
```ruby
72+
OpenapiFirst.load('openapi.yaml') do |config|
73+
config.request_validation_error_response = :jsonapi
74+
end
75+
```
76+
5477
## Rack Middlewares
5578

5679
### Request validation
5780

5881
The request validation middleware returns a 4xx if the request is invalid or not defined in the API description. It adds a request object to the current Rack environment at `env[OpenapiFirst::REQUEST]` with the request parameters parsed exactly as described in your API description plus access to meta information from your API description. See _[Manual use](#manual-use)_ for more details about that object.
5982

6083
```ruby
61-
use OpenapiFirst::Middlewares::RequestValidation, 'openapi.yaml'
84+
use OpenapiFirst::Middlewares::RequestValidation
6285

6386
# Pass `raise_error: true` to raise an error if request is invalid:
64-
use OpenapiFirst::Middlewares::RequestValidation, 'openapi.yaml', raise_error: true
87+
use OpenapiFirst::Middlewares::RequestValidation, raise_error: true
6588
```
6689

6790
#### Error responses
@@ -152,10 +175,10 @@ This middleware raises an error by default if the response is not valid.
152175
This can be useful in a test or staging environment, especially if you are adopting OpenAPI for an existing implementation.
153176

154177
```ruby
155-
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml' if ENV['RACK_ENV'] == 'test'
178+
use OpenapiFirst::Middlewares::ResponseValidation if ENV['RACK_ENV'] == 'test'
156179

157180
# Pass `raise_error: false` to not raise an error:
158-
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml', raise_error: false
181+
use OpenapiFirst::Middlewares::ResponseValidation, raise_error: false
159182
```
160183

161184
If you are adopting OpenAPI you can use these options together with [hooks](#hooks) to get notified about requests/responses that do match your API description.
@@ -166,13 +189,11 @@ You can see your OpenAPI API description as a contract that your clients can rel
166189

167190
Here is how to set it up:
168191

169-
1. Register all OpenAPI documents to track coverage for.
170-
This should go at the top of your test helper file before loading your application code.
192+
1. Setup the test mode
171193
```ruby
194+
# spec_helper.rb
172195
require 'openapi_first'
173-
OpenapiFirst::Test.setup do |config|
174-
config.register('openapi/openapi.yaml')
175-
end
196+
OpenapiFirst::Test.setup
176197
```
177198
2. Observe your application. You can do this in multiple ways:
178199
- Add an `app` method to your tests (which is called by rack-test) that wraps your application with silent request / response validation.
@@ -195,7 +216,7 @@ Here is how to set it up:
195216
config.include OpenapiFirst::Test::Methods[MyApp], type: :request
196217
end
197218
```
198-
4. Run your tests. The Coverage feature will tell you about missing or invalid requests/responses:
219+
3. Run your tests. The Coverage feature will tell you about missing or invalid requests/responses:
199220
```
200221
GET /stations
201222
200(application/json)
@@ -210,19 +231,18 @@ Here is how to set it up:
210231
211232
### Configure test coverage
212233
213-
OpenapiFirst::Test raises an error when a response status is not defined. You can deactivate this with:
234+
OpenapiFirst::Test raises an error when a response status is not defined except for 404 and 500. You can change this:
214235
215236
```ruby
216237
OpenapiFirst::Test.setup do |test|
217-
[403, 401].each { test.ignored_unknown_status << it }
238+
test.ignored_unknown_status << 403
218239
end
219240
```
220241
221242
Or you can ignore all unknown response status:
222243
223244
```ruby
224245
OpenapiFirst::Test.setup do |test|
225-
# …
226246
test.ignore_all_unknown_status = true
227247
end
228248
```
@@ -231,7 +251,6 @@ Exclude certain _responses_ from coverage with `skip_coverage`:
231251
232252
```ruby
233253
OpenapiFirst::Test.setup do |test|
234-
# …
235254
test.skip_response_coverage do |response_definition|
236255
response_definition.status == '5XX'
237256
end
@@ -242,7 +261,6 @@ Skip coverage for a request and all responses alltogether of a route with `skip_
242261
243262
```ruby
244263
OpenapiFirst::Test.setup do |test|
245-
# …
246264
test.skip_coverage do |path, request_method|
247265
path == '/bookings/{bookingId}' && requests_method == 'DELETE'
248266
end
@@ -253,7 +271,6 @@ OpenapiFirst::Test raises an error when a request is not defined. You can deacti
253271
254272
```ruby
255273
OpenapiFirst::Test.setup do |test|
256-
# …
257274
test.ignore_unknown_requests = true
258275
end
259276
```
@@ -267,23 +284,23 @@ openapi_first ships with a simple but powerful Test method `assert_api_conform`
267284

268285
Here is how to use it with RSpec, but MiniTest works just as good:
269286

287+
```ruby
288+
# spec_helper.rb
289+
OpenapiFirst::Test.setup do |test|
290+
test.register(File.join(__dir__, '../examples/openapi.yaml'), as: :example_app)
291+
end
292+
```
293+
294+
270295
Inside your test :
271296
```ruby
272297
RSpec.describe 'Example App' do
273298
include Rack::Test::Methods
274-
include OpenapiFirst::Test::Methods
275-
276-
before do
277-
OpenapiFirst::Test.register(File.join(__dir__, '../examples/openapi.yaml'), as: :example_app)
278-
end
279-
280-
def app
281-
App
282-
end
299+
include OpenapiFirst::Test::Methods[App]
283300
284301
it 'is API conform' do
285302
get '/'
286-
assert_api_conform(status: 200, api: :example_app)
303+
assert_api_conform(status: 200)
287304
end
288305
end
289306
```
@@ -341,27 +358,6 @@ validated_response.parsed_headers
341358
definition.validate_response(rack_request,rack_response, raise_error: true) # Raises OpenapiFirst::ResponseInvalidError or OpenapiFirst::ResponseNotFoundError
342359
```
343360

344-
## Configuration
345-
346-
You can configure default options globally:
347-
348-
```ruby
349-
OpenapiFirst.configure do |config|
350-
# Specify which plugin is used to render error responses returned by the request validation middleware (defaults to :default)
351-
config.request_validation_error_response = :jsonapi
352-
# Configure if the request validation middleware should raise an exception (defaults to false)
353-
config.request_validation_raise_error = true
354-
end
355-
```
356-
357-
or configure per instance:
358-
359-
```ruby
360-
OpenapiFirst.load('openapi.yaml') do |config|
361-
config.request_validation_error_response = :jsonapi
362-
end
363-
```
364-
365361
## Hooks
366362

367363
You can integrate your code at certain points during request/response validation via hooks.

0 commit comments

Comments
 (0)