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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +4 -0
  4. data/.rubocop_todo.yml +29 -32
  5. data/.travis.yml +2 -0
  6. data/CHANGELOG.md +15 -0
  7. data/Gemfile +1 -1
  8. data/LICENSE.txt +1 -1
  9. data/README.md +253 -223
  10. data/UPGRADING.md +4 -0
  11. data/grape-swagger.gemspec +2 -2
  12. data/lib/grape-swagger.rb +10 -4
  13. data/lib/grape-swagger/doc_methods.rb +10 -5
  14. data/lib/grape-swagger/doc_methods/build_model_definition.rb +38 -0
  15. data/lib/grape-swagger/doc_methods/data_type.rb +6 -5
  16. data/lib/grape-swagger/doc_methods/move_params.rb +9 -1
  17. data/lib/grape-swagger/doc_methods/optional_object.rb +1 -1
  18. data/lib/grape-swagger/doc_methods/parse_params.rb +20 -19
  19. data/lib/grape-swagger/doc_methods/tag_name_description.rb +9 -17
  20. data/lib/grape-swagger/endpoint.rb +26 -29
  21. data/lib/grape-swagger/model_parsers.rb +7 -0
  22. data/lib/grape-swagger/version.rb +1 -1
  23. data/spec/issues/427_entity_as_string_spec.rb +39 -0
  24. data/spec/issues/430_entity_definitions_spec.rb +48 -4
  25. data/spec/support/model_parsers/entity_parser.rb +10 -1
  26. data/spec/support/model_parsers/mock_parser.rb +4 -1
  27. data/spec/support/model_parsers/representable_parser.rb +13 -2
  28. data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +0 -2
  29. data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +56 -0
  30. data/spec/swagger_v2/api_swagger_v2_spec.rb +87 -61
  31. data/spec/swagger_v2/endpoint_versioned_path_spec.rb +24 -3
  32. data/spec/swagger_v2/guarded_endpoint_spec.rb +8 -3
  33. data/spec/swagger_v2/namespace_tags_prefix_spec.rb +2 -8
  34. data/spec/swagger_v2/namespace_tags_spec.rb +2 -8
  35. data/spec/swagger_v2/params_array_spec.rb +36 -0
  36. data/spec/swagger_v2/simple_mounted_api_spec.rb +2 -14
  37. metadata +12 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99ac4d378547c6998e6c712a83eb82d8277786fb
4
- data.tar.gz: 9d18548301ad46bd474671a770c5e528ee7a4f3c
3
+ metadata.gz: 65693b18186fa5904b3f3012ac7a94cec5d90a44
4
+ data.tar.gz: d7e2b413a40e081cd155e76a554b1d7f3c532c9f
5
5
  SHA512:
6
- metadata.gz: b609366f8b1e964dbffbc0a65593bca55d050ac46cac23896627fd0b2b49f9a2b4fc368022e20d3e2866d458322d083a13f1bc44d7cc36c4bff323d7082497ed
7
- data.tar.gz: 6c6e93549bf4b7eebc659155dcb21ce5d999ef811ea1f8dbceb7c6104b35c3765d0ea6ef16fb706303f9dcd86c99811862fd939af471cbb1a5ff730c9c07aa50
6
+ metadata.gz: 2e998b82aba1e6252f6e63fe9dcc2ea53cb6b8aa4f384de774ed71cae5855158378e3e8ecdf920742ec2f5a196434f80c7798713d75d2b1a593e5aed7b6b8719
7
+ data.tar.gz: 3047cca817fd55c251717de9cf015dc26272f6da6996b67e317b2e058c18ff02fdd42bc5b1835da03500e8096cfc7da4ba1b813b012b5ee1e1993e18d413b502
data/.gitignore CHANGED
@@ -5,6 +5,9 @@ doc
5
5
  # bundler
6
6
  .bundle
7
7
 
8
+ #IDEA temp files
9
+ .idea
10
+
8
11
  # jeweler generated
9
12
  pkg
10
13
 
@@ -4,3 +4,7 @@ AllCops:
4
4
  - example/**/*
5
5
 
6
6
  inherit_from: .rubocop_todo.yml
7
+
8
+ Metrics/LineLength:
9
+ Exclude:
10
+ - spec/**/*
@@ -1,30 +1,20 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-05-11 23:53:37 +0300 using RuboCop version 0.40.0.
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: 16
25
+ Max: 15
36
26
 
37
- # Offense count: 719
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: 454
31
+ Max: 160
42
32
 
43
- # Offense count: 31
33
+ # Offense count: 34
44
34
  # Configuration parameters: CountComments.
45
35
  Metrics/MethodLength:
46
36
  Max: 101
47
37
 
48
- # Offense count: 6
38
+ # Offense count: 5
49
39
  Metrics/PerceivedComplexity:
50
- Max: 17
40
+ Max: 16
51
41
 
52
- # Offense count: 4
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: 27
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: 3
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
@@ -19,6 +19,8 @@ matrix:
19
19
  env: GRAPE_VERSION=0.16.2
20
20
  - rvm: 2.3.1
21
21
  env: GRAPE_VERSION=0.17.0
22
+ - rvm: 2.3.1
23
+ env: GRAPE_VERSION=0.18.0
22
24
  - rvm: 2.3.1
