openapi_first 0.11.0.alpha → 0.11.0
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 +4 -4
- data/.rubocop.yml +6 -0
- data/CHANGELOG.md +4 -1
- data/Gemfile.lock +5 -4
- data/README.md +105 -81
- data/benchmarks/Gemfile.lock +2 -2
- data/lib/openapi_first.rb +4 -3
- data/lib/openapi_first/app.rb +3 -7
- data/lib/openapi_first/request_validation.rb +4 -1
- data/lib/openapi_first/{operation_resolver.rb → responder.rb} +8 -1
- data/lib/openapi_first/response_validation.rb +3 -58
- data/lib/openapi_first/router.rb +1 -0
- data/lib/openapi_first/router_required.rb +13 -0
- data/lib/openapi_first/version.rb +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a7011597803fb49d100073027c9545634d497971b34ac49c2eeb0916258f424
|
4
|
+
data.tar.gz: 1f552e13bd915d41d39e2fffcc7a026b5abd3ba5c19ed4f726e3538c68a893df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50053573288bde3ce62df411c706dc9fe64a4395b7110a539cf4e1b685b1953140cdbc8d9ad7e7d8b0eee4d9b4211243f7c3e85ef6b79a44720f9b56ae316c3f
|
7
|
+
data.tar.gz: 017b082c94490f45cc0cb90935c0f5a463958740d858b56f2d6baf1b2e579f9e5820acda5cc5f53c0d9fe50cc261600a1f8c93c498f165997f026f68617c888f
|
data/.rubocop.yml
CHANGED
@@ -16,6 +16,12 @@ Lint/DeprecatedOpenSSLConstant:
|
|
16
16
|
Enabled: true
|
17
17
|
Lint/RaiseException:
|
18
18
|
Enabled: true
|
19
|
+
Lint/MixedRegexpCaptureTypes:
|
20
|
+
Enabled: true
|
21
|
+
Style/RedundantRegexpCharacterClass:
|
22
|
+
Enabled: true
|
23
|
+
Style/RedundantRegexpEscape:
|
24
|
+
Enabled: true
|
19
25
|
Style/SlicingWithRange:
|
20
26
|
Enabled: true
|
21
27
|
Lint/StructNewOverride:
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
##
|
3
|
+
## 0.11.0
|
4
|
+
- Raise error if you forgot to add the Router middleware
|
5
|
+
- Make OpenapiFirst.app raise an error in test env when request path is not specified
|
6
|
+
- Rename OperationResolver to Responder
|
4
7
|
- Add ResponseValidation middleware that validates the response body
|
5
8
|
- Add `raise` option to Router middleware to raise an error if request could not be found in the API description similar to committee's raise option.
|
6
9
|
- Move namespace option from Router to OperationResolver
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
openapi_first (0.11.0
|
4
|
+
openapi_first (0.11.0)
|
5
5
|
deep_merge (>= 1.2.1)
|
6
6
|
hanami-router (~> 2.0.alpha3)
|
7
7
|
hanami-utils (~> 2.0.alpha1)
|
@@ -23,7 +23,7 @@ GEM
|
|
23
23
|
public_suffix (>= 2.0.2, < 5.0)
|
24
24
|
ast (2.4.0)
|
25
25
|
builder (3.2.4)
|
26
|
-
coderay (1.1.
|
26
|
+
coderay (1.1.3)
|
27
27
|
concurrent-ruby (1.1.6)
|
28
28
|
deep_merge (1.2.1)
|
29
29
|
diff-lcs (1.3)
|
@@ -66,7 +66,7 @@ GEM
|
|
66
66
|
mustermann-contrib (~> 1.1.1)
|
67
67
|
nokogiri
|
68
68
|
parallel (1.19.1)
|
69
|
-
parser (2.7.1.
|
69
|
+
parser (2.7.1.3)
|
70
70
|
ast (~> 2.4.0)
|
71
71
|
pry (0.13.1)
|
72
72
|
coderay (~> 1.1)
|
@@ -92,10 +92,11 @@ GEM
|
|
92
92
|
diff-lcs (>= 1.2.0, < 2.0)
|
93
93
|
rspec-support (~> 3.9.0)
|
94
94
|
rspec-support (3.9.3)
|
95
|
-
rubocop (0.
|
95
|
+
rubocop (0.85.0)
|
96
96
|
parallel (~> 1.10)
|
97
97
|
parser (>= 2.7.0.1)
|
98
98
|
rainbow (>= 2.2.2, < 4.0)
|
99
|
+
regexp_parser (>= 1.7)
|
99
100
|
rexml
|
100
101
|
rubocop-ast (>= 0.0.3)
|
101
102
|
ruby-progressbar (~> 1.7)
|
data/README.md
CHANGED
@@ -1,42 +1,130 @@
|
|
1
1
|
# OpenapiFirst
|
2
2
|
|
3
|
+
[](https://gitter.im/openapi_first/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4
|
+
|
3
5
|
OpenapiFirst helps to implement HTTP APIs based on an [OpenApi](https://www.openapis.org/) API description. The idea is that you create an API description first, then add code that returns data and implements your business logic and be done.
|
4
6
|
|
5
|
-
Start with writing an OpenAPI file that describes the API, which you are about to
|
7
|
+
Start with writing an OpenAPI file that describes the API, which you are about to implement. Use a [validator](https://github.com/stoplightio/spectral/) to make sure the file is valid.
|
8
|
+
|
9
|
+
You can use OpenapiFirst via its [Rack middlewares](#rack-middlewares) or in [standalone mode](#standalone-usage).
|
6
10
|
|
7
11
|
## Rack middlewares
|
8
12
|
OpenapiFirst consists of these Rack middlewares:
|
9
13
|
|
10
|
-
- `OpenapiFirst::Router` – Finds the operation for the current request or returns 404 if no operation was found. This can be customized.
|
11
|
-
- `OpenapiFirst::RequestValidation` – Validates the request against the API description and returns 400 if the request is invalid.
|
12
|
-
- `OpenapiFirst::
|
14
|
+
- [`OpenapiFirst::Router`](#OpenapiFirst::Router) – Finds the OpenAPI operation for the current request or returns 404 if no operation was found. This can be customized.
|
15
|
+
- [`OpenapiFirst::RequestValidation`](#OpenapiFirst::RequestValidation) – Validates the request against the API description and returns 400 if the request is invalid.
|
16
|
+
- [`OpenapiFirst::Responder`](#OpenapiFirst::Responder) calls the [handler](#handlers) found for the operation.
|
17
|
+
- [`OpenapiFirst::ResponseValidation`](#OpenapiFirst::ResponseValidation) Validates the response and raises an exception if the response body is invalid.
|
13
18
|
|
14
19
|
## OpenapiFirst::Router
|
20
|
+
You always have to add this middleware first in order to make the other middlewares work.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
use OpenapiFirst::Router, spec: OpenapiFirst.load('./openapi/openapi.yaml')
|
24
|
+
```
|
25
|
+
|
26
|
+
This middleware adds `env[OpenapiFirst::OPERATION]` which holds an Operation object that responds to `operation_id` and `path`.
|
27
|
+
|
15
28
|
Options and their defaults:
|
16
29
|
|
17
30
|
| Name | Possible values | Description | Default
|
18
31
|
|:---|---|---|---|
|
19
|
-
|
32
|
+
|`spec:`| | The spec loaded via `OpenapiFirst.load` ||
|
33
|
+
| `not_found:` |`nil`, `:continue`, `Proc`| Specifies what to do if the path was not found in the API description. `nil` (default) returns a 404 response. `:continue` does nothing an calls the next app. `Proc` (or something that responds to `call`) to customize the response. | `nil` (return 404)
|
20
34
|
| `raise:` |`false`, `true` | If set to true the middleware raises `OpenapiFirst::NotFoundError` when a path or method was not found in the API description. This is useful during testing to spot an incomplete API description. | `false` (don't raise an exception)
|
21
35
|
|
36
|
+
## OpenapiFirst::RequestValidation
|
22
37
|
|
23
|
-
|
24
|
-
If you just want to use the request validation part without any handlers you can use the rack middlewares standalone:
|
38
|
+
This middleware returns a 400 status code with a body that describes the error if the request is not valid.
|
25
39
|
|
26
40
|
```ruby
|
27
|
-
use OpenapiFirst::Router, spec: OpenapiFirst.load('./openapi/openapi.yaml')
|
28
41
|
use OpenapiFirst::RequestValidation
|
29
42
|
```
|
30
43
|
|
31
|
-
|
32
|
-
|
44
|
+
The error responses conform with [JSON:API](https://jsonapi.org).
|
45
|
+
|
46
|
+
Here's an example response body for a missing query parameter "search":
|
47
|
+
|
48
|
+
```json
|
49
|
+
http-status: 400
|
50
|
+
content-type: "application/vnd.api+json"
|
51
|
+
|
52
|
+
{
|
53
|
+
"errors": [
|
54
|
+
{
|
55
|
+
"title": "is missing",
|
56
|
+
"source": {
|
57
|
+
"parameter": "search"
|
58
|
+
}
|
59
|
+
}
|
60
|
+
]
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
This middleware adds `env[OpenapiFirst::INBOX]` which holds the (filtered) path and query parameters and the parsed request body.
|
65
|
+
|
66
|
+
### Parameter validation
|
67
|
+
|
68
|
+
The middleware filteres all top-level query parameters and paths parameters and tries to convert numeric values. Meaning, if you have an `:something_id` path with `type: integer`, it will try convert the value to an integer.
|
69
|
+
Note that is currently does not convert date, date-time or time formats and that conversion is currently done only for path and query parameters, but not for the request body. It just works with a parameter with `name: filter[age]`.
|
70
|
+
|
71
|
+
If you want to forbid _nested_ query parameters you will need to use [`additionalProperties: false`](https://json-schema.org/understanding-json-schema/reference/object.html#properties) in your query parameter JSON schema.
|
72
|
+
|
73
|
+
_OpenapiFirst always treats query parameters like [`style: deepObject`](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#style-values), **but** it just works with nested objects (`filter[foo][bar]=baz`) (see [this discussion](https://github.com/OAI/OpenAPI-Specification/issues/1706))._
|
74
|
+
|
75
|
+
### Request body validation
|
76
|
+
|
77
|
+
The middleware will return a status `415` if the requests content type does not match or `400` if the request body is invalid.
|
78
|
+
This will also add the parsed request body to `env[OpenapiFirst::REQUEST_BODY]`.
|
79
|
+
|
80
|
+
### Header, Cookie, Path parameter validation
|
81
|
+
|
82
|
+
tbd.
|
83
|
+
|
84
|
+
## OpenapiFirst::Responder
|
85
|
+
|
86
|
+
This Rack endpoint maps the HTTP request to a method call based on the [operationId](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operation-object) in your API description and calls it. Responder also adds a content-type to the response.
|
87
|
+
|
88
|
+
Currently there are no customization options for this part. Please [share your ideas](#contributing) on how to best meet your needs and preferred style.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
run OpenapiFirst::Responder, spec: OpenapiFirst.load('./openapi/openapi.yaml')
|
92
|
+
```
|
93
|
+
|
94
|
+
| Name | Description
|
95
|
+
|:---|---|
|
96
|
+
|`spec:`| The spec loaded via `OpenapiFirst.load` |
|
97
|
+
| `namespace:` | A class or module where to find the handler method. |
|
98
|
+
|
99
|
+
It works like this:
|
100
|
+
|
101
|
+
- An operationId "create_pet" or "createPet" or "create pet" calls `MyApi.create_pet(params, response)`
|
102
|
+
- "some_things.create" calls: `MyApi::SomeThings.create(params, response)`
|
103
|
+
- "pets#create" calls: `MyApi::Pets::Create.new.call(params, response)` If `MyApi::Pets::Create.new` accepts an argument, it will pass the rack `env`.
|
104
|
+
|
105
|
+
### Handlers
|
106
|
+
|
107
|
+
These handler methods are called with two arguments:
|
108
|
+
|
109
|
+
- `params` - Holds the parsed request body, filtered query params and path parameters (same as `env[OpenapiFirst::INBOX]`)
|
110
|
+
- `res` - Holds a Rack::Response that you can modify if needed
|
111
|
+
|
112
|
+
You can call `params.env` to access the Rack env (just like in [Hanami actions](https://guides.hanamirb.org/actions/parameters/))
|
113
|
+
|
114
|
+
There are two ways to set the response body:
|
115
|
+
|
116
|
+
- Calling `res.write "things"` (see [Rack::Response](https://www.rubydoc.info/github/rack/rack/Rack/Response))
|
117
|
+
- Returning a value which will get converted to JSON
|
33
118
|
|
34
|
-
|
35
|
-
|
119
|
+
## OpenapiFirst::ResponseValidation
|
120
|
+
This middleware is especially useful when testing. It raises an error if the response is not valid.
|
36
121
|
|
122
|
+
```ruby
|
123
|
+
use OpenapiFirst::ResponseValidation if ENV['RACK_ENV'] == 'test'
|
124
|
+
```
|
37
125
|
|
38
126
|
## Standalone usage
|
39
|
-
|
127
|
+
Instead of composing these middlewares yourself you can use `OpenapiFirst.app`.
|
40
128
|
|
41
129
|
```ruby
|
42
130
|
module Pets
|
@@ -65,29 +153,7 @@ Handler functions (`find_pet`) are called with two arguments:
|
|
65
153
|
- `res` - Holds a Rack::Response that you can modify if needed
|
66
154
|
If you want to access to plain Rack env you can call `params.env`.
|
67
155
|
|
68
|
-
|
69
|
-
|
70
|
-
OpenapiFirst maps the HTTP request to a method call based on the [operationId](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operation-object) in your API description and calls it via the `OperationResolver` middleware.
|
71
|
-
|
72
|
-
It works like this:
|
73
|
-
|
74
|
-
- "create_pet" or "createPet" or "create pet" calls `MyApi.create_pet(params, response)`
|
75
|
-
- "some_things.create" calls: `MyApi::SomeThings.create(params, response)`
|
76
|
-
- "pets#create" calls: `MyApi::Pets::Create.new.call(params, response)` If `MyApi::Pets::Create.new` accepts an argument, it will pass the rack `env`.
|
77
|
-
|
78
|
-
These handler methods are called with two arguments:
|
79
|
-
|
80
|
-
- `params` - Holds the parsed request body, filtered query params and path parameters
|
81
|
-
- `res` - Holds a Rack::Response that you can modify if needed
|
82
|
-
|
83
|
-
You can call `params.env` to access the Rack env (just like in [Hanami actions](https://guides.hanamirb.org/actions/parameters/))
|
84
|
-
|
85
|
-
There are two ways to set the response body:
|
86
|
-
|
87
|
-
- Calling `res.write "things"` (see [Rack::Response](https://www.rubydoc.info/github/rack/rack/Rack/Response))
|
88
|
-
- Returning a value from the function (see example above) (this will always converted to JSON)
|
89
|
-
|
90
|
-
### If your API description does not contain all endpoints
|
156
|
+
## If your API description does not contain all endpoints
|
91
157
|
|
92
158
|
```ruby
|
93
159
|
run OpenapiFirst.middleware('./openapi/openapi.yaml', namespace: Pets)
|
@@ -95,7 +161,7 @@ run OpenapiFirst.middleware('./openapi/openapi.yaml', namespace: Pets)
|
|
95
161
|
|
96
162
|
Here all requests that are not part of the API description will be passed to the next app.
|
97
163
|
|
98
|
-
|
164
|
+
## Try it out
|
99
165
|
|
100
166
|
See [examples](examples).
|
101
167
|
|
@@ -109,49 +175,7 @@ gem 'openapi_first'
|
|
109
175
|
|
110
176
|
OpenapiFirst uses [`multi_json`](https://rubygems.org/gems/multi_json).
|
111
177
|
|
112
|
-
##
|
113
|
-
|
114
|
-
If the request is not valid, these middlewares return a 400 status code with a body that describes the error.
|
115
|
-
|
116
|
-
The error responses conform with [JSON:API](https://jsonapi.org).
|
117
|
-
|
118
|
-
Here's an example response body for a missing query parameter "search":
|
119
|
-
|
120
|
-
```json
|
121
|
-
http-status: 400
|
122
|
-
content-type: "application/vnd.api+json"
|
123
|
-
|
124
|
-
{
|
125
|
-
"errors": [
|
126
|
-
{
|
127
|
-
"title": "is missing",
|
128
|
-
"source": {
|
129
|
-
"parameter": "search"
|
130
|
-
}
|
131
|
-
}
|
132
|
-
]
|
133
|
-
}
|
134
|
-
```
|
135
|
-
|
136
|
-
### Parameter validation
|
137
|
-
|
138
|
-
The middleware filteres all top-level query parameters and paths parameters and tries to convert numeric values. Meaning, if you have an `:something_id` path with `type: integer`, it will try convert the value to an integer.
|
139
|
-
Note that is currently does not convert date, date-time or time formats and that conversion is currently on done for path and query parameters, but not for request bodies.
|
140
|
-
|
141
|
-
If you want to forbid _nested_ query parameters you will need to use [`additionalProperties: false`](https://json-schema.org/understanding-json-schema/reference/object.html#properties) in your query parameter JSON schema.
|
142
|
-
|
143
|
-
_OpenapiFirst always treats query parameters like [`style: deepObject`](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#style-values), **but** it just works with nested objects (`filter[foo][bar]=baz`) (see [this discussion](https://github.com/OAI/OpenAPI-Specification/issues/1706))._
|
144
|
-
|
145
|
-
### Request body validation
|
146
|
-
|
147
|
-
The middleware will return a `415` if the requests content type does not match or `400` if the request body is invalid.
|
148
|
-
This will add the parsed request body to `env[OpenapiFirst::REQUEST_BODY]`.
|
149
|
-
|
150
|
-
### Header, Cookie, Path parameter validation
|
151
|
-
|
152
|
-
tbd.
|
153
|
-
|
154
|
-
## Response validation
|
178
|
+
## Manual response validation
|
155
179
|
|
156
180
|
Response validation is useful to make sure your app responds as described in your API description. You usually do this in your tests using [rack-test](https://github.com/rack-test/rack-test).
|
157
181
|
|
@@ -234,6 +258,6 @@ bundle exec ruby benchmarks.rb
|
|
234
258
|
|
235
259
|
## Contributing
|
236
260
|
|
237
|
-
If you have a question or an idea or found a bug don't hesitate to [create an issue on GitHub](https://github.com/ahx/openapi_first/issues).
|
261
|
+
If you have a question or an idea or found a bug don't hesitate to [create an issue on GitHub](https://github.com/ahx/openapi_first/issues) or [reach out via chat](https://gitter.im/openapi_first/community).
|
238
262
|
|
239
263
|
Pull requests are very welcome as well, of course. Feel free to create a "draft" pull request early on, even if your change is still work in progress. 🤗
|
data/benchmarks/Gemfile.lock
CHANGED
data/lib/openapi_first.rb
CHANGED
@@ -9,7 +9,7 @@ require 'openapi_first/router'
|
|
9
9
|
require 'openapi_first/request_validation'
|
10
10
|
require 'openapi_first/response_validator'
|
11
11
|
require 'openapi_first/response_validation'
|
12
|
-
require 'openapi_first/
|
12
|
+
require 'openapi_first/responder'
|
13
13
|
require 'openapi_first/app'
|
14
14
|
|
15
15
|
module OpenapiFirst
|
@@ -29,12 +29,13 @@ module OpenapiFirst
|
|
29
29
|
|
30
30
|
def self.app(spec, namespace:)
|
31
31
|
spec = OpenapiFirst.load(spec) if spec.is_a?(String)
|
32
|
-
|
32
|
+
test = ENV['RACK_ENV'] == 'test'
|
33
|
+
App.new(nil, spec, namespace: namespace, router_raise: test)
|
33
34
|
end
|
34
35
|
|
35
36
|
def self.middleware(spec, namespace:)
|
36
37
|
spec = OpenapiFirst.load(spec) if spec.is_a?(String)
|
37
|
-
AppWithOptions.new(spec, namespace: namespace)
|
38
|
+
AppWithOptions.new(spec, namespace: namespace, router_raise: false)
|
38
39
|
end
|
39
40
|
|
40
41
|
class AppWithOptions
|
data/lib/openapi_first/app.rb
CHANGED
@@ -5,16 +5,12 @@ require 'logger'
|
|
5
5
|
|
6
6
|
module OpenapiFirst
|
7
7
|
class App
|
8
|
-
def initialize(
|
9
|
-
parent_app,
|
10
|
-
spec,
|
11
|
-
namespace:
|
12
|
-
)
|
8
|
+
def initialize(parent_app, spec, namespace:, router_raise:)
|
13
9
|
@stack = Rack::Builder.app do
|
14
10
|
freeze_app
|
15
|
-
use OpenapiFirst::Router, spec: spec, parent_app: parent_app
|
11
|
+
use OpenapiFirst::Router, spec: spec, raise: router_raise, parent_app: parent_app
|
16
12
|
use OpenapiFirst::RequestValidation
|
17
|
-
run OpenapiFirst::
|
13
|
+
run OpenapiFirst::Responder.new(
|
18
14
|
spec: spec,
|
19
15
|
namespace: namespace
|
20
16
|
)
|
@@ -4,11 +4,14 @@ require 'rack'
|
|
4
4
|
require 'json_schemer'
|
5
5
|
require 'multi_json'
|
6
6
|
require_relative 'inbox'
|
7
|
+
require_relative 'router_required'
|
7
8
|
require_relative 'validation_format'
|
8
9
|
|
9
10
|
module OpenapiFirst
|
10
11
|
class RequestValidation # rubocop:disable Metrics/ClassLength
|
11
|
-
|
12
|
+
prepend RouterRequired
|
13
|
+
|
14
|
+
def initialize(app)
|
12
15
|
@app = app
|
13
16
|
end
|
14
17
|
|
@@ -5,7 +5,7 @@ require_relative 'inbox'
|
|
5
5
|
require_relative 'find_handler'
|
6
6
|
|
7
7
|
module OpenapiFirst
|
8
|
-
class
|
8
|
+
class Responder
|
9
9
|
def initialize(spec:, namespace:)
|
10
10
|
@handlers = FindHandler.new(spec, namespace).all
|
11
11
|
@namespace = namespace
|
@@ -29,4 +29,11 @@ module OpenapiFirst
|
|
29
29
|
MultiJson.dump(result)
|
30
30
|
end
|
31
31
|
end
|
32
|
+
|
33
|
+
class OperationResolver < Responder
|
34
|
+
def initialize(spec:, namespace:)
|
35
|
+
warn "#{self.class.name} was renamed to #{OpenapiFirst::Responder.name}"
|
36
|
+
super
|
37
|
+
end
|
38
|
+
end
|
32
39
|
end
|
@@ -2,10 +2,13 @@
|
|
2
2
|
|
3
3
|
require 'json_schemer'
|
4
4
|
require 'multi_json'
|
5
|
+
require_relative 'router_required'
|
5
6
|
require_relative 'validation'
|
6
7
|
|
7
8
|
module OpenapiFirst
|
8
9
|
class ResponseValidation
|
10
|
+
prepend RouterRequired
|
11
|
+
|
9
12
|
def initialize(app)
|
10
13
|
@app = app
|
11
14
|
end
|
@@ -56,61 +59,3 @@ module OpenapiFirst
|
|
56
59
|
end
|
57
60
|
end
|
58
61
|
end
|
59
|
-
|
60
|
-
# frozen_string_literal: true
|
61
|
-
|
62
|
-
require 'json_schemer'
|
63
|
-
require 'multi_json'
|
64
|
-
require_relative 'validation'
|
65
|
-
|
66
|
-
module OpenapiFirst
|
67
|
-
class ResponseValidator
|
68
|
-
def initialize(spec)
|
69
|
-
@spec = spec
|
70
|
-
end
|
71
|
-
|
72
|
-
def validate(request, response)
|
73
|
-
errors = validation_errors(request, response)
|
74
|
-
Validation.new(errors || [])
|
75
|
-
rescue OasParser::ResponseCodeNotFound, OasParser::MethodNotFound => e
|
76
|
-
Validation.new([e.message])
|
77
|
-
end
|
78
|
-
|
79
|
-
private
|
80
|
-
|
81
|
-
def validation_errors(request, response)
|
82
|
-
content = response_for(request, response)&.content
|
83
|
-
return unless content
|
84
|
-
|
85
|
-
content_type = content[response.content_type]
|
86
|
-
return ["Content type not found: '#{response.content_type}'"] unless content_type
|
87
|
-
|
88
|
-
response_schema = content_type['schema']
|
89
|
-
return unless response_schema
|
90
|
-
|
91
|
-
response_data = MultiJson.load(response.body)
|
92
|
-
validate_json_schema(response_schema, response_data)
|
93
|
-
end
|
94
|
-
|
95
|
-
def validate_json_schema(schema, data)
|
96
|
-
JSONSchemer.schema(schema).validate(data).to_a.map do |error|
|
97
|
-
format_error(error)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def format_error(error)
|
102
|
-
ValidationFormat.error_details(error)
|
103
|
-
.merge!(
|
104
|
-
data_pointer: error['data_pointer'],
|
105
|
-
schema_pointer: error['schema_pointer']
|
106
|
-
).tap do |formatted|
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
def response_for(request, response)
|
111
|
-
@spec
|
112
|
-
.find_operation!(request)
|
113
|
-
&.response_by_code(response.status.to_s, use_default: true)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
data/lib/openapi_first/router.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenapiFirst
|
4
|
+
module RouterRequired
|
5
|
+
def call(env)
|
6
|
+
unless env.key?(OPERATION)
|
7
|
+
raise 'OpenapiFirst::Router missing in middleware stack. Did you forget adding OpenapiFirst::Router?'
|
8
|
+
end
|
9
|
+
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
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: 0.11.0
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Haller
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: deep_merge
|
@@ -206,12 +206,13 @@ files:
|
|
206
206
|
- lib/openapi_first/find_handler.rb
|
207
207
|
- lib/openapi_first/inbox.rb
|
208
208
|
- lib/openapi_first/operation.rb
|
209
|
-
- lib/openapi_first/operation_resolver.rb
|
210
209
|
- lib/openapi_first/request_validation.rb
|
210
|
+
- lib/openapi_first/responder.rb
|
211
211
|
- lib/openapi_first/response_object.rb
|
212
212
|
- lib/openapi_first/response_validation.rb
|
213
213
|
- lib/openapi_first/response_validator.rb
|
214
214
|
- lib/openapi_first/router.rb
|
215
|
+
- lib/openapi_first/router_required.rb
|
215
216
|
- lib/openapi_first/utils.rb
|
216
217
|
- lib/openapi_first/validation.rb
|
217
218
|
- lib/openapi_first/validation_format.rb
|
@@ -235,9 +236,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
235
236
|
version: '0'
|
236
237
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
237
238
|
requirements:
|
238
|
-
- - "
|
239
|
+
- - ">="
|
239
240
|
- !ruby/object:Gem::Version
|
240
|
-
version:
|
241
|
+
version: '0'
|
241
242
|
requirements: []
|
242
243
|
rubygems_version: 3.1.2
|
243
244
|
signing_key:
|