swagger-blocks 1.3.4 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e1f287d8e209522490c3d90b62948e11902c2926
4
- data.tar.gz: 0d22ea28d1a2370ae8d896709b4ea52f3e66c221
3
+ metadata.gz: cdfadc9db4d5a4357533c00d7c1cb23d281d41f6
4
+ data.tar.gz: 06820847ade582beb6525e9d3bb9ca253ec0d161
5
5
  SHA512:
6
- metadata.gz: aacf4e7ca9eb945bda88b89d3bdf4869130a63f5667caf305efcefd4658b973faa112e695d24aa43942ad68843db120c6399817f8598a16688dcfd96cd14dcaa
7
- data.tar.gz: ce5b0633b5e74fe5cd6a2e4971d0086ee5089d8b7b266677ed42c16e1c3754eb3562ead1e4b1a892979b25bde6d72339b900472766f6d5cf87765d3a790ba4ac
6
+ metadata.gz: a8945bccc9050739d17f83c2b673fd85cba4b02ed21471e5bc361473779ee138f24aa1fff98095de83579e66b3df0d9e2a8a36b7fd2cb3bb4d4117c108901b65
7
+ data.tar.gz: 42f9773eb3c9a71f2851b7a7eeed46d6819130b168c44fa398f23cbbc510c31efe66e13cbfc38982ab11c8a26f2d8f3947831a600e2c6432efd35a1f9f23af84
data/README.md CHANGED
@@ -331,6 +331,31 @@ The `key` block simply takes the value you give and puts it directly into the fi
331
331
  key :foo, {some_complex: {nested_object: true}}
332
332
  ```
333
333
 
334
+ #### Parameter referencing
335
+
336
+ It is possible to reference parameters rather than explicitly define them in every action in which they are used.
337
+
338
+ To define a reusable parameter, declare it within `swagger_root`
339
+ To reference the parameter, call it within a `swagger_path` or `operation` node
340
+
341
+ ```ruby
342
+ swagger_root do
343
+ key :swagger, '2.0'
344
+ # ...
345
+ parameter :species do
346
+ key :name, :species
347
+ key :description, 'Species of this pet'
348
+ end
349
+ end
350
+
351
+ swagger_path '/pets/' do
352
+ operation :post do
353
+ parameter :species
354
+ # ...
355
+ end
356
+ end
357
+ ```
358
+
334
359
  #### Inline keys
335
360
 
336
361
  It is possible to omit numerous `key` calls using inline hash keys on any block.
@@ -381,6 +406,39 @@ def build_and_override_root_json(overrides = {})
381
406
  end
382
407
  ```
383
408
 
409
+ #### Reducing boilerplate
410
+
411
+ To create reusable parameters, please see [parameter referencing](#parameter-referencing).
412
+
413
+ Most APIs have some common responses for 401s, 404s, etc. Rather than declaring these responses over and over, you can create a reusable module.
414
+
415
+ ```ruby
416
+ module SwaggerResponses
417
+ module AuthenticationError
418
+ def self.extended(base)
419
+ base.response 401 do
420
+ key :description, 'not authorized'
421
+ schema do
422
+ key :'$ref', :AuthenticationError
423
+ end
424
+ end
425
+ end
426
+ end
427
+ end
428
+ ```
429
+
430
+ Now, you just need to extend it:
431
+
432
+ ```ruby
433
+ operation :post do
434
+ extend SwaggerResponses::AuthenticationError
435
+ # ...
436
+ response 200 do
437
+ # ...
438
+ end
439
+ end
440
+ ```
441
+
384
442
  ### Swagger 1.2 example (Rails)
385
443
 
386
444
  See the [v1.2 docs](https://github.com/fotinakis/swagger-blocks/blob/master/README_v1_2.md).
@@ -413,6 +471,7 @@ Throw a ★ on it! :)
413
471
 
414
472
  ## Release notes
415
473
 
474
+ * v1.4.0: Allow parameters to be defined once in swagger_root and reused.
416
475
  * v1.3.4: Fix support for fully-qualified URIs in `$ref` values.
417
476
  * v1.3.3: Bugfix to allow `parameter` inside `swagger_path`.
418
477
  * v1.3.2: Bugfix to allow `property` inside `items` for rare extended schema uses.
@@ -501,6 +501,8 @@ module Swagger
501
501
  end
502
502
 
503
503
  def parameter(inline_keys = nil, &block)
504
+ inline_keys = {'$ref' => "#/parameters/#{inline_keys}"} if inline_keys.is_a?(Symbol)
505
+
504
506
  self.data[:parameters] ||= []
505
507
  self.data[:parameters] << Swagger::Blocks::ParameterNode.call(version: version, inline_keys: inline_keys, &block)
506
508
  end
@@ -509,8 +511,9 @@ module Swagger
509
511
  # v1.2: http://goo.gl/PvwUXj#523-operation-object
510
512
  # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operation-object
511
513
  class OperationNode < Node
512
-
513
514
  def parameter(inline_keys = nil, &block)
515
+ inline_keys = {'$ref' => "#/parameters/#{inline_keys}"} if inline_keys.is_a?(Symbol)
516
+
514
517
  self.data[:parameters] ||= []
515
518
  self.data[:parameters] << Swagger::Blocks::ParameterNode.call(version: version, inline_keys: inline_keys, &block)
516
519
  end
@@ -1,5 +1,5 @@
1
1
  module Swagger
2
2
  module Blocks
3
- VERSION = '1.3.4'
3
+ VERSION = '1.4.0'
4
4
  end
5
5
  end
@@ -49,6 +49,14 @@
49
49
  }
