|
1 | 1 | # openapi_first |
2 | 2 |
|
3 | | -openapi_first is a Ruby gem for request / response validation and contract-testing against an [OpenAPI](https://www.openapis.org/) 3.0 or 3.1 API description. It makes an APIFirst workflow easy and reliable. |
| 3 | +openapi_first is a Ruby gem for request / response validation and contract-testing against an [OpenAPI](https://www.openapis.org/) 3.0 or 3.1 Openapi API description (OAD). It makes an APIFirst workflow easy and reliable. |
4 | 4 |
|
5 | | -You can use openapi_first on production for [request validation](#request-validation) and in your [tests](#contract-testing) to avoid API drift with it's request/response validation and coverage features. |
| 5 | +## Usage |
| 6 | + |
| 7 | +Use an OAD to validate incoming requests in production: |
| 8 | +```ruby |
| 9 | +use OpenapiFirst::Middlewares::RequestValidation, 'openapi/openapi.yaml' |
| 10 | +``` |
| 11 | + |
| 12 | +Turn your request tests into contract tests against an OAD: |
| 13 | +```ruby |
| 14 | +# spec_helper.rb |
| 15 | +require 'openapi_first' |
| 16 | +OpenapiFirst::Test.setup do |config| |
| 17 | + config.register('openapi/openapi.yaml') |
| 18 | +end |
| 19 | +require 'application' # Load Application code |
| 20 | +OpenapiFirst::Test.observe(Application) |
| 21 | +``` |
6 | 22 |
|
7 | 23 | ## Contents |
8 | 24 |
|
@@ -37,23 +53,26 @@ Here is how to set it up: |
37 | 53 | require 'openapi_first' |
38 | 54 | OpenapiFirst::Test.setup do |config| |
39 | 55 | config.register('openapi/openapi.yaml') |
40 | | - # Optional: Skip certain responses, which are described in your API description, but need no test coverage |
41 | | - config.skip_response_coverage { |response_definition| response_definition.status.to_s == '500' } |
42 | | - end |
43 | | - ``` |
44 | | -2. Add an `app` method to your tests by including a Module. This `app` method wraps your application with silent request / response validation. This validates all requests/responses in your test run. (✷1) |
45 | | - ```ruby |
46 | | - RSpec.configure do |config| |
47 | | - config.include OpenapiFirst::Test::Methods[MyApp], type: :request |
48 | | - end |
49 | | - ``` |
50 | | - Or add the `app` method yourself: |
51 | | - |
52 | | - ```ruby |
53 | | - def app |
54 | | - OpenapiFirst::Test.app(MyApp) |
55 | 56 | end |
56 | 57 | ``` |
| 58 | +2. Observe your application. You can do this in one of two ways: |
| 59 | + - Inject a Module to wrap (prepend) the `call` method of your Rack app Class. |
| 60 | + ```ruby |
| 61 | + OpenapiFirst::Test.observe(MyApplication) |
| 62 | + ``` |
| 63 | + - Or add an `app` method to your tests, which wraps your application with silent request / response validation. (✷1) |
| 64 | + ```ruby |
| 65 | + RSpec.configure do |config| |
| 66 | + config.include OpenapiFirst::Test::Methods[MyApp], type: :request |
| 67 | + end |
| 68 | + ``` |
| 69 | + Or add the `app` method yourself: |
| 70 | + |
| 71 | + ```ruby |
| 72 | + def app |
| 73 | + OpenapiFirst::Test.app(MyApp) |
| 74 | + end |
| 75 | + ``` |
57 | 76 | 3. Run your tests. The Coverage feature will tell you about missing or invalid requests/responses. |
58 | 77 |
|
59 | 78 | (✷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. |
|
0 commit comments