grape-swagger 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +33 -0
  3. data/.rubocop.yml +36 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +3 -0
  7. data/CHANGELOG.md +90 -0
  8. data/CONTRIBUTING.md +126 -0
  9. data/Gemfile +1 -21
  10. data/LICENSE.txt +1 -1
  11. data/README.md +397 -0
  12. data/RELEASING.md +80 -0
  13. data/Rakefile +6 -23
  14. data/UPGRADING.md +47 -0
  15. data/grape-swagger.gemspec +26 -84
  16. data/lib/grape-swagger.rb +237 -111
  17. data/lib/grape-swagger/errors.rb +9 -0
  18. data/lib/grape-swagger/markdown.rb +23 -0
  19. data/lib/grape-swagger/markdown/kramdown_adapter.rb +37 -0
  20. data/lib/grape-swagger/markdown/redcarpet_adapter.rb +89 -0
  21. data/lib/grape-swagger/version.rb +3 -0
  22. data/spec/api_description_spec.rb +41 -0
  23. data/spec/api_global_models_spec.rb +76 -0
  24. data/spec/api_models_spec.rb +190 -93
  25. data/spec/default_api_spec.rb +31 -36
  26. data/spec/form_params_spec.rb +56 -53
  27. data/spec/grape-swagger_helper_spec.rb +88 -49
  28. data/spec/grape-swagger_spec.rb +7 -5
  29. data/spec/hide_api_spec.rb +58 -55
  30. data/spec/markdown/kramdown_adapter_spec.rb +38 -0
  31. data/spec/markdown/markdown_spec.rb +27 -0
  32. data/spec/markdown/redcarpet_adapter_spec.rb +81 -0
  33. data/spec/namespaced_api_spec.rb +47 -0
  34. data/spec/non_default_api_spec.rb +372 -222
  35. data/spec/response_model_spec.rb +80 -0
  36. data/spec/simple_mounted_api_spec.rb +113 -118
  37. data/spec/spec_helper.rb +0 -1
  38. data/spec/version_spec.rb +8 -0
  39. data/test/api.rb +62 -0
  40. data/test/config.ru +10 -2
  41. data/test/splines.png +0 -0
  42. metadata +145 -91
  43. data/.rvmrc +0 -48
  44. data/CHANGELOG.markdown +0 -55
  45. data/Gemfile.lock +0 -94
  46. data/README.markdown +0 -168
  47. data/VERSION +0 -1
  48. data/test/nested_api.rb +0 -30
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a1bf3c5bf4c84bdda47522ddc8148c78c538995
4
+ data.tar.gz: 04f9c3468140334518a3051593c5ff94da824a0a
5
+ SHA512:
6
+ metadata.gz: 302ac60608a12fcbb40ea2988ad73a4459e469605058992aa379f6c6d5d043855560db1d57de531a280e05e054cab6d962cbc6847f405166fec25aadd5d2deb7
7
+ data.tar.gz: 9b1a01f0aac40776531b393465e24baa14b873824ada7171df0e546711bb92d143804d44339b8ae27fd1313afcd74e46aa50fe92ce059807c55d86b87dcff219
data/.gitignore ADDED
@@ -0,0 +1,33 @@
1
+ # yard generated
2
+ doc
3
+ .yardoc
4
+
5
+ # bundler
6
+ .bundle
7
+
8
+ # jeweler generated
9
+ pkg
10
+
11
+ # rails stuff
12
+ log/*.log
13
+ test/dummy/db/*.sqlite3
14
+ test/dummy/log/*.log
15
+ test/dummy/tmp/
16
+ test/dummy/.sass-cache
17
+ Gemfile.lock
18
+
19
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
20
+ #
21
+ # * Create a file at ~/.gitignore
22
+ # * Include files you want ignored
23
+ # * Run: git config --global core.excludesfile ~/.gitignore
24
+ #
25
+ # After doing this, these files will be ignored in all your git projects,
26
+ # saving you from having to 'pollute' every project you touch with them
27
+ #
28
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
29
+ #
30
+ # For MacOS:
31
+ #
32
+ .DS_Store
33
+ .project
data/.rubocop.yml ADDED
@@ -0,0 +1,36 @@
1
+ AllCops:
2
+ Excludes:
3
+ - vendor/**/*
4
+
5
+ LineLength:
6
+ Enabled: false
7
+
8
+ MethodLength:
9
+ Enabled: false
10
+
11
+ ClassLength:
12
+ Enabled: false
13
+
14
+ Documentation:
15
+ # don't require classes to be documented
16
+ Enabled: false
17
+
18
+ Encoding:
19
+ # no need to always specify encoding
20
+ Enabled: false
21
+
22
+ FileName:
23
+ # allow grape-swagger.rb
24
+ Enabled: false
25
+
26
+ PredicateName:
27
+ Enabled: false
28
+
29
+ DoubleNegation:
30
+ Enabled: false
31
+
32
+ CyclomaticComplexity:
33
+ Enabled: false
34
+
35
+ ClassVars:
36
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ grape-swagger
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1.2
data/.travis.yml CHANGED
@@ -2,4 +2,7 @@ rvm:
2
2
  - jruby-19mode
