grape-swagger 0.24.0 → 0.25.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +4 -0
- data/.rubocop_todo.yml +29 -32
- data/.travis.yml +2 -0
- data/CHANGELOG.md +15 -0
- data/Gemfile +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +253 -223
- data/UPGRADING.md +4 -0
- data/grape-swagger.gemspec +2 -2
- data/lib/grape-swagger.rb +10 -4
- data/lib/grape-swagger/doc_methods.rb +10 -5
- data/lib/grape-swagger/doc_methods/build_model_definition.rb +38 -0
- data/lib/grape-swagger/doc_methods/data_type.rb +6 -5
- data/lib/grape-swagger/doc_methods/move_params.rb +9 -1
- data/lib/grape-swagger/doc_methods/optional_object.rb +1 -1
- data/lib/grape-swagger/doc_methods/parse_params.rb +20 -19
- data/lib/grape-swagger/doc_methods/tag_name_description.rb +9 -17
- data/lib/grape-swagger/endpoint.rb +26 -29
- data/lib/grape-swagger/model_parsers.rb +7 -0
- data/lib/grape-swagger/version.rb +1 -1
- data/spec/issues/427_entity_as_string_spec.rb +39 -0
- data/spec/issues/430_entity_definitions_spec.rb +48 -4
- data/spec/support/model_parsers/entity_parser.rb +10 -1
- data/spec/support/model_parsers/mock_parser.rb +4 -1
- data/spec/support/model_parsers/representable_parser.rb +13 -2
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +0 -2
- data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +56 -0
- data/spec/swagger_v2/api_swagger_v2_spec.rb +87 -61
- data/spec/swagger_v2/endpoint_versioned_path_spec.rb +24 -3
- data/spec/swagger_v2/guarded_endpoint_spec.rb +8 -3
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +2 -8
- data/spec/swagger_v2/namespace_tags_spec.rb +2 -8
- data/spec/swagger_v2/params_array_spec.rb +36 -0
- data/spec/swagger_v2/simple_mounted_api_spec.rb +2 -14
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65693b18186fa5904b3f3012ac7a94cec5d90a44
|
4
|
+
data.tar.gz: d7e2b413a40e081cd155e76a554b1d7f3c532c9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e998b82aba1e6252f6e63fe9dcc2ea53cb6b8aa4f384de774ed71cae5855158378e3e8ecdf920742ec2f5a196434f80c7798713d75d2b1a593e5aed7b6b8719
|
7
|
+
data.tar.gz: 3047cca817fd55c251717de9cf015dc26272f6da6996b67e317b2e058c18ff02fdd42bc5b1835da03500e8096cfc7da4ba1b813b012b5ee1e1993e18d413b502
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -1,30 +1,20 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2016-
|
3
|
+
# on 2016-10-31 11:51:42 +0100 using RuboCop version 0.45.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
-
# Offense count: 1
|
10
|
-
Lint/AmbiguousOperator:
|
11
|
-
Exclude:
|
12
|
-
- 'spec/lib/move_params_spec.rb'
|
13
|
-
|
14
|
-
# Offense count: 1
|
15
|
-
Lint/UnreachableCode:
|
16
|
-
Exclude:
|
17
|
-
- 'example/config.ru'
|
18
|
-
|
19
|
-
# Offense count: 1
|
20
|
-
Lint/UselessAssignment:
|
21
|
-
Exclude:
|
22
|
-
- 'spec/lib/move_params_spec.rb'
|
23
|
-
|
24
9
|
# Offense count: 27
|
25
10
|
Metrics/AbcSize:
|
26
11
|
Max: 56
|
27
12
|
|
13
|
+
# Offense count: 1
|
14
|
+
# Configuration parameters: CountComments.
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Max: 30
|
17
|
+
|
28
18
|
# Offense count: 3
|
29
19
|
# Configuration parameters: CountComments.
|
30
20
|
Metrics/ClassLength:
|
@@ -32,38 +22,32 @@ Metrics/ClassLength:
|
|
32
22
|
|
33
23
|
# Offense count: 10
|
34
24
|
Metrics/CyclomaticComplexity:
|
35
|
-
Max:
|
25
|
+
Max: 15
|
36
26
|
|
37
|
-
# Offense count:
|
38
|
-
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
27
|
+
# Offense count: 803
|
28
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives.
|
39
29
|
# URISchemes: http, https
|
40
30
|
Metrics/LineLength:
|
41
|
-
Max:
|
31
|
+
Max: 160
|
42
32
|
|
43
|
-
# Offense count:
|
33
|
+
# Offense count: 34
|
44
34
|
# Configuration parameters: CountComments.
|
45
35
|
Metrics/MethodLength:
|
46
36
|
Max: 101
|
47
37
|
|
48
|
-
# Offense count:
|
38
|
+
# Offense count: 5
|
49
39
|
Metrics/PerceivedComplexity:
|
50
|
-
Max:
|
40
|
+
Max: 16
|
51
41
|
|
52
|
-
# Offense count:
|
42
|
+
# Offense count: 3
|
53
43
|
Style/ClassVars:
|
54
44
|
Exclude:
|
55
|
-
- 'example/api/endpoints.rb'
|
56
45
|
- 'lib/grape-swagger/doc_methods.rb'
|
57
46
|
|
58
|
-
# Offense count:
|
47
|
+
# Offense count: 23
|
59
48
|
Style/Documentation:
|
60
49
|
Enabled: false
|
61
50
|
|
62
|
-
# Offense count: 1
|
63
|
-
Style/DoubleNegation:
|
64
|
-
Exclude:
|
65
|
-
- 'example/api/endpoints.rb'
|
66
|
-
|
67
51
|
# Offense count: 5
|
68
52
|
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts.
|
69
53
|
Style/FileName:
|
@@ -74,7 +58,20 @@ Style/FileName:
|
|
74
58
|
- 'spec/swagger_v2/api_swagger_v2_type-format_spec.rb'
|
75
59
|
- 'spec/swagger_v2/grape-swagger_spec.rb'
|
76
60
|
|
77
|
-
# Offense count:
|
61
|
+
# Offense count: 94
|
62
|
+
# Cop supports --auto-correct.
|
63
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
64
|
+
# SupportedStyles: when_needed, always
|
65
|
+
Style/FrozenStringLiteralComment:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
# Offense count: 1
|
69
|
+
# Cop supports --auto-correct.
|
70
|
+
Style/MultilineIfModifier:
|
71
|
+
Exclude:
|
72
|
+
- 'lib/grape-swagger/grape/route.rb'
|
73
|
+
|
74
|
+
# Offense count: 4
|
78
75
|
# Cop supports --auto-correct.
|
79
76
|
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
|
80
77
|
# SupportedStyles: slashes, percent_r, mixed
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -8,6 +8,21 @@
|
|
8
8
|
|
9
9
|
* Your contribution here.
|
10
10
|
|
11
|
+
### 0.25.0 (October 31, 2016)
|
12
|
+
|
13
|
+
#### Features
|
14
|
+
|
15
|
+
* [#524](https://github.com/ruby-grape/grape-swagger/pull/524): Use route tags for global tag set - [@LeFnord](https://github.com/LeFnord).
|
16
|
+
* [#523](https://github.com/ruby-grape/grape-swagger/pull/523): Allow specifying custom tags at the route level - [@jordanfbrown](https://github.com/jordanfbrown).
|
17
|
+
* [#520](https://github.com/ruby-grape/grape-swagger/pull/520): Response model can have required attributes - [@WojciechKo](https://github.com/WojciechKo).
|
18
|
+
* [#510](https://github.com/ruby-grape/grape-swagger/pull/510): Use 'token_owner' instead of 'oauth_token' on Swagger UI endpoint authorization. - [@texpert](https://github.com/texpert).
|
19
|
+
|
20
|
+
#### Fixes
|
21
|
+
|
22
|
+
* [#527](https://github.com/ruby-grape/grape-swagger/pull/527): Accepts string as entity - [@LeFnord](https://github.com/LeFnord).
|
23
|
+
* [#515](https://github.com/ruby-grape/grape-swagger/pull/515): Removes limit on model names - [@LeFnord](https://github.com/LeFnord).
|
24
|
+
* [#511](https://github.com/ruby-grape/grape-swagger/pull/511): Fix incorrect data type linking for request params of entity types - [@serggl](https://github.com/serggl).
|
25
|
+
|
11
26
|
### 0.24.0 (September 23, 2016)
|
12
27
|
|
13
28
|
#### Features
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -14,10 +14,9 @@
|
|
14
14
|
* [Model Parsers](#model_parsers)
|
15
15
|
* [Configure](#configure)
|
16
16
|
* [Routes Configuration](#routes)
|
17
|
+
* [Using Grape Entities](#grape-entity)
|
17
18
|
* [Securing the Swagger UI](#oauth)
|
18
19
|
* [Markdown](#md_usage)
|
19
|
-
* [Response documentation](#response)
|
20
|
-
* [Extensions](#extensions)
|
21
20
|
* [Example](#example)
|
22
21
|
* [Rake Tasks](#rake)
|
23
22
|
|
@@ -28,7 +27,7 @@ The grape-swagger gem provides an autogenerated documentation for your [Grape](h
|
|
28
27
|
|
29
28
|
![Demo Screenshot](example/swagger-example.png)
|
30
29
|
|
31
|
-
|
30
|
+
This screenshot is based on the [Hussars](https://github.com/LeFnord/hussars) sample app.
|
32
31
|
|
33
32
|
<a name="related" />
|
34
33
|
## Related Projects
|
@@ -157,7 +156,7 @@ GrapeSwagger.model_parsers.insert_before(GrapeSwagger::Representable::Parser, Gr
|
|
157
156
|
GrapeSwagger.model_parsers.insert_after(GrapeSwagger::Roar::Parser, GrapeSwagger::Representable::Parser, Representable::Decorator)
|
158
157
|
```
|
159
158
|
|
160
|
-
As we know, `Roar::Decorator` uses
|
159
|
+
As we know, `Roar::Decorator` uses `Representable::Decorator` as a superclass, this allows to avoid a problem when Roar objects are processed by `GrapeSwagger::Representable::Parser` instead of `GrapeSwagger::Roar::Parser`.
|
161
160
|
|
162
161
|
|
163
162
|
### CORS
|
@@ -181,7 +180,7 @@ before do
|
|
181
180
|
header['Access-Control-Allow-Origin'] = '*'
|
182
181
|
header['Access-Control-Request-Method'] = '*'
|
183
182
|
end
|
184
|
-
|
183
|
+
```
|
185
184
|
|
186
185
|
|
187
186
|
<a name="configure" />
|
@@ -196,7 +195,7 @@ end
|
|
196
195
|
* [markdown](#markdown)
|
197
196
|
* [endpoint_auth_wrapper](#endpoint_auth_wrapper)
|
198
197
|
* [swagger_endpoint_guard](#swagger_endpoint_guard)
|
199
|
-
* [
|
198
|
+
* [token_owner](#token_owner)
|
200
199
|
* [security_definitions](#security_definitions)
|
201
200
|
* [models](#models)
|
202
201
|
* [hide_documentation_path](#hide_documentation_path)
|
@@ -204,10 +203,10 @@ end
|
|
204
203
|
|
205
204
|
|
206
205
|
You can pass a hash with optional configuration settings to ```add_swagger_documentation```.
|
207
|
-
The examples
|
206
|
+
The examples show the default value.
|
208
207
|
|
209
208
|
|
210
|
-
The `host` and `base_path` options also accept a `proc` or `lambda` to evaluate, which is passed a [request](http://www.rubydoc.info/github/rack/rack/Rack/Request) object:
|
209
|
+
The `host` and `base_path` options also accept a `proc` or a `lambda` to evaluate, which is passed a [request](http://www.rubydoc.info/github/rack/rack/Rack/Request) object:
|
211
210
|
|
212
211
|
```ruby
|
213
212
|
add_swagger_documentation \
|
@@ -296,19 +295,21 @@ add_swagger_documentation \
|
|
296
295
|
swagger_endpoint_guard: 'oauth2 false'
|
297
296
|
```
|
298
297
|
|
299
|
-
<a name="
|
300
|
-
####
|
301
|
-
Specify the method
|
298
|
+
<a name="token_owner" />
|
299
|
+
#### token_owner:
|
300
|
+
Specify the token_owner method, provided by the middleware, which is typically named 'resource_owner'.
|
302
301
|
|
303
302
|
```ruby
|
304
303
|
add_swagger_documentation \
|
305
|
-
|
304
|
+
token_owner: 'resource_owner'
|
306
305
|
```
|
307
306
|
|
308
307
|
<a name="security_definitions" />
|
309
308
|
#### security_definitions:
|
310
309
|
Specify the [Security Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-definitions-object)
|
311
310
|
|
311
|
+
_NOTE: [Swagger-UI is supporting only implicit flow yet](https://github.com/swagger-api/swagger-ui/issues/2406#issuecomment-248651879)_
|
312
|
+
|
312
313
|
```ruby
|
313
314
|
add_swagger_documentation \
|
314
315
|
security_definitions: {
|
@@ -388,14 +389,20 @@ add_swagger_documentation \
|
|
388
389
|
* [Swagger Header Parameters](#headers)
|
389
390
|
* [Hiding an Endpoint](#hiding)
|
390
391
|
* [Overriding Auto-Generated Nicknames](#overriding-auto-generated-nicknames)
|
391
|
-
* [Defining an endpoint as array](#array)
|
392
|
-
* [Using an options hash](#options)
|
393
392
|
* [Specify endpoint details](#details)
|
394
|
-
* [Overriding
|
395
|
-
* [Overriding
|
396
|
-
* [
|
393
|
+
* [Overriding the route summary](#summary)
|
394
|
+
* [Overriding the tags](#tags)
|
395
|
+
* [Defining an endpoint as an array](#array)
|
396
|
+
* [Using an options hash](#options)
|
397
|
+
* [Overriding parameter type](#overriding-param-type)
|
398
|
+
* [Overriding data type of the parameter](#overriding-type-of-param)
|
399
|
+
* [Multiple types](#multiple-types)
|
400
|
+
* [Array of data type](#array-type)
|
401
|
+
* [Collection Format](#collection-format)
|
397
402
|
* [Hiding parameters](#hiding-parameters)
|
403
|
+
* [Setting a Swagger default value](#default-value)
|
398
404
|
* [Response documentation](#response)
|
405
|
+
* [Extensions](#extensions)
|
399
406
|
|
400
407
|
|
401
408
|
<a name="headers" />
|
@@ -436,6 +443,7 @@ desc 'Conditionally hide this endpoint', hidden: lambda { ENV['EXPERIMENTAL'] !=
|
|
436
443
|
```
|
437
444
|
|
438
445
|
|
446
|
+
<a name="overriding-auto-generated-nicknames" />
|
439
447
|
#### Overriding Auto-Generated Nicknames
|
440
448
|
|
441
449
|
You can specify a swagger nickname to use instead of the auto generated name by adding `:nickname 'string'``` in the description of the endpoint.
|
@@ -445,10 +453,55 @@ desc 'Get a full list of pets', nickname: 'getAllPets'
|
|
445
453
|
```
|
446
454
|
|
447
455
|
|
456
|
+
<a name="details" />
|
457
|
+
#### Specify endpoint details
|
458
|
+
|
459
|
+
To specify further details for an endpoint, use the `detail` option within a block passed to `desc`:
|
460
|
+
|
461
|
+
```ruby
|
462
|
+
desc 'Get all kittens!' do
|
463
|
+
detail 'this will expose all the kittens'
|
464
|
+
end
|
465
|
+
get '/kittens' do
|
466
|
+
```
|
467
|
+
|
468
|
+
|
469
|
+
<a name="summary" />
|
470
|
+
#### Overriding the route summary
|
471
|
+
|
472
|
+
To override the summary, add `summary: '[string]'` after the description.
|
473
|
+
|
474
|
+
```ruby
|
475
|
+
namespace 'order' do
|
476
|
+
desc 'This will be your summary',
|
477
|
+
summary: 'Now this is your summary!'
|
478
|
+
get :order_id do
|
479
|
+
...
|
480
|
+
end
|
481
|
+
end
|
482
|
+
```
|
483
|
+
|
484
|
+
|
485
|
+
<a name="tags" />
|
486
|
+
#### Overriding the tags
|
487
|
+
|
488
|
+
Tags are used for logical grouping of operations by resources or any other qualifier. To override the
|
489
|
+
tags array, add `tags: ['tag1', 'tag2']` after the description.
|
490
|
+
|
491
|
+
```ruby
|
492
|
+
namespace 'order' do
|
493
|
+
desc 'This will be your summary', tags: ['orders']
|
494
|
+
get :order_id do
|
495
|
+
...
|
496
|
+
end
|
497
|
+
end
|
498
|
+
```
|
499
|
+
|
500
|
+
|
448
501
|
<a name="array" />
|
449
|
-
#### Defining an endpoint as array
|
502
|
+
#### Defining an endpoint as an array
|
450
503
|
|
451
|
-
You can define an endpoint as array by adding `is_array` in the description:
|
504
|
+
You can define an endpoint as an array by adding `is_array` in the description:
|
452
505
|
|
453
506
|
```ruby
|
454
507
|
desc 'Get a full list of pets', is_array: true
|
@@ -466,8 +519,8 @@ desc 'Get all kittens!', {
|
|
466
519
|
hidden: true,
|
467
520
|
is_array: true,
|
468
521
|
nickname: 'getKittens',
|
469
|
-
|
470
|
-
|
522
|
+
success: Entities::Kitten, # or success
|
523
|
+
failures: [[401, 'KittenBitesError', Entities::BadKitten]] # or failure
|
471
524
|
# also explicit as hash: [{ code: 401, mssage: 'KittenBitesError', model: Entities::BadKitten }]
|
472
525
|
produces: [ "array", "of", "mime_types" ],
|
473
526
|
consumes: [ "array", "of", "mime_types" ]
|
@@ -476,39 +529,54 @@ get '/kittens' do
|
|
476
529
|
```
|
477
530
|
|
478
531
|
|
479
|
-
<a name="
|
480
|
-
####
|
532
|
+
<a name="overriding-param-type" />
|
533
|
+
#### Overriding parameter type
|
481
534
|
|
482
|
-
|
535
|
+
You can override paramType, using the documentation hash. See [parameter object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object) for available types.
|
483
536
|
|
484
537
|
```ruby
|
485
|
-
|
486
|
-
|
538
|
+
params do
|
539
|
+
requires :action, type: Symbol, values: [:PAUSE, :RESUME, :STOP], documentation: { param_type: 'query' }
|
540
|
+
end
|
541
|
+
post :act do
|
542
|
+
...
|
487
543
|
end
|
488
|
-
get '/kittens' do
|
489
544
|
```
|
490
545
|
|
491
546
|
|
492
|
-
|
547
|
+
<a name="overriding-type-of-param" />
|
548
|
+
#### Overriding data type of the parameter
|
493
549
|
|
494
|
-
You can override
|
550
|
+
You can override type, using the documentation hash.
|
495
551
|
|
496
552
|
```ruby
|
497
553
|
params do
|
498
|
-
requires :
|
554
|
+
requires :input, type: String, documentation: { type: 'integer' }
|
499
555
|
end
|
500
556
|
post :act do
|
501
557
|
...
|
502
558
|
end
|
503
559
|
```
|
504
560
|
|
505
|
-
|
561
|
+
```json
|
562
|
+
{
|
563
|
+
"in": "formData",
|
564
|
+
"name": "input",
|
565
|
+
"type": "integer",
|
566
|
+
"format": "int32",
|
567
|
+
"required": true
|
568
|
+
}
|
569
|
+
```
|
570
|
+
|
506
571
|
|
507
|
-
|
572
|
+
<a name="multiple-types" />
|
573
|
+
#### Multiple types
|
574
|
+
|
575
|
+
By default when you set multiple types, the first type is selected as swagger type
|
508
576
|
|
509
577
|
```ruby
|
510
578
|
params do
|
511
|
-
requires :
|
579
|
+
requires :action, types: [String, Integer]
|
512
580
|
end
|
513
581
|
post :act do
|
514
582
|
...
|
@@ -518,14 +586,15 @@ end
|
|
518
586
|
```json
|
519
587
|
{
|
520
588
|
"in": "formData",
|
521
|
-
"name": "
|
522
|
-
"type": "
|
523
|
-
"format": "int32",
|
589
|
+
"name": "action",
|
590
|
+
"type": "string",
|
524
591
|
"required": true
|
525
592
|
}
|
526
593
|
```
|
527
594
|
|
528
|
-
|
595
|
+
|
596
|
+
<a name="array-type" />
|
597
|
+
#### Array of data type
|
529
598
|
|
530
599
|
Array types are also supported.
|
531
600
|
|
@@ -550,6 +619,8 @@ end
|
|
550
619
|
}
|
551
620
|
```
|
552
621
|
|
622
|
+
|
623
|
+
<a name="collection-format" />
|
553
624
|
#### Collection format of arrays
|
554
625
|
|
555
626
|
You can set the collection format of an array, using the documentation hash.
|
@@ -583,28 +654,8 @@ end
|
|
583
654
|
}
|
584
655
|
```
|
585
656
|
|
586
|
-
#### Multi types
|
587
|
-
|
588
|
-
By default when you set multi types, the first type is selected as swagger type
|
589
|
-
|
590
|
-
```ruby
|
591
|
-
params do
|
592
|
-
requires :action, types: [String, Integer]
|
593
|
-
end
|
594
|
-
post :act do
|
595
|
-
...
|
596
|
-
end
|
597
|
-
```
|
598
|
-
|
599
|
-
```json
|
600
|
-
{
|
601
|
-
"in": "formData",
|
602
|
-
"name": "action",
|
603
|
-
"type": "string",
|
604
|
-
"required": true
|
605
|
-
}
|
606
|
-
```
|
607
657
|
|
658
|
+
<a name="hiding-parameters" />
|
608
659
|
#### Hiding parameters
|
609
660
|
|
610
661
|
Exclude single optional parameter from the documentation
|
@@ -619,28 +670,27 @@ post :act do
|
|
619
670
|
end
|
620
671
|
```
|
621
672
|
|
622
|
-
#### Overriding the route summary
|
623
673
|
|
624
|
-
|
674
|
+
<a name="default-value" />
|
675
|
+
#### Setting a Swagger default value
|
676
|
+
|
677
|
+
Grape allows for an additional documentation hash to be passed to a parameter.
|
625
678
|
|
626
679
|
```ruby
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
...
|
631
|
-
end
|
680
|
+
params do
|
681
|
+
requires :id, type: Integer, desc: 'Coffee ID'
|
682
|
+
requires :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', documentation: { default: 72 }
|
632
683
|
end
|
633
684
|
```
|
634
685
|
|
635
|
-
|
686
|
+
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
|
687
|
+
|
688
|
+
Grape uses the option `default` to set a default value for optional parameters. This is different in that Grape will set your parameter to the provided default if the parameter is omitted, whereas the example value above will only set the value in the UI itself. This will set the Swagger `defaultValue` to the provided value. Note that the example value will override the Grape default value.
|
636
689
|
|
637
690
|
```ruby
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
get :order_id do
|
642
|
-
...
|
643
|
-
end
|
691
|
+
params do
|
692
|
+
requires :id, type: Integer, desc: 'Coffee ID'
|
693
|
+
optional :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', default: 72
|
644
694
|
end
|
645
695
|
```
|
646
696
|
|
@@ -692,46 +742,140 @@ end
|
|
692
742
|
```
|
693
743
|
|
694
744
|
|
695
|
-
<a name="
|
696
|
-
|
697
|
-
|
698
|
-
* [Markdown in Detail](#md_usage)
|
699
|
-
* [Response documentation](#response)
|
745
|
+
<a name="response" />
|
746
|
+
#### Response documentation
|
700
747
|
|
701
|
-
|
748
|
+
You can also document the HTTP status codes with a description and a specified model, as ref in the schema to the definitions, that your API returns with one of the following syntax.
|
702
749
|
|
703
|
-
|
750
|
+
In the following cases, the schema ref would be taken from route.
|
704
751
|
|
705
752
|
```ruby
|
706
|
-
|
707
|
-
|
708
|
-
|
753
|
+
desc 'thing', failures: [ { code: 400, message: "Invalid parameter entry" } ]
|
754
|
+
get '/thing' do
|
755
|
+
...
|
709
756
|
end
|
710
757
|
```
|
711
758
|
|
712
|
-
|
759
|
+
```ruby
|
760
|
+
desc 'thing' do
|
761
|
+
params Entities::Something.documentation
|
762
|
+
failures [ { code: 400, message: "Invalid parameter entry" } ]
|
763
|
+
end
|
764
|
+
get '/thing' do
|
765
|
+
...
|
766
|
+
end
|
767
|
+
```
|
713
768
|
|
714
|
-
|
769
|
+
```ruby
|
770
|
+
get '/thing', failures: [
|
771
|
+
{ code: 200, message: 'Ok' },
|
772
|
+
{ code: 400, message: "Invalid parameter entry" }
|
773
|
+
] do
|
774
|
+
...
|
775
|
+
end
|
776
|
+
```
|
715
777
|
|
778
|
+
By adding a `model` key, e.g. this would be taken.
|
716
779
|
```ruby
|
717
|
-
|
718
|
-
|
719
|
-
|
780
|
+
get '/thing', failures: [
|
781
|
+
{ code: 200, message: 'Ok' },
|
782
|
+
{ code: 422, message: "Invalid parameter entry", model: Entities::ApiError }
|
783
|
+
] do
|
784
|
+
...
|
720
785
|
end
|
721
786
|
```
|
787
|
+
If no status code is defined [defaults](/lib/grape-swagger/endpoint.rb#L121) would be taken.
|
788
|
+
|
789
|
+
The result is then something like following:
|
790
|
+
|
791
|
+
```json
|
792
|
+
"responses": {
|
793
|
+
"200": {
|
794
|
+
"description": "get Horses",
|
795
|
+
"schema": {
|
796
|
+
"$ref": "#/definitions/Thing"
|
797
|
+
}
|
798
|
+
},
|
799
|
+
"401": {
|
800
|
+
"description": "HorsesOutError",
|
801
|
+
"schema": {
|
802
|
+
"$ref": "#/definitions/ApiError"
|
803
|
+
}
|
804
|
+
}
|
805
|
+
},
|
806
|
+
```
|
722
807
|
|
808
|
+
<a name="extensions" />
|
809
|
+
#### Extensions
|
723
810
|
|
724
|
-
|
811
|
+
Swagger spec2.0 supports extensions on different levels, for the moment,
|
812
|
+
the documentation on `verb`, `path` and `definition` level would be supported.
|
813
|
+
The documented key would be generated from the `x` + `-` + key of the submitted hash,
|
814
|
+
for possibilities refer to the [extensions spec](spec/lib/extensions_spec.rb).
|
815
|
+
To get an overview *how* the extensions would be defined on grape level, see the following examples:
|
725
816
|
|
726
|
-
|
817
|
+
- `verb` extension, add a `x` key to the `desc` hash:
|
818
|
+
```ruby
|
819
|
+
desc 'This returns something with extension on verb level',
|
820
|
+
x: { some: 'stuff' }
|
821
|
+
```
|
822
|
+
this would generate:
|
823
|
+
```json
|
824
|
+
"/path":{
|
825
|
+
"get":{
|
826
|
+
"…":"…",
|
827
|
+
"x-some":"stuff"
|
828
|
+
}
|
829
|
+
}
|
830
|
+
```
|
727
831
|
|
728
|
-
|
832
|
+
- `path` extension, by setting via route settings:
|
833
|
+
```ruby
|
834
|
+
route_setting :x_path, { some: 'stuff' }
|
835
|
+
```
|
836
|
+
this would generate:
|
837
|
+
```json
|
838
|
+
"/path":{
|
839
|
+
"x-some":"stuff",
|
840
|
+
"get":{
|
841
|
+
"…":"…",
|
842
|
+
}
|
843
|
+
}
|
844
|
+
```
|
845
|
+
|
846
|
+
- `definition` extension, again by setting via route settings,
|
847
|
+
here the status code must be provided, for which definition the extensions should be:
|
848
|
+
```ruby
|
849
|
+
route_setting :x_def, { for: 422, other: 'stuff' }
|
850
|
+
```
|
851
|
+
this would generate:
|
852
|
+
```json
|
853
|
+
"/definitions":{
|
854
|
+
"ApiError":{
|
855
|
+
"x-other":"stuff",
|
856
|
+
"…":"…",
|
857
|
+
}
|
858
|
+
}
|
859
|
+
```
|
860
|
+
or, for more definitions:
|
861
|
+
```ruby
|
862
|
+
route_setting :x_def, [{ for: 422, other: 'stuff' }, { for: 200, some: 'stuff' }]
|
863
|
+
```
|
864
|
+
|
865
|
+
|
866
|
+
<a name="grape-entity" />
|
867
|
+
## Using Grape Entities
|
868
|
+
|
869
|
+
Add the [grape-entity](https://github.com/ruby-grape/grape-entity) and [grape-swagger-entity](https://github.com/ruby-grape/grape-swagger-entity) gem to your Gemfile.
|
870
|
+
|
871
|
+
The following example exposes statuses. And exposes statuses documentation adding :type, :desc and :required.
|
872
|
+
The documented class/definition name could be set via `#entity_name`.
|
729
873
|
|
730
874
|
```ruby
|
731
875
|
module API
|
732
876
|
module Entities
|
733
877
|
class Status < Grape::Entity
|
734
|
-
expose :text, documentation: { type: 'string', desc: 'Status update text.' }
|
878
|
+
expose :text, documentation: { type: 'string', desc: 'Status update text.', required: true }
|
735
879
|
expose :links, using: Link, documentation: { type: 'link', is_array: true }
|
736
880
|
expose :numbers, documentation: { type: 'integer', desc: 'favourite number', values: [1,2,3,4] }
|
737
881
|
end
|
@@ -739,6 +883,11 @@ module API
|
|
739
883
|
class Link < Grape::Entity
|
740
884
|
expose :href, documentation: { type: 'url' }
|
741
885
|
expose :rel, documentation: { type: 'string'}
|
886
|
+
|
887
|
+
def self.entity_name
|
888
|
+
'LinkedStatus'
|
889
|
+
end
|
890
|
+
|
742
891
|
end
|
743
892
|
end
|
744
893
|
|
@@ -764,12 +913,12 @@ end
|
|
764
913
|
```
|
765
914
|
|
766
915
|
|
767
|
-
|
916
|
+
### Relationships
|
768
917
|
|
769
918
|
You may safely omit `type` from relationships, as it can be inferred. However, if you need to specify or override it, use the full name of the class leaving out any modules named `Entities` or `Entity`.
|
770
919
|
|
771
920
|
|
772
|
-
|
921
|
+
#### 1xN
|
773
922
|
|
774
923
|
```ruby
|
775
924
|
module API
|
@@ -799,7 +948,7 @@ end
|
|
799
948
|
```
|
800
949
|
|
801
950
|
|
802
|
-
|
951
|
+
#### 1x1
|
803
952
|
|
804
953
|
Note: `is_array` is `false` by default.
|
805
954
|
|
@@ -836,9 +985,10 @@ end
|
|
836
985
|
|
837
986
|
The Swagger UI on Grape could be secured from unauthorized access using any middleware, which provides certain methods:
|
838
987
|
|
839
|
-
- a *before* method to be run in the Grape controller for authorization purpose;
|
840
988
|
- some guard method, which could receive as argument a string or an array of authorization scopes;
|
841
|
-
- a method
|
989
|
+
- a *before* method to be run in the Grape controller for authorization purpose;
|
990
|
+
- a set of methods which will process the access token received in the HTTP request headers (usually in the
|
991
|
+
'HTTP_AUTHORIZATION' header) and try to return the owner of the token.
|
842
992
|
|
843
993
|
Below are some examples of securing the Swagger UI on Grape installed along with Ruby on Rails:
|
844
994
|
|
@@ -856,14 +1006,14 @@ This is how to configure the grape_swagger documentation:
|
|
856
1006
|
hide_format: true,
|
857
1007
|
endpoint_auth_wrapper: WineBouncer::OAuth2, # This is the middleware for securing the Swagger UI
|
858
1008
|
swagger_endpoint_guard: 'oauth2 false', # this is the guard method and scope
|
859
|
-
|
1009
|
+
token_owner: 'resource_owner' # This is the method returning the owner of the token
|
860
1010
|
```
|
861
1011
|
|
862
1012
|
The guard method should inject the Security Requirement Object into the endpoint's route settings (see Grape::DSL::Settings.route_setting method).
|
863
1013
|
|
864
|
-
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e.
|
865
|
-
is retreiving
|
866
|
-
the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
|
1014
|
+
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. the
|
1015
|
+
access_token is being retreiving from the HTTP request, but the 'false' scope is for skipping authorization and
|
1016
|
+
showing the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
|
867
1017
|
wouldn't be displayed at all to unauthorized users.
|
868
1018
|
|
869
1019
|
Further on, the guard could be used, where necessary, for endpoint access protection. Put it prior to the endpoint's method:
|
@@ -886,7 +1036,7 @@ And, finally, if you want to not only restrict the access, but to completely hid
|
|
886
1036
|
users, you could pass a lambda to the :hidden key of a endpoint's description:
|
887
1037
|
|
888
1038
|
```ruby
|
889
|
-
not_admins = lambda { |
|
1039
|
+
not_admins = lambda { |token_owner = nil| token_owner.nil? || !token_owner.admin? }
|
890
1040
|
|
891
1041
|
resource :users do
|
892
1042
|
desc 'Create user', hidden: not_admins
|
@@ -897,7 +1047,7 @@ users, you could pass a lambda to the :hidden key of a endpoint's description:
|
|
897
1047
|
end
|
898
1048
|
```
|
899
1049
|
|
900
|
-
The lambda is checking whether the user is authenticated (if not, the
|
1050
|
+
The lambda is checking whether the user is authenticated (if not, the token_owner is nil by default), and has the admin
|
901
1051
|
role - only admins can see this endpoint.
|
902
1052
|
|
903
1053
|
<a name="md_usage" />
|
@@ -974,126 +1124,6 @@ end
|
|
974
1124
|
```
|
975
1125
|
|
976
1126
|
|
977
|
-
<a name="response" />
|
978
|
-
## Response documentation
|
979
|
-
|
980
|
-
You can also document the HTTP status codes with a description and a specified model, as ref in the schema to the definitions, that your API returns with one of the following syntax.
|
981
|
-
|
982
|
-
In the following cases, the schema ref would be taken from route.
|
983
|
-
|
984
|
-
```ruby
|
985
|
-
desc 'thing', http_codes: [ { code: 400, message: "Invalid parameter entry" } ]
|
986
|
-
get '/thing' do
|
987
|
-
...
|
988
|
-
end
|
989
|
-
```
|
990
|
-
|
991
|
-
```ruby
|
992
|
-
desc 'thing' do
|
993
|
-
params Entities::Something.documentation
|
994
|
-
http_codes [ { code: 400, message: "Invalid parameter entry" } ]
|
995
|
-
end
|
996
|
-
get '/thing' do
|
997
|
-
...
|
998
|
-
end
|
999
|
-
```
|
1000
|
-
|
1001
|
-
```ruby
|
1002
|
-
get '/thing', http_codes: [
|
1003
|
-
{ code: 200, message: 'Ok' },
|
1004
|
-
{ code: 400, message: "Invalid parameter entry" }
|
1005
|
-
] do
|
1006
|
-
...
|
1007
|
-
end
|
1008
|
-
```
|
1009
|
-
|
1010
|
-
By adding a `model` key, e.g. this would be taken.
|
1011
|
-
```ruby
|
1012
|
-
get '/thing', http_codes: [
|
1013
|
-
{ code: 200, message: 'Ok' },
|
1014
|
-
{ code: 422, message: "Invalid parameter entry", model: Entities::ApiError }
|
1015
|
-
] do
|
1016
|
-
...
|
1017
|
-
end
|
1018
|
-
```
|
1019
|
-
If no status code is defined [defaults](/lib/grape-swagger/endpoint.rb#L121) would be taken.
|
1020
|
-
|
1021
|
-
The result is then something like following:
|
1022
|
-
|
1023
|
-
```json
|
1024
|
-
"responses": {
|
1025
|
-
"200": {
|
1026
|
-
"description": "get Horses",
|
1027
|
-
"schema": {
|
1028
|
-
"$ref": "#/definitions/Thing"
|
1029
|
-
}
|
1030
|
-
},
|
1031
|
-
"401": {
|
1032
|
-
"description": "HorsesOutError",
|
1033
|
-
"schema": {
|
1034
|
-
"$ref": "#/definitions/ApiError"
|
1035
|
-
}
|
1036
|
-
}
|
1037
|
-
},
|
1038
|
-
```
|
1039
|
-
|
1040
|
-
<a name="extensions" />
|
1041
|
-
## Extensions
|
1042
|
-
|
1043
|
-
Swagger spec2.0 supports extensions on different levels, for the moment,
|
1044
|
-
the documentation on `verb`, `path` and `definition` level would be supported.
|
1045
|
-
The documented key would be generated from the `x` + `-` + key of the submitted hash,
|
1046
|
-
for possibilities refer to the [extensions spec](spec/lib/extensions_spec.rb).
|
1047
|
-
To get an overview *how* the extensions would be defined on grape level, see the following examples:
|
1048
|
-
|
1049
|
-
- `verb` extension, add a `x` key to the `desc` hash:
|
1050
|
-
```ruby
|
1051
|
-
desc 'This returns something with extension on verb level',
|
1052
|
-
x: { some: 'stuff' }
|
1053
|
-
```
|
1054
|
-
this would generate:
|
1055
|
-
```json
|
1056
|
-
"/path":{
|
1057
|
-
"get":{
|
1058
|
-
"…":"…",
|
1059
|
-
"x-some":"stuff"
|
1060
|
-
}
|
1061
|
-
}
|
1062
|
-
```
|
1063
|
-
|
1064
|
-
- `path` extension, by setting via route settings:
|
1065
|
-
```ruby
|
1066
|
-
route_setting :x_path, { some: 'stuff' }
|
1067
|
-
```
|
1068
|
-
this would generate:
|
1069
|
-
```json
|
1070
|
-
"/path":{
|
1071
|
-
"x-some":"stuff",
|
1072
|
-
"get":{
|
1073
|
-
"…":"…",
|
1074
|
-
}
|
1075
|
-
}
|
1076
|
-
```
|
1077
|
-
|
1078
|
-
- `definition` extension, again by setting via route settings,
|
1079
|
-
here the status code must be provided, for which definition the extensions should be:
|
1080
|
-
```ruby
|
1081
|
-
route_setting :x_def, { for: 422, other: 'stuff' }
|
1082
|
-
```
|
1083
|
-
this would generate:
|
1084
|
-
```json
|
1085
|
-
"/definitions":{
|
1086
|
-
"ApiError":{
|
1087
|
-
"x-other":"stuff",
|
1088
|
-
"…":"…",
|
1089
|
-
}
|
1090
|
-
}
|
1091
|
-
```
|
1092
|
-
or, for more definitions:
|
1093
|
-
```ruby
|
1094
|
-
route_setting :x_def, [{ for: 422, other: 'stuff' }, { for: 200, some: 'stuff' }]
|
1095
|
-
```
|
1096
|
-
|
1097
1127
|
<a="example" />
|
1098
1128
|
## Example
|
1099
1129
|
|
@@ -1178,4 +1208,4 @@ See [CONTRIBUTING](CONTRIBUTING.md).
|
|
1178
1208
|
|
1179
1209
|
## Copyright and License
|
1180
1210
|
|
1181
|
-
Copyright (c) 2012-
|
1211
|
+
Copyright (c) 2012-2016 Tim Vandecasteele, ruby-grape and contributors. See [LICENSE.txt](LICENSE.txt) for details.
|