50
50
  }
51
51
  ],
52
+ "parameters": {
53
+ "species": {
54
+ "name": "species",
55
+ "in": "body",
56
+ "description": "Species of this pet",
57
+ "type": "string"
58
+ }
59
+ },
52
60
  "paths": {
53
61
  "/pets": {
54
62
  "get": {
@@ -114,6 +122,9 @@
114
122
  "schema": {
115
123
  "$ref": "#/definitions/PetInput"
116
124
  }
125
+ },
126
+ {
127
+ "$ref": "#/parameters/species"
117
128
  }
118
129
  ],
119
130
  "responses": {
@@ -178,6 +189,41 @@
178
189
  }
179
190
  ]
180
191
  },
192
+ "put": {
193
+ "description": "Update a pet in the store.",
194
+ "operationId": "updatePet",
195
+ "produces": [
196
+ "application/json"
197
+ ],
198
+ "parameters": [
199
+ {
200
+ "name": "pet",
201
+ "in": "body",
202
+ "description": "Pet to update in the store",
203
+ "required": true,
204
+ "schema": {
205
+ "$ref": "#/definitions/PetInput"
206
+ }
207
+ },
208
+ {
209
+ "$ref": "#/parameters/species"
210
+ }
211
+ ],
212
+ "responses": {
213
+ "200": {
214
+ "description": "pet response",
215
+ "schema": {
216
+ "$ref": "#/parameters/Pet"
217
+ }
218
+ },
219
+ "default": {
220
+ "description": "unexpected error",
221
+ "schema": {
222
+ "$ref": "http://example.com/schema.json#/definitions/ErrorModel"
223
+ }
224
+ }
225
+ }
226
+ },
181
227
  "delete": {
182
228
  "description": "deletes a single pet based on the ID supplied",
183
229
  "operationId": "deletePet",
@@ -44,6 +44,12 @@ class PetControllerV2
44
44
  key :url, 'https://swagger.io'
45
45
  end
46
46
  end
47
+ parameter :species do
48
+ key :name, :species
49
+ key :in, :body
50
+ key :description, 'Species of this pet'
51
+ key :type, :string
52
+ end
47
53
  end
48
54
 
49
55
  swagger_path '/pets' do
@@ -105,6 +111,7 @@ class PetControllerV2
105
111
  key :'$ref', :PetInput
106
112
  end
107
113
  end
114
+ parameter :species
108
115
  response 200 do
109
116
  key :description, 'pet response'
110
117
  schema do
@@ -129,6 +136,37 @@ class PetControllerV2
129
136
  key :type, :integer
130
137
  key :format, :int64
131
138
  end
139
+ operation :put do
140
+ key :description, 'Update a pet in the store.'
141
+ key :operationId, 'updatePet'
142
+ key :produces, [
143
+ 'application/json'
144
+ ]
145
+ parameter do
146
+ key :name, :pet
147
+ key :in, :body
148
+ key :description, 'Pet to update in the store'
149
+ key :required, true
150
+ schema do
151
+ key :'$ref', :PetInput
152
+ end
153
+ end
154
+
155
+ parameter :species
156
+
157
+ response 200 do
158
+ key :description, 'pet response'
159
+ schema do
160
+ # Wrong form here, but checks that #/ strings are not transformed.
161
+ key :'$ref', '#/parameters/Pet'
162
+ end
163
+ end
164
+ response :default, description: 'unexpected error' do
165
+ schema do
166
+ key :'$ref', 'http://example.com/schema.json#/definitions/ErrorModel'
167
+ end
168
+ end
169
+ end
132
170
  operation :get do
133
171
  key :description, 'Returns a user based on a single ID, if the user does not have access to the pet'
134
172
  key :operationId, 'findPetById'
@@ -270,7 +308,6 @@ describe 'Swagger::Blocks v2' do
270
308
  it 'is idempotent' do
271
309
  swaggered_classes = [PetControllerV2, PetV2, ErrorModelV2]
272
310
  actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
273
- actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
274
311
  data = JSON.parse(RESOURCE_LISTING_JSON_V2)
275
312
  expect(actual).to eq(data)
276
313
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger-blocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Fotinakis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-27 00:00:00.000000000 Z
11
+ date: 2016-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  requirements: []
112
112
  rubyforge_project:
113
- rubygems_version: 2.2.2
113
+ rubygems_version: 2.6.7
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Define and serve live-updating Swagger JSON for Ruby apps.