rspec-rails-swagger 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CONTRIBUTING.md +6 -6
- data/README.md +79 -5
- data/lib/rspec/rails/swagger/formatter.rb +10 -6
- data/lib/rspec/rails/swagger/helpers.rb +8 -2
- data/lib/rspec/rails/swagger/request_builder.rb +1 -2
- data/lib/rspec/rails/swagger/version.rb +1 -1
- metadata +3 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: de41f3d47e3469fc0fe09a379b42999cc53682686ff73c7dbe9c240166d33e93
|
4
|
+
data.tar.gz: 94d1a84a6177c62c39b2203612c1beb63a2fe8f2f7b8cda3372693d08e9238fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a0037db7e527b66aab7b738b9c43c5011ab18f941ac7a01a6d08cb8fd7f912f6d115709f18e2d4375d3ceff3490c27f599cd265468fdd6e0ef9a7d036ae0197
|
7
|
+
data.tar.gz: 6bc4fd57a755cacad9c7a5369cb5bb46330c80d88ffd5f2b56b5db495b45e2c7414683a782922a156f0e7c11c6a80ef3e86c7559ace706eefc3f8c1550a27e85
|
data/CONTRIBUTING.md
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
## Running tests
|
4
4
|
|
5
|
-
The `make_site.sh` script will create a test site for a specific version of
|
6
|
-
Rails
|
5
|
+
The `scripts/make_site.sh` script will create a test site for a specific version of
|
6
|
+
Rails:
|
7
7
|
```
|
8
|
-
RAILS_VERSION=4.2.0
|
9
|
-
|
8
|
+
export RAILS_VERSION=4.2.0
|
9
|
+
scripts/make_site.sh
|
10
10
|
```
|
11
11
|
|
12
|
-
Once the test site is created you can
|
12
|
+
Once the test site is created you can run the tests:
|
13
13
|
```
|
14
|
-
|
14
|
+
scripts/run_tests.sh
|
15
15
|
```
|
data/README.md
CHANGED
@@ -37,7 +37,7 @@ rails generate rspec:swagger_install
|
|
37
37
|
## Documenting Your API
|
38
38
|
|
39
39
|
Now you can edit `spec/swagger_helper.rb` and start filling in the top level
|
40
|
-
Swagger
|
40
|
+
Swagger documentation, e.g. basePath, [definitions](http://swagger.io/specification/#definitionsObject),
|
41
41
|
[parameters](http://swagger.io/specification/#parametersDefinitionsObject),
|
42
42
|
[tags](http://swagger.io/specification/#tagObject), etc.
|
43
43
|
|
@@ -66,7 +66,7 @@ works pretty well and supports multiple documents.
|
|
66
66
|
|
67
67
|
## RSpec DSL
|
68
68
|
|
69
|
-
The DSL follows the
|
69
|
+
The DSL follows the hierarchy of the Swagger Schema:
|
70
70
|
|
71
71
|
- [Paths Object](http://swagger.io/specification/#paths-object-29)
|
72
72
|
- [Path Item Object](http://swagger.io/specification/#path-item-object-32)
|
@@ -98,7 +98,7 @@ RSpec.describe "Posts Controller", type: :request do
|
|
98
98
|
# Path Object
|
99
99
|
path '/posts/{post_id}' do
|
100
100
|
# Parameter Object
|
101
|
-
parameter "post_id", {in: :path, type: :integer}
|
101
|
+
parameter "post_id", { in: :path, type: :integer }
|
102
102
|
let(:post_id) { 1 }
|
103
103
|
|
104
104
|
# Operation Object
|
@@ -107,6 +107,48 @@ RSpec.describe "Posts Controller", type: :request do
|
|
107
107
|
response 200, description: "success"
|
108
108
|
end
|
109
109
|
end
|
110
|
+
|
111
|
+
# Path Post Object
|
112
|
+
path '/posts/' do
|
113
|
+
# Parameter Object for content type could be defined like:
|
114
|
+
consumes 'application/json'
|
115
|
+
# or:
|
116
|
+
parameter 'Content-Type', { in: :header, type: :string }
|
117
|
+
let(:'Content-Type') { 'application/json' }
|
118
|
+
# one of them would be considered
|
119
|
+
|
120
|
+
# authorization token in the header:
|
121
|
+
parameter 'Authorization', { in: :header, type: :string }
|
122
|
+
let(:'Authorization') { 'Bearer <token-here>' }
|
123
|
+
|
124
|
+
# Parameter Object
|
125
|
+
parameter "post_id", { in: :path, type: :integer }
|
126
|
+
let(:post_id) { 1 }
|
127
|
+
|
128
|
+
# Parameter Object for Body
|
129
|
+
parameter "body", { in: :body, required: true, schema: {
|
130
|
+
type: :object,
|
131
|
+
properties: {
|
132
|
+
title: { type: :string },
|
133
|
+
author_email: { type: :email }
|
134
|
+
}
|
135
|
+
}
|
136
|
+
let (:body) {
|
137
|
+
{ post:
|
138
|
+
{ title: 'my example',
|
139
|
+
author_email: 'me@example.com' }
|
140
|
+
}
|
141
|
+
}
|
142
|
+
}
|
143
|
+
# checkout http://swagger.io/specification/#parameter-object-44 for more information, options and details
|
144
|
+
|
145
|
+
# Operation Object
|
146
|
+
post summary: "update an item" do
|
147
|
+
# Response Object
|
148
|
+
response 200, description: "success"
|
149
|
+
end
|
150
|
+
# ...
|
151
|
+
end
|
110
152
|
```
|
111
153
|
|
112
154
|
|
@@ -116,12 +158,44 @@ These methods are available inside of an RSpec contexts with the `type: :request
|
|
116
158
|
#### `path(template, attributes = {}, &block)`
|
117
159
|
Defines a new Path Item.
|
118
160
|
|
161
|
+
The `attributes` parameter accepts:
|
162
|
+
- `swagger_doc` a key in `RSpec.configuration.swagger_docs` that determines
|
163
|
+
which file the path belongs in.
|
164
|
+
- `tags` with an array of tags that will be applied to the child Operations.
|
165
|
+
|
166
|
+
You can also provide a default file and tags by setting them on a parent RSpec
|
167
|
+
context block:
|
168
|
+
```rb
|
169
|
+
RSpec.describe "Sample Requests", type: :request do
|
170
|
+
# The swagger_doc will be used as a default for the paths defined within the
|
171
|
+
# context block. Similarly, the tags will be merged with those set on paths
|
172
|
+
# defined within them.
|
173
|
+
context "setting defaults", swagger_doc: 'default_document.json', tags: [:context_tag] do
|
174
|
+
path '/posts', swagger_doc: 'overridden_document.yaml' tags: ['path_tag'] do
|
175
|
+
operation "GET", summary: "fetch list" do
|
176
|
+
produces 'application/json'
|
177
|
+
tags 'operation_tag'
|
178
|
+
|
179
|
+
response(200, { description: "successful" })
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
```
|
185
|
+
The `GET /posts` operation in this example will be saved in the `'overridden_document.yaml'`
|
186
|
+
file and tagged with `["context_tag", "path_tag", "operation_tag"]`.
|
187
|
+
|
188
|
+
|
119
189
|
### Path Item Object
|
120
190
|
These methods are available inside of blocks passed to the `path` method.
|
121
191
|
|
122
192
|
#### `operation(method, attributes = {}, &block)`
|
123
193
|
Defines a new Operation Object. The `method` is case insensitive.
|
124
194
|
|
195
|
+
The `attributes` parameter accepts:
|
196
|
+
- `tags` with an array of tags. These will be merged with tags passed to the
|
197
|
+
Path Item or `tags` method inside the Operation's block.
|
198
|
+
|
125
199
|
#### `delete(attributes = {}, &block)`
|
126
200
|
Alias for `operation(:delete, attributes, block)`.
|
127
201
|
|
@@ -161,7 +235,7 @@ parameter ref: "#/parameters/site_id"
|
|
161
235
|
Values for the parameters are set using `let`:
|
162
236
|
```rb
|
163
237
|
post summary: "create" do
|
164
|
-
parameter "body", in: :body, schema: { foo: :bar}
|
238
|
+
parameter "body", in: :body, schema: { foo: :bar }
|
165
239
|
let(:body) { { post: { title: 'asdf', body: "blah" } } }
|
166
240
|
# ...
|
167
241
|
end
|
@@ -203,7 +277,7 @@ RSpec.describe "Sample Requests", type: :request, tags: [:context_tag] do
|
|
203
277
|
produces 'application/json'
|
204
278
|
tags 'operation_tag'
|
205
279
|
|
206
|
-
response(200, {description: "successful"})
|
280
|
+
response(200, { description: "successful" })
|
207
281
|
end
|
208
282
|
end
|
209
283
|
end
|
@@ -37,7 +37,7 @@ module RSpec
|
|
37
37
|
return unless metadata[:swagger_object] == :response
|
38
38
|
|
39
39
|
# Then add everything to the document
|
40
|
-
document = document_for(metadata[:
|
40
|
+
document = document_for(metadata[:swagger_doc])
|
41
41
|
path_item = path_item_for(document, metadata[:swagger_path_item])
|
42
42
|
operation = operation_for(path_item, metadata[:swagger_operation])
|
43
43
|
response = response_for(operation, metadata[:swagger_response])
|
@@ -73,7 +73,7 @@ module RSpec
|
|
73
73
|
def write_file(name, document)
|
74
74
|
output =
|
75
75
|
if %w(.yaml .yml).include? File.extname(name)
|
76
|
-
YAML.dump(
|
76
|
+
YAML.dump(deep_stringify(document))
|
77
77
|
else
|
78
78
|
JSON.pretty_generate(document) + "\n"
|
79
79
|
end
|
@@ -85,15 +85,19 @@ module RSpec
|
|
85
85
|
target.write(output)
|
86
86
|
end
|
87
87
|
|
88
|
-
#
|
89
|
-
|
88
|
+
# Converts hash keys and symbolic values into strings.
|
89
|
+
#
|
90
|
+
# Based on ActiveSupport's Hash _deep_transform_keys_in_object
|
91
|
+
def deep_stringify(object)
|
90
92
|
case object
|
91
93
|
when Hash
|
92
94
|
object.each_with_object({}) do |(key, value), result|
|
93
|
-
result[key.to_s] =
|
95
|
+
result[key.to_s] = deep_stringify(value)
|
94
96
|
end
|
95
97
|
when Array
|
96
|
-
object.map { |e|
|
98
|
+
object.map { |e| deep_stringify(e) }
|
99
|
+
when Symbol
|
100
|
+
object.to_s
|
97
101
|
else
|
98
102
|
object
|
99
103
|
end
|
@@ -45,7 +45,7 @@ module RSpec
|
|
45
45
|
#TODO template might be a $ref
|
46
46
|
meta = {
|
47
47
|
swagger_object: :path_item,
|
48
|
-
|
48
|
+
swagger_doc: attributes[:swagger_doc] || default_document,
|
49
49
|
swagger_path_item: {path: template},
|
50
50
|
}
|
51
51
|
# Merge tags passed into the path with those from parent contexts.
|
@@ -54,6 +54,12 @@ module RSpec
|
|
54
54
|
end
|
55
55
|
describe(template, meta, &block)
|
56
56
|
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def default_document
|
61
|
+
metadata.try(:[], :swagger_doc) || RSpec.configuration.swagger_docs.keys.first
|
62
|
+
end
|
57
63
|
end
|
58
64
|
|
59
65
|
module PathItem
|
@@ -122,7 +128,7 @@ module RSpec
|
|
122
128
|
def resolve_document metadata
|
123
129
|
# TODO: It's really inefficient to keep recreating this. It'd be nice
|
124
130
|
# if we could cache them some place.
|
125
|
-
name = metadata[:
|
131
|
+
name = metadata[:swagger_doc]
|
126
132
|
Document.new(RSpec.configuration.swagger_docs[name])
|
127
133
|
end
|
128
134
|
|
@@ -17,8 +17,7 @@ module RSpec
|
|
17
17
|
# and parameter references can be resolved.
|
18
18
|
def document
|
19
19
|
@document ||= begin
|
20
|
-
|
21
|
-
Document.new(RSpec.configuration.swagger_docs[name])
|
20
|
+
Document.new(RSpec.configuration.swagger_docs[metadata[:swagger_doc]])
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-rails-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- andrew morton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: rails
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '3.1'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '3.1'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rspec-rails
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,8 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
68
|
- !ruby/object:Gem::Version
|
83
69
|
version: '0'
|
84
70
|
requirements: []
|
85
|
-
|
86
|
-
rubygems_version: 2.5.2.1
|
71
|
+
rubygems_version: 3.0.2
|
87
72
|
signing_key:
|
88
73
|
specification_version: 4
|
89
74
|
summary: Generate Swagger docs from RSpec integration tests
|