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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 30e7c45fb94e4b7f988d7d989276764e32963b33
4
- data.tar.gz: 4b4f4491f8de4ce2bf4a531f004fc37122c318a7
2
+ SHA256:
3
+ metadata.gz: de41f3d47e3469fc0fe09a379b42999cc53682686ff73c7dbe9c240166d33e93
4
+ data.tar.gz: 94d1a84a6177c62c39b2203612c1beb63a2fe8f2f7b8cda3372693d08e9238fc
5
5
  SHA512:
6
- metadata.gz: 2c8267bd639942994777a10f7988f29fd2ffd291e52721998791f2c8567151addc095929907dae159abb57d30950304819036f674a4dc46a8d920e473bac8d78
7
- data.tar.gz: 8f27c383b3d0b131986f3c5a2224b67cd479270433a52c3c96c56efc2ab77ebea9a67c8f14a73abdce3c768b6acd76cc38697e44fb28957b1c3e26b07d8bf9c8
6
+ metadata.gz: 6a0037db7e527b66aab7b738b9c43c5011ab18f941ac7a01a6d08cb8fd7f912f6d115709f18e2d4375d3ceff3490c27f599cd265468fdd6e0ef9a7d036ae0197
7
+ data.tar.gz: 6bc4fd57a755cacad9c7a5369cb5bb46330c80d88ffd5f2b56b5db495b45e2c7414683a782922a156f0e7c11c6a80ef3e86c7559ace706eefc3f8c1550a27e85
@@ -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 and run the tests:
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
- ./make_site.sh
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 just re-run the tests:
12
+ Once the test site is created you can run the tests:
13
13
  ```
14
- bundle exec rspec
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 documention, e.g. basePath, [definitions](http://swagger.io/specification/#definitionsObject),
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 hierachy of the Swagger Schema:
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[:swagger_document])
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(deep_stringify_keys(document))
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
- # Lifted from ActiveSupport's Hash _deep_transform_keys_in_object
89
- def deep_stringify_keys(object)
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] = deep_stringify_keys(value)
95
+ result[key.to_s] = deep_stringify(value)
94
96
  end
95
97
  when Array
96
- object.map { |e| deep_stringify_keys(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
- swagger_document: attributes[:swagger_document] || RSpec.configuration.swagger_docs.keys.first,
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[:swagger_document]
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
- name = metadata[:swagger_document]
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
 
@@ -3,7 +3,7 @@ module RSpec
3
3
  # Version information for RSpec Swagger.
4
4
  module Swagger
5
5
  module Version
6
- STRING = '0.1.5'
6
+ STRING = '0.2.0'
7
7
  end
8
8
  end
9
9
  end
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.1.5
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: 2018-05-06 00:00:00.000000000 Z
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
- rubyforge_project:
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