3
3
  - 1.9.3
4
4
  - 2.0.0
5
+ - 2.1.2
6
+ - rbx-2.2.10
7
+
5
8
 
data/CHANGELOG.md ADDED
@@ -0,0 +1,90 @@
1
+ ### 0.8.0 (August 30, 2014)
2
+
3
+ #### Features
4
+
5
+ * [#139](https://github.com/tim-vandecasteele/grape-swagger/pull/139): Added support for `Rack::Multipart::UploadedFile` parameters - [@timgluz](https://github.com/timgluz).
6
+ * [#136](https://github.com/tim-vandecasteele/grape-swagger/pull/136), [#94](https://github.com/tim-vandecasteele/grape-swagger/pull/94): Recurse combination of namespaces when using mounted apps - [@renier](https://github.com/renier).
7
+ * [#100](https://github.com/tim-vandecasteele/grape-swagger/pull/100): Added ability to specify a nickname for an endpoint - [@lhorne](https://github.com/lhorne).
8
+ * [#94](https://github.com/tim-vandecasteele/grape-swagger/pull/94): Added support for namespace descriptions - [@renier](https://github.com/renier).
9
+ * [#110](https://github.com/tim-vandecasteele/grape-swagger/pull/110), [#111](https://github.com/tim-vandecasteele/grape-swagger/pull/111) - Added `responseModel` support - [@bagilevi](https://github.com/bagilevi).
10
+ * [#114](https://github.com/tim-vandecasteele/grape-swagger/pull/114): Added support for generating nested models from composed Grape Entities - [@dspaeth-faber](https://github.com/dspaeth-faber).
11
+ * [#124](https://github.com/tim-vandecasteele/grape-swagger/pull/124): Added ability to change the description and parameters of the API endpoints generated by grape-swagger - [@dblock](https://github.com/dblock).
12
+ * [#128](https://github.com/tim-vandecasteele/grape-swagger/pull/128): Combine global models and endpoint entities - [@dspaeth-faber](https://github.com/dspaeth-faber).
13
+ * [#132](https://github.com/tim-vandecasteele/grape-swagger/pull/132): Addes support for enum values in entity documentation and form parameters - [@Antek-drzewiecki](https://github.com/Antek-drzewiecki).
14
+ * [#142](https://github.com/tim-vandecasteele/grape-swagger/pull/142), [#143](https://github.com/tim-vandecasteele/grape-swagger/pull/143): Added support for kramdown, redcarpet and custom formatters - [@Antek-drzewiecki](https://github.com/Antek-drzewiecki).
15
+
16
+ #### Fixes
17
+
18
+ * [#105](https://github.com/tim-vandecasteele/grape-swagger/pull/105): Fixed compatibility with Swagger-UI - [@CraigCottingham](https://github.com/CraigCottingham).
19
+ * [#87](https://github.com/tim-vandecasteele/grape-swagger/pull/87): Fixed mapping of `default` to `defaultValue` - [@m-o-e](https://github.com/m-o-e).
20
+ * [#127](https://github.com/tim-vandecasteele/grape-swagger/pull/127): Fixed `undefined method 'reject' for nil:NilClass` error for an invalid route, now returning 404 Not Found - [@dblock](https://github.com/dblock).
21
+ * [#135](https://github.com/tim-vandecasteele/grape-swagger/pull/135): Fixed model inclusion in models with aliased references - [@cdarne](https://github.com/cdarne).
22
+
23
+ #### Dev
24
+
25
+ * [#126](https://github.com/tim-vandecasteele/grape-swagger/pull/126): Rewritten demo in the `test` folder with CORS enabled - [@dblock](https://github.com/dblock).
26
+ * Rewritten .gemspec and removed Jeweler - [@dblock](https://github.com/dblock).
27
+ * Added `GrapeSwagger::VERSION` - [@dblock](https://github.com/dblock).
28
+ * Added Rubocop, Ruby-style linter - [@dblock](https://github.com/dblock).
29
+
30
+ ### 0.7.2 (February 6, 2014)
31
+
32
+ * [#84](https://github.com/tim-vandecasteele/grape-swagger/pull/84): Markdown is now Github Flavored Markdown - [@jeromegn](https://github.com/jeromegn).
33
+ * [#83](https://github.com/tim-vandecasteele/grape-swagger/pull/83): Improved support for nested Entity types - [@jeromegn](https://github.com/jeromegn).
34
+ * [#79](https://github.com/tim-vandecasteele/grape-swagger/pull/79): Added `dataType` to the `params` output - [@Phobos98](https://github.com/Phobos98).
35
+ * [#75](https://github.com/tim-vandecasteele/grape-swagger/pull/75), [#82](https://github.com/tim-vandecasteele/grape-swagger/pull/82): Added Swagger 1.2 support - [@joelvh](https://github.com/joelvh), [@jeromegn](https://github.com/jeromegn).
36
+ * [#73](https://github.com/tim-vandecasteele/grape-swagger/pull/73): Added the ability to add additional API `info` - [@mattbeedle](https://github.com/mattbeedle).
37
+ * [#69](https://github.com/tim-vandecasteele/grape-swagger/pull/69): Make relative `base_path` values absolute - [@dm1try](https://github.com/dm1try).
38
+ * [#66](https://github.com/tim-vandecasteele/grape-swagger/pull/66): Fixed documentation generated for paths that don't match the base URL pattern - [@swistaczek](https://github.com/swistaczek).
39
+ * [#63](https://github.com/tim-vandecasteele/grape-swagger/pull/63): Added support for hiding endpoints from the documentation - [@arturoherrero](https://github.com/arturoherrero).
40
+ * [#62](https://github.com/tim-vandecasteele/grape-swagger/pull/62): Fixed handling of URLs with the `-` character - [@dadario](https://github.com/dadario).
41
+ * [#57](https://github.com/tim-vandecasteele/grape-swagger/pull/57): Fixed documenting of multiple API versions - [@Drakula2k](https://github.com/Drakula2k).
42
+ * [#58](https://github.com/tim-vandecasteele/grape-swagger/pull/58): Fixed resource groupings for prefixed APIs - [@aew](https://github.com/aew).
43
+ * [#56](https://github.com/tim-vandecasteele/grape-swagger/pull/56): Fixed `hide_documentation_path` on prefixed APIs - [@spier](https://github.com/spier).
44
+ * [#54](https://github.com/tim-vandecasteele/grape-swagger/pull/54): Adding support for generating swagger `responseClass` and models from Grape Entities - [@calebwoods](https://github.com/calebwoods).
45
+ * [#46](https://github.com/tim-vandecasteele/grape-swagger/pull/46): Fixed translating parameter `type` to String, enables using Mongoid fields as parameter definitions - [@dblock](https://github.com/dblock).
46
+
47
+ ### 0.6.0 (June 19, 2013)
48
+
49
+ * Added Rails 4 support - [@jrhe](https://github.com/jrhe).
50
+ * Fix: document APIs at root level - [@dblock](https://github.com/dblock).
51
+ * Added support for procs in basepath - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
52
+ * Support both `:desc` and `:description` when describing parameters - [@dblock](https://github.com/dblock).
53
+ * Fix: allow parameters such as `name[]` - [@dblock](https://github.com/dblock).
54
+
55
+ ### 0.5.0 (March 28, 2013)
56
+
57
+ * Added Grape 0.5.0 support - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
58
+
59
+ ### 0.4.0 (March 28, 2013)
60
+
61
+ * Support https - [@cutalion](https://github.com/cutalion).
62
+
63
+ ### 0.3.0 (October 19, 2012)
64
+
65
+ * Added version support - [@agileanimal](https://github.com/agileanimal), [@fknappe](https://github.com/fknappe).
66
+ * Added support for nested parameters - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
67
+ * Added basic support for specifying parameters that need to be passed in the header - [@agileanimal](https://github.com/agileanimal).
68
+ * Add possibility to hide the documentation paths in the generated swagger documentation - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
69
+
70
+ ### 0.2.1 (August 17, 2012)
71
+
72
+ * Added support for markdown in notes field - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
73
+ * Fix: compatibility with Rails - [@qwert666](https://github.com/qwert666).
74
+ * Fix: swagger UI history - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
75
+
76
+ ### 0.2.0 (July 27, 2012)
77
+
78
+ * Use resource as root for swagger - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
79
+ * Added support for file uploads, and proper `paramType` - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
80
+ * Added tests - [@nathanvda](https://github.com/nathanvda).
81
+
82
+ ### 0.1.0 (July 19, 2012)
83
+
84
+ * Added some configurability to the generated documentation - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
85
+ * Adapted to rails plugin structure - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
86
+ * Allowed cross origin, so swagger can be used from official site - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
87
+
88
+ ### 0.0.0 (July 19, 2012)
89
+
90
+ * Initial public release - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,126 @@
1
+ # Contributing to Grape-Swagger
2
+
3
+ This project is work of [many contributors](https://github.com/tim-vandecasteele/grape-swagger/graphs/contributors).
4
+ You're encouraged to submit [pull requests](https://github.com/tim-vandecasteele/grape-swagger/pulls),
5
+ [propose features and discuss issues](https://github.com/tim-vandecasteele/grape-swagger/issues).
6
+ When in doubt, ask a question in the [Grape Google Group](http://groups.google.com/group/ruby-grape).
7
+
8
+ In the examples below, substitute your Github username for `contributor` in URLs.
9
+
10
+ ## Fork the Project
11
+
12
+ Fork the [project on Github](https://github.com/tim-vandecasteele/grape-swagger) and check out your copy.
13
+
14
+ ```
15
+ git clone https://github.com/contributor/grape-swagger.git
16
+ cd grape-swagger
17
+ git remote add upstream https://github.com/tim-vandecasteele/grape-swagger.git
18
+ ```
19
+
20
+ ## Create a Topic Branch
21
+
22
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
23
+
24
+ ```
25
+ git checkout master
26
+ git pull upstream master
27
+ git checkout -b my-feature-branch
28
+ ```
29
+
30
+ ## Bundle Install and Test
31
+
32
+ Ensure that you can build the project and run tests.
33
+
34
+ ```
35
+ bundle install
36
+ bundle exec rake
37
+ ```
38
+
39
+ ## Write Tests
40
+
41
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build.
42
+ Add to [spec](spec).
43
+
44
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
45
+
46
+ ## Write Code
47
+
48
+ Implement your feature or bug fix.
49
+
50
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop).
51
+ Run `bundle exec rubocop` and fix any style issues highlighted.
52
+
53
+ Make sure that `bundle exec rake` completes without errors.
54
+
55
+ ## Write Documentation
56
+
57
+ Document any external behavior in the [README](README.md).
58
+
59
+ ## Update Changelog
60
+
61
+ Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*.
62
+ Make it look like every other line, including your name and link to your Github account.
63
+
64
+ ## Commit Changes
65
+
66
+ Make sure git knows your name and email address:
67
+
68
+ ```
69
+ git config --global user.name "Your Name"
70
+ git config --global user.email "contributor@example.com"
71
+ ```
72
+
73
+ Writing good commit logs is important. A commit log should describe what changed and why.
74
+
75
+ ```
76
+ git add ...
77
+ git commit
78
+ ```
79
+
80
+ ## Push
81
+
82
+ ```
83
+ git push origin my-feature-branch
84
+ ```
85
+
86
+ ## Make a Pull Request
87
+
88
+ Go to https://github.com/contributor/grape and select your feature branch.
89
+ Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
90
+
91
+ ## Rebase
92
+
93
+ If you've been working on a change for a while, rebase with upstream/master.
94
+
95
+ ```
96
+ git fetch upstream
97
+ git rebase upstream/master
98
+ git push origin my-feature-branch -f
99
+ ```
100
+
101
+ ## Update CHANGELOG Again
102
+
103
+ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
104
+
105
+ ```
106
+ * [#123](https://github.com/tim-vandecasteele/grape-swagger/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
107
+ ```
108
+
109
+ Amend your previous commit and force push the changes.
110
+
111
+ ```
112
+ git commit --amend
113
+ git push origin my-feature-branch -f
114
+ ```
115
+
116
+ ## Check on Your Pull Request
117
+
118
+ Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
119
+
120
+ ## Be Patient
121
+
122
+ It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
123
+
124
+ ## Thank You
125
+
126
+ Please do know that we really appreciate and value your time and work. We love you, really.
data/Gemfile CHANGED
@@ -1,23 +1,3 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
5
2
 
6
- gem 'grape', '>= 0.2.0'
7
- gem 'grape-entity', '>= 0.3.0'
8
- gem 'kramdown', '>= 1.3.1'
9
-
10
- # Add dependencies to develop your gem here.
11
- # Include everything needed to run rake, tests, features, etc.
12
- group :development do
13
- gem "shoulda", ">= 0"
14
- gem "rdoc", "~> 3.12"
15
- gem "bundler", "> 1.0.0"
16
- gem "jeweler", "~> 1.8.4"
17
-
18
- gem "pry"
19
-
20
- gem "rack-test"
21
-
22
- gem "rspec"
23
- end
3
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Tim Vandecasteele
1
+ Copyright (c) 2012-2014 Tim Vandecasteele 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 ADDED
@@ -0,0 +1,397 @@
1
+ # grape-swagger
2
+
3
+ [![Build Status](https://travis-ci.org/tim-vandecasteele/grape-swagger.svg?branch=master)](https://travis-ci.org/tim-vandecasteele/grape-swagger)
4
+
5
+ ## What is grape-swagger?
6
+
7
+ The grape-swagger gem provides an autogenerated documentation for your [Grape](https://github.com/intridea/grape) API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in [Swagger UI](https://github.com/wordnik/swagger-ui). You should be able to point [the petstore demo](http://petstore.swagger.wordnik.com) to your API.
8
+
9
+ ![Demo Screenshot](test/splines.png)
10
+
11
+ ## Related Projects
12
+
13
+ * [Grape](https://github.com/intridea/grape)
14
+ * [Swagger UI](https://github.com/wordnik/swagger-ui)
15
+
16
+ ## Installation
17
+
18
+ Add to your Gemfile:
19
+
20
+ ```gem 'grape-swagger'```
21
+
22
+ ## Upgrade
23
+
24
+ Please see [UPGRADING](UPGRADING.md) when upgrading from a previous version.
25
+
26
+ ## Usage
27
+
28
+ Mount all your different APIs (with ```Grape::API``` superclass) on a root node. In the root class definition, include ```add_swagger_documentation```, this sets up the system and registers the documentation on '/swagger_doc.json'. See [test/api.rb](test/api.rb) for a simple demo.
29
+
30
+
31
+ ``` ruby
32
+ require 'grape-swagger'
33
+
34
+ module API
35
+ class Root < Grape::API
36
+ mount API::Cats
37
+ mount API::Dogs
38
+ mount API::Pirates
39
+ add_swagger_documentation
40
+ end
41
+ end
42
+ ```
43
+
44
+ To explore your API, either download [Swagger UI](https://github.com/wordnik/swagger-ui) and set it up yourself or go to the [online swagger demo](http://petstore.swagger.wordnik.com/) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc.json).
45
+
46
+ ### CORS
47
+
48
+ If you use the online demo, make sure your API supports foreign requests by enabling CORS in Grape, otherwise you'll see the API description, but requests on the API won't return. Use [rack-cors](https://github.com/cyu/rack-cors) to enable CORS.
49
+
50
+ ```` ruby
51
+ require 'rack/cors'
52
+ use Rack::Cors do
53
+ allow do
54
+ origins '*'
55
+ resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
56
+ end
57
+ end
58
+ ```
59
+
60
+ Alternatively you can set CORS headers in a Grape `before` block.
61
+
62
+ ``` ruby
63
+ before do
64
+ header['Access-Control-Allow-Origin'] = '*'
65
+ header['Access-Control-Request-Method'] = '*'
66
+ end
67
+ ````
68
+
69
+ ## Configure
70
+
71
+ You can pass a hash with optional configuration settings to ```add_swagger_documentation```.
72
+
73
+ #### target_class
74
+
75
+ The API class to document, default `self`.
76
+
77
+ #### mount_path
78
+
79
+ The path where the API documentation is loaded, default is `/swagger_doc`.
80
+
81
+ #### class_name
82
+
83
+ API class name.
84
+
85
+ #### markdown
86
+
87
+ Allow markdown in `notes`, default is `nil`. (disabled) See below for details.
88
+
89
+ #### hide_format
90
+
91
+ Don't add `.(format)` to the end of URLs, default is `false`.
92
+
93
+ #### api_version
94
+
95
+ Version of the API that's being exposed.
96
+
97
+ #### base_path
98
+
99
+ Base path of the API that's being exposed. This configuration parameter accepts a `proc` to evaluate `base_path`, useful when you need to use request attributes to determine its value.
100
+
101
+ #### authorizations
102
+
103
+ This value is added to the `authorizations` key in the JSON documentation.
104
+
105
+ #### root_base_path
106
+
107
+ Add `basePath` key to the JSON documentation, default is `true`.
108
+
109
+ #### models
110
+
111
+ A list of entities to document. Combine with the [grape-entity](https://github.com/intridea/grape-entity) gem. See below for details.
112
+
113
+ #### hide_documentation_path
114
+
115
+ Don't show the `/swagger_doc` path in the generated swagger documentation.
116
+
117
+ #### format
118
+
119
+ Documentation response format, default is `:json`.
120
+
121
+ #### info
122
+
123
+ A hash merged into the `info` key of the JSON documentation. This may contain:
124
+
125
+ * `:title`: The API title to be displayed on the API homepage.
126
+ * `:description`: A description of the API.
127
+ * `:contact`: Contact email.
128
+ * `:license`: The name of the license.
129
+ * `:license_url`: The URL of the license.
130
+ * `:terms_of_service_url`: The URL of the API terms and conditions.
131
+
132
+ #### api_documentation
133
+
134
+ Customize the Swagger API documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description".
135
+
136
+ ``` ruby
137
+ add_swagger_documentation \
138
+ api_documentation: { desc: 'Reticulated splines API swagger-compatible documentation.' }
139
+ ```
140
+
141
+ #### specific_api_documentation
142
+
143
+ Customize the Swagger API specific documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description for specific API".
144
+
145
+ ``` ruby
146
+ add_swagger_documentation \
147
+ specific_api_documentation: { desc: 'Reticulated splines API swagger-compatible endpoint documentation.' }
148
+ ```
149
+
150
+ ## Swagger Header Parameters
151
+
152
+ Swagger also supports the documentation of parameters passed in the header. Since grape's ```params[]``` doesn't return header parameters we can specify header parameters seperately in a block after the description.
153
+
154
+ ``` ruby
155
+ desc "Return super-secret information", {
156
+ headers: {
157
+ "XAuthToken" => {
158
+ description: "Valdates your identity",
159
+ required: true
160
+ },
161
+ "XOptionalHeader" => {
162
+ description: "Not really needed",
163
+ required: false
164
+ }
165
+ }
166
+ }
167
+ ```
168
+
169
+ ## Hiding an Endpoint
170
+
171
+ You can hide an endpoint by adding ```hidden: true``` in the description of the endpoint:
172
+
173
+ ``` ruby
174
+ desc 'Hide this endpoint', hidden: true
175
+ ```
176
+
177
+ ## Overriding Auto-Generated Nicknames
178
+
179
+ You can specify a swagger nickname to use instead of the auto generated name by adding `:nickname 'string'``` in the description of the endpoint.
180
+
181
+ ``` ruby
182
+ desc 'Get a full list of pets', nickname: 'getAllPets'
183
+ ```
184
+
185
+ ## Grape Entities
186
+
187
+ Add the [grape-entity](https://github.com/agileanimal/grape-entity) gem to our Gemfile.
188
+
189
+ The following example exposes statuses. And exposes statuses documentation adding :type and :desc.
190
+
191
+ ```ruby
192
+ module API
193
+ module Entities
194
+ class Status < Grape::Entity
195
+ expose :text, documentation: { type: 'string', desc: 'Status update text.' }
196
+ expose :links, using: Link, documentation: { type: 'link', is_array: true }
197
+ expose :numbers, documentation: { type: 'integer', desc: 'favourite number', values: [1,2,3,4] }
198
+ end
199
+
200
+ class Link < Grape::Entity
201
+ def self.entity_name
202
+ 'link'
203
+ end
204
+
205
+ expose :href, documentation: { type: 'url' }
206
+ expose :rel, documentation: { type: 'string'}
207
+ end
208
+ end
209
+
210
+ class Statuses < Grape::API
211
+ version 'v1'
212
+
213
+ desc 'Statuses index', entity: API::Entities::Status
214
+ get '/statuses' do
215
+ statuses = Status.all
216
+ type = current_user.admin? ? :full : :default
217
+ present statuses, with: API::Entities::Status, type: type
218
+ end
219
+
220
+ desc 'Creates a new status', entity: API::Entities::Status, params: API::Entities::Status.documentation
221
+ post '/statuses' do
222
+ ...
223
+ end
224
+ end
225
+ end
226
+ ```
227
+
228
+ ### Relationships
229
+
230
+ #### 1xN
231
+
232
+ ```ruby
233
+ module API
234
+ module Entities
235
+ class Client < Grape::Entity
236
+ expose :name, documentation: { type: 'string', desc: 'Name' }
237
+ expose :addresses, using: Entities::Address,
238
+ documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: true }
239
+ end
240
+
241
+ class Address < Grape::Entity
242
+ expose :street, documentation: { type: 'string', desc: 'Street.' }
243
+ end
244
+ end
245
+
246
+ class Clients < Grape::API
247
+ version 'v1'
248
+
249
+ desc 'Clients index', params: Entities::Client.documentation
250
+ get '/clients' do
251
+ ...
252
+ end
253
+ end
254
+
255
+ add_swagger_documentation models: [Entities::Client, Entities::Address]
256
+ end
257
+ ```
258
+
259
+ #### 1x1
260
+
261
+ Note: `is_array` is `false` by default.
262
+
263
+ ```ruby
264
+ module API
265
+ module Entities
266
+ class Client < Grape::Entity
267
+ expose :name, documentation: { type: 'string', desc: 'Name' }
268
+ expose :address, using: Entities::Address,
269
+ documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: false }
270
+ end
271
+
272
+ class Address < Grape::Entity
273
+ expose :street, documentation: { type: 'string', desc: 'Street' }
274
+ end
275
+ end
276
+
277
+ class Clients < Grape::API
278
+ version 'v1'
279
+
280
+ desc 'Clients index', params: Entities::Client.documentation
281
+ get '/clients' do
282
+ ...
283
+ end
284
+ end
285
+
286
+ add_swagger_documentation models: [Entities::Client, Entities::Address]
287
+ end
288
+ ```
289
+
290
+ ## Markdown in Notes
291
+
292
+ The grape-swagger gem allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Swagger UI.
293
+ Grape-swagger uses adapters for several markdown formatters. It includes adapters for [kramdown](http://kramdown.rubyforge.org) (kramdown [syntax](http://kramdown.rubyforge.org/syntax.html)) and [redcarpet](https://github.com/vmg/redcarpet).
294
+ The adapters are packed in the GrapeSwagger::Markdown modules. We do not include the markdown gems in our gemfile, so be sure to include or install the depended gems.
295
+
296
+
297
+ ### Kramdown
298
+ If you want to use kramdown as markdown formatter, you need to add kramdown to your gemfile.
299
+
300
+ ```
301
+ gem 'kramdown'
302
+ ```
303
+
304
+ Configure your api documentation route with:
305
+
306
+
307
+ ``` ruby
308
+ add_swagger_documentation(
309
+ markdown: GrapeSwagger::Markdown::KramdownAdapter
310
+ )
311
+ ```
312
+
313
+ Finally you can write endpoint descriptions the with markdown enabled.
314
+
315
+ ``` ruby
316
+ desc "Reserve a virgin in heaven", {
317
+ notes: <<-NOTE
318
+ Virgins in Heaven
319
+ -----------------
320
+
321
+ > A virgin doesn't come for free
322
+
323
+ If you want to reserve a virgin in heaven, you have to do
324
+ some crazy stuff on earth.
325
+
326
+ def do_good
327
+ puts 'help people'
328
+ end
329
+
330
+ * _Will go to Heaven:_ Probably
331
+ * _Will go to Hell:_ Probably not
332
+ NOTE
333
+ }
334
+ ```
335
+
336
+ ### Redcarpet
337
+ As alternative you can use [redcarpet](https://github.com/vmg/redcarpet) as formatter, you need to include redcarpet in your gemspec. If you also want to use [rouge](https://github.com/jneen/rouge) as syntax highlighter you also need to include it.
338
+
339
+ ```
340
+ gem 'redcarpet'
341
+ gem 'rouge'
342
+ ```
343
+
344
+ Configure your api documentation route with:
345
+
346
+ ``` ruby
347
+ add_swagger_documentation(
348
+ markdown: GrapeSwagger::Markdown::RedcarpetAdapter.new(render_options: { highlighter: :rouge })
349
+ )
350
+ ```
351
+
352
+ Alternatively you can disable rouge by adding `:none` as highlighter option. You can add redcarpet extensions and render options trough the `extenstions:` and `render_options:` parameters.
353
+
354
+ ### Custom markdown formatter
355
+ You can also add your custom adapter for your favourite markdown formatter, as long it responds to the method `markdown(text)` and it formats the given text.
356
+
357
+ ``` ruby
358
+ module API
359
+
360
+ class MySuperbMarkdownFormatterAdapter
361
+ attr_reader :adapter
362
+
363
+ def initialize(options)
364
+ require 'superbmarkdownformatter'
365
+ @adapter = SuperbMarkdownFormatter.new options
366
+ end
367
+
368
+ def markdown(text)
369
+ @adapter.render_supreme(text)
370
+ end
371
+ end
372
+
373
+ add_swagger_documentation markdown: MySuperbMarkdownFormatterAdapter.new(no_links: true)
374
+ end
375
+
376
+ ```
377
+
378
+ ## Response documentation
379
+
380
+ You can also document the HTTP status codes with a description and a specified model that your API returns with the following syntax.
381
+
382
+ ``` ruby
383
+ get '/', http_codes: [
384
+ [200, 'Ok', Entities::Client],
385
+ [400, "Invalid parameter entry"]
386
+ ] do
387
+ ...
388
+ end
389
+ ```
390
+
391
+ ## Contributing to grape-swagger
392
+
393
+ See [CONTRIBUTING](CONTRIBUTING.md).
394
+
395
+ ## Copyright and License
396
+
397
+ Copyright (c) 2012-2014 Tim Vandecasteele and contributors. See [LICENSE.txt](LICENSE.txt) for details.