23
25
  env: GRAPE_VERSION=HEAD
24
26
  - rvm: 2.3.0
@@ -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
@@ -2,7 +2,7 @@ source 'http://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- case version = ENV['GRAPE_VERSION'] || '~> 0.17.0'
5
+ case version = ENV['GRAPE_VERSION'] || '~> 0.18'
6
6
  when 'HEAD'
7
7
  gem 'grape', github: 'ruby-grape/grape'
8
8
  else
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-2014 Tim Vandecasteele and Contributors
1
+ Copyright (c) 2012-2016 Tim Vandecasteele, ruby-grape and Contributors
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
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
- These screenshot is based on the [Hussars](https://github.com/LeFnord/hussars) sample app.
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 as superclass `Representable::Decorator`, this allow to avoid problem when Roar objects will be processed by `GrapeSwagger::Representable::Parser`, instead `GrapeSwagger::Roar::Parser`.
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
- * [oauth_token](#oauth_token)
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 shows the default value.
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="oauth_token" />
300
- #### oauth_token:
301
- Specify the method to get the oauth_token, provided by the middleware.
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
- oauth_token: 'doorkeeper_access_token'
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 param type](#overriding-param-type)
395
- * [Overriding type](#overriding-type)
396
- * [Multi types](#multi-types)
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
- entity: Entities::Kitten, # or success
470
- http_codes: [[401, 'KittenBitesError', Entities::BadKitten]] # or failure
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="details" />
480
- #### Specify endpoint details
532
+ <a name="overriding-param-type" />
533
+ #### Overriding parameter type
481
534
 
482
- To specify further details for an endpoint, use the `detail` option within a block passed to `desc`:
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
- desc 'Get all kittens!' do
486
- detail 'this will expose all the kittens'
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
- #### Overriding param type
547
+ <a name="overriding-type-of-param" />
548
+ #### Overriding data type of the parameter
493
549
 
494
- You can override paramType in POST|PUT methods to query, using the documentation hash.
550
+ You can override type, using the documentation hash.
495
551
 
496
552
  ```ruby
497
553
  params do
498
- requires :action, type: Symbol, values: [:PAUSE, :RESUME, :STOP], documentation: { param_type: 'query' }
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
- #### Overriding type
561
+ ```json
562
+ {
563
+ "in": "formData",
564
+ "name": "input",
565
+ "type": "integer",
566
+ "format": "int32",
567
+ "required": true
568
+ }
569
+ ```
570
+
506
571
 
507
- You can override type, using the documentation hash.
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 :input, type: String, documentation: { type: 'integer' }
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": "input",
522
- "type": "integer",
523
- "format": "int32",
589
+ "name": "action",
590
+ "type": "string",
524
591
  "required": true
525
592
  }
526
593
  ```
527
594
 
528
- #### Array type
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
- By default, the route summary is filled with the value supplied to `desc`.
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
- namespace 'order' do
628
- desc 'This will be your summary'
629
- get :order_id do
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
- To override the summary, add `summary: '[string]'` after the description.
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
- namespace 'order' do
639
- desc 'This will be your summary',
640
- summary: 'Now this is your summary!'
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="additions" />
696
- ## Additional documentation
697
-
698
- * [Markdown in Detail](#md_usage)
699
- * [Response documentation](#response)
745
+ <a name="response" />
746
+ #### Response documentation
700
747
 
701
- ### Setting a Swagger defaultValue
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
- Grape allows for an additional documentation hash to be passed to a parameter.
750
+ In the following cases, the schema ref would be taken from route.
704
751
 
705
752
  ```ruby
706
- params do
707
- requires :id, type: Integer, desc: 'Coffee ID'
708
- requires :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', documentation: { default: 72 }
753
+ desc 'thing', failures: [ { code: 400, message: "Invalid parameter entry" } ]
754
+ get '/thing' do
755
+ ...
709
756
  end
710
757
  ```
711
758
 
712
- The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
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
- 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.
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
- params do
718
- requires :id, type: Integer, desc: 'Coffee ID'
719
- optional :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', default: 72
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
- ### Grape Entities
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
- Add the [grape-entity](https://github.com/agileanimal/grape-entity) gem to your Gemfile.
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
- The following example exposes statuses. And exposes statuses documentation adding :type and :desc.
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
- #### Relationships
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
- ##### 1xN
921
+ #### 1xN
773
922
 
774
923
  ```ruby
775
924
  module API
@@ -799,7 +948,7 @@ end
799
948
  ```
800
949
 
801
950
 
802
- ##### 1x1
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 which processes and returns the access token received in the HTTP request headers (usually in the 'HTTP_AUTHORIZATION' header).
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
- oauth_token: 'doorkeeper_access_token' # This is the method returning the access_token
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. it
865
- is retreiving the access_token from the HTTP request, but the 'false' scope is for skipping authorization and showing
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 { |token=nil| token.nil? || !User.find(token.resource_owner_id).admin? }
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 token is nil by default), and has the admin
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-2014 Tim Vandecasteele and contributors. See [LICENSE.txt](LICENSE.txt) for details.
1211
+ Copyright (c) 2012-2016 Tim Vandecasteele, ruby-grape and contributors. See [LICENSE.txt](LICENSE.txt) for details.