gitlab-grape-swagger 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.github/dependabot.yml +20 -0
- data/.github/workflows/ci.yml +45 -0
- data/.gitignore +44 -0
- data/.gitlab-ci.yml +19 -0
- data/.rspec +3 -0
- data/.rubocop.yml +136 -0
- data/.rubocop_todo.yml +60 -0
- data/.ruby-gemset +1 -0
- data/CHANGELOG.md +671 -0
- data/CONTRIBUTING.md +126 -0
- data/Dangerfile +3 -0
- data/Gemfile +45 -0
- data/Gemfile.lock +249 -0
- data/LICENSE.txt +20 -0
- data/README.md +1772 -0
- data/RELEASING.md +82 -0
- data/Rakefile +20 -0
- data/UPGRADING.md +201 -0
- data/example/api/endpoints.rb +131 -0
- data/example/api/entities.rb +18 -0
- data/example/config.ru +42 -0
- data/example/example_requests.postman_collection +146 -0
- data/example/splines.png +0 -0
- data/example/swagger-example.png +0 -0
- data/grape-swagger.gemspec +23 -0
- data/lib/grape-swagger/doc_methods/build_model_definition.rb +68 -0
- data/lib/grape-swagger/doc_methods/data_type.rb +110 -0
- data/lib/grape-swagger/doc_methods/extensions.rb +101 -0
- data/lib/grape-swagger/doc_methods/file_params.rb +17 -0
- data/lib/grape-swagger/doc_methods/format_data.rb +53 -0
- data/lib/grape-swagger/doc_methods/headers.rb +20 -0
- data/lib/grape-swagger/doc_methods/move_params.rb +209 -0
- data/lib/grape-swagger/doc_methods/operation_id.rb +32 -0
- data/lib/grape-swagger/doc_methods/optional_object.rb +30 -0
- data/lib/grape-swagger/doc_methods/parse_params.rb +190 -0
- data/lib/grape-swagger/doc_methods/path_string.rb +52 -0
- data/lib/grape-swagger/doc_methods/produces_consumes.rb +15 -0
- data/lib/grape-swagger/doc_methods/status_codes.rb +21 -0
- data/lib/grape-swagger/doc_methods/tag_name_description.rb +34 -0
- data/lib/grape-swagger/doc_methods/version.rb +20 -0
- data/lib/grape-swagger/doc_methods.rb +142 -0
- data/lib/grape-swagger/endpoint/params_parser.rb +76 -0
- data/lib/grape-swagger/endpoint.rb +476 -0
- data/lib/grape-swagger/errors.rb +17 -0
- data/lib/grape-swagger/instance.rb +7 -0
- data/lib/grape-swagger/model_parsers.rb +42 -0
- data/lib/grape-swagger/rake/oapi_tasks.rb +135 -0
- data/lib/grape-swagger/version.rb +5 -0
- data/lib/grape-swagger.rb +174 -0
- data/spec/issues/267_nested_namespaces.rb +55 -0
- data/spec/issues/403_versions_spec.rb +124 -0
- data/spec/issues/427_entity_as_string_spec.rb +39 -0
- data/spec/issues/430_entity_definitions_spec.rb +94 -0
- data/spec/issues/532_allow_custom_format_spec.rb +42 -0
- data/spec/issues/533_specify_status_code_spec.rb +78 -0
- data/spec/issues/537_enum_values_spec.rb +50 -0
- data/spec/issues/539_array_post_body_spec.rb +65 -0
- data/spec/issues/542_array_of_type_in_post_body_spec.rb +46 -0
- data/spec/issues/553_align_array_put_post_params_spec.rb +152 -0
- data/spec/issues/572_array_post_body_spec.rb +51 -0
- data/spec/issues/579_align_put_post_parameters_spec.rb +185 -0
- data/spec/issues/582_file_response_spec.rb +55 -0
- data/spec/issues/587_range_parameter_delimited_by_dash_spec.rb +26 -0
- data/spec/issues/605_root_route_documentation_spec.rb +23 -0
- data/spec/issues/650_params_array_spec.rb +65 -0
- data/spec/issues/677_consumes_produces_add_swagger_documentation_options_spec.rb +100 -0
- data/spec/issues/680_keep_204_error_schemas_spec.rb +55 -0
- data/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb +62 -0
- data/spec/issues/751_deeply_nested_objects_spec.rb +190 -0
- data/spec/issues/776_multiple_presents_spec.rb +59 -0
- data/spec/issues/784_extensions_on_params_spec.rb +42 -0
- data/spec/issues/809_utf8_routes_spec.rb +55 -0
- data/spec/issues/832_array_hash_float_decimal_spec.rb +114 -0
- data/spec/issues/847_route_param_options_spec.rb +37 -0
- data/spec/issues/873_wildcard_segments_path_parameters_spec.rb +28 -0
- data/spec/issues/878_optional_path_segments_spec.rb +29 -0
- data/spec/issues/881_handle_file_params_spec.rb +38 -0
- data/spec/issues/883_query_array_parameter_spec.rb +46 -0
- data/spec/issues/884_dont_document_non_schema_examples_spec.rb +49 -0
- data/spec/issues/887_prevent_duplicate_operation_ids_spec.rb +35 -0
- data/spec/lib/data_type_spec.rb +111 -0
- data/spec/lib/endpoint/params_parser_spec.rb +124 -0
- data/spec/lib/endpoint_spec.rb +153 -0
- data/spec/lib/extensions_spec.rb +185 -0
- data/spec/lib/format_data_spec.rb +115 -0
- data/spec/lib/model_parsers_spec.rb +104 -0
- data/spec/lib/move_params_spec.rb +444 -0
- data/spec/lib/oapi_tasks_spec.rb +163 -0
- data/spec/lib/operation_id_spec.rb +55 -0
- data/spec/lib/optional_object_spec.rb +47 -0
- data/spec/lib/parse_params_spec.rb +68 -0
- data/spec/lib/path_string_spec.rb +101 -0
- data/spec/lib/produces_consumes_spec.rb +116 -0
- data/spec/lib/tag_name_description_spec.rb +80 -0
- data/spec/lib/version_spec.rb +28 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/support/empty_model_parser.rb +23 -0
- data/spec/support/grape_version.rb +13 -0
- data/spec/support/mock_parser.rb +23 -0
- data/spec/support/model_parsers/entity_parser.rb +334 -0
- data/spec/support/model_parsers/mock_parser.rb +346 -0
- data/spec/support/model_parsers/representable_parser.rb +406 -0
- data/spec/support/namespace_tags.rb +93 -0
- data/spec/support/the_paths_definitions.rb +109 -0
- data/spec/swagger_v2/api_documentation_spec.rb +42 -0
- data/spec/swagger_v2/api_swagger_v2_additional_properties_spec.rb +83 -0
- data/spec/swagger_v2/api_swagger_v2_body_definitions_spec.rb +48 -0
- data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +36 -0
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +79 -0
- data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +145 -0
- data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +137 -0
- data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +56 -0
- data/spec/swagger_v2/api_swagger_v2_hash_and_array_spec.rb +64 -0
- data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +58 -0
- data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +57 -0
- data/spec/swagger_v2/api_swagger_v2_hide_param_spec.rb +109 -0
- data/spec/swagger_v2/api_swagger_v2_ignore_defaults_spec.rb +48 -0
- data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +153 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +355 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +217 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +247 -0
- data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +80 -0
- data/spec/swagger_v2/api_swagger_v2_response_spec.rb +147 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_examples_spec.rb +135 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_headers_spec.rb +216 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_models_spec.rb +53 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_root_spec.rb +153 -0
- data/spec/swagger_v2/api_swagger_v2_spec.rb +245 -0
- data/spec/swagger_v2/api_swagger_v2_status_codes_spec.rb +93 -0
- data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +90 -0
- data/spec/swagger_v2/boolean_params_spec.rb +38 -0
- data/spec/swagger_v2/default_api_spec.rb +175 -0
- data/spec/swagger_v2/deprecated_field_spec.rb +25 -0
- data/spec/swagger_v2/description_not_initialized_spec.rb +39 -0
- data/spec/swagger_v2/endpoint_versioned_path_spec.rb +130 -0
- data/spec/swagger_v2/errors_spec.rb +77 -0
- data/spec/swagger_v2/float_api_spec.rb +36 -0
- data/spec/swagger_v2/form_params_spec.rb +76 -0
- data/spec/swagger_v2/grape-swagger_spec.rb +17 -0
- data/spec/swagger_v2/guarded_endpoint_spec.rb +162 -0
- data/spec/swagger_v2/hide_api_spec.rb +147 -0
- data/spec/swagger_v2/host_spec.rb +43 -0
- data/spec/swagger_v2/inheritance_and_discriminator_spec.rb +57 -0
- data/spec/swagger_v2/mount_override_api_spec.rb +58 -0
- data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +122 -0
- data/spec/swagger_v2/namespace_tags_spec.rb +78 -0
- data/spec/swagger_v2/namespaced_api_spec.rb +121 -0
- data/spec/swagger_v2/nicknamed_api_spec.rb +25 -0
- data/spec/swagger_v2/operation_id_api_spec.rb +27 -0
- data/spec/swagger_v2/param_multi_type_spec.rb +82 -0
- data/spec/swagger_v2/param_type_spec.rb +95 -0
- data/spec/swagger_v2/param_values_spec.rb +180 -0
- data/spec/swagger_v2/params_array_collection_format_spec.rb +105 -0
- data/spec/swagger_v2/params_array_spec.rb +225 -0
- data/spec/swagger_v2/params_example_spec.rb +38 -0
- data/spec/swagger_v2/params_hash_spec.rb +77 -0
- data/spec/swagger_v2/params_nested_spec.rb +92 -0
- data/spec/swagger_v2/parent_less_namespace_spec.rb +32 -0
- data/spec/swagger_v2/reference_entity_spec.rb +129 -0
- data/spec/swagger_v2/security_requirement_spec.rb +46 -0
- data/spec/swagger_v2/simple_mounted_api_spec.rb +332 -0
- data/spec/version_spec.rb +10 -0
- metadata +225 -0
data/README.md
ADDED
@@ -0,0 +1,1772 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/grape-swagger.svg)](http://badge.fury.io/rb/grape-swagger)
|
2
|
+
[![Build Status](https://travis-ci.org/ruby-grape/grape-swagger.svg?branch=master)](https://travis-ci.org/ruby-grape/grape-swagger)
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/github/ruby-grape/grape-swagger/badge.svg?branch=master)](https://coveralls.io/github/ruby-grape/grape-swagger?branch=master)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/ruby-grape/grape-swagger.svg)](https://codeclimate.com/github/ruby-grape/grape-swagger)
|
5
|
+
|
6
|
+
## Fork notice
|
7
|
+
|
8
|
+
This is a fork of https://github.com/ruby-grape/grape-swagger
|
9
|
+
|
10
|
+
We are forking this gem to be able to iterate faster. The original gem has some issues that we need to fix in order to generate a valid OpenAPI V2 documentation.
|
11
|
+
|
12
|
+
MRs created on the fork should also be able to be created on the original repository.
|
13
|
+
|
14
|
+
##### Table of Contents
|
15
|
+
|
16
|
+
* [What is grape-swagger?](#what)
|
17
|
+
* [Related Projects](#related)
|
18
|
+
* [Compatibility](#version)
|
19
|
+
* [Swagger-Spec](#swagger-spec)
|
20
|
+
* [Installation](#install)
|
21
|
+
* [Usage](#usage)
|
22
|
+
* [Model Parsers](#model_parsers)
|
23
|
+
* [Configure](#configure)
|
24
|
+
* [Routes Configuration](#routes)
|
25
|
+
* [Using Grape Entities](#grape-entity)
|
26
|
+
* [Securing the Swagger UI](#oauth)
|
27
|
+
* [Example](#example)
|
28
|
+
* [Rake Tasks](#rake)
|
29
|
+
|
30
|
+
|
31
|
+
## What is grape-swagger? <a name="what"></a>
|
32
|
+
|
33
|
+
The grape-swagger gem provides an autogenerated documentation for your [Grape](https://github.com/ruby-grape/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.io/) to your API.
|
34
|
+
|
35
|
+
![Demo Screenshot](example/swagger-example.png)
|
36
|
+
|
37
|
+
This screenshot is based on the [Hussars](https://github.com/LeFnord/hussars) sample app.
|
38
|
+
|
39
|
+
|
40
|
+
## Related Projects <a name="related"></a>
|
41
|
+
|
42
|
+
* [Grape](https://github.com/ruby-grape/grape)
|
43
|
+
* [Grape Swagger Entity](https://github.com/ruby-grape/grape-swagger-entity)
|
44
|
+
* [Grape Entity](https://github.com/ruby-grape/grape-entity)
|
45
|
+
* [Grape Swagger Representable](https://github.com/ruby-grape/grape-swagger-representable)
|
46
|
+
* [Swagger UI](https://github.com/wordnik/swagger-ui)
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
## Compatibility <a name="version"></a>
|
51
|
+
|
52
|
+
The following versions of grape, grape-entity and grape-swagger can currently be used together.
|
53
|
+
|
54
|
+
| grape-swagger | swagger spec | grape | grape-entity | representable |
|
55
|
+
| ------------- | ------------ | ----------------------- | ------------ | ------------- |
|
56
|
+
| 0.10.5 | 1.2 | >= 0.10.0 ... <= 0.14.0 | < 0.5.0 | n/a |
|
57
|
+
| 0.11.0 | 1.2 | >= 0.16.2 | < 0.5.0 | n/a |
|
58
|
+
| 0.25.2 | 2.0 | >= 0.14.0 ... <= 0.18.0 | <= 0.6.0 | >= 2.4.1 |
|
59
|
+
| 0.26.0 | 2.0 | >= 0.16.2 ... <= 1.1.0 | <= 0.6.1 | >= 2.4.1 |
|
60
|
+
| 0.27.0 | 2.0 | >= 0.16.2 ... <= 1.1.0 | >= 0.5.0 | >= 2.4.1 |
|
61
|
+
| 0.32.0 | 2.0 | >= 0.16.2 | >= 0.5.0 | >= 2.4.1 |
|
62
|
+
| 0.34.0 | 2.0 | >= 0.16.2 ... < 1.3.0 | >= 0.5.0 | >= 2.4.1 |
|
63
|
+
| >= 1.0.0 | 2.0 | >= 1.3.0 | >= 0.5.0 | >= 2.4.1 |
|
64
|
+
|
65
|
+
|
66
|
+
## Swagger-Spec <a name="swagger-spec"></a>
|
67
|
+
|
68
|
+
Grape-swagger generates documentation per [Swagger / OpenAPI Spec 2.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md).
|
69
|
+
|
70
|
+
<!-- validating it with: http://bigstickcarpet.com/swagger-parser/www/index.html -->
|
71
|
+
|
72
|
+
|
73
|
+
## Installation <a name="install"></a>
|
74
|
+
|
75
|
+
Add to your Gemfile:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
gem 'grape-swagger'
|
79
|
+
```
|
80
|
+
|
81
|
+
## Upgrade
|
82
|
+
|
83
|
+
Please see [UPGRADING](UPGRADING.md) when upgrading from a previous version.
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
## Usage <a name="usage"></a>
|
88
|
+
|
89
|
+
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'. See [example/config.ru](example/config.ru) for a simple demo.
|
90
|
+
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'grape-swagger'
|
94
|
+
|
95
|
+
module API
|
96
|
+
class Root < Grape::API
|
97
|
+
format :json
|
98
|
+
mount API::Cats
|
99
|
+
mount API::Dogs
|
100
|
+
mount API::Pirates
|
101
|
+
add_swagger_documentation
|
102
|
+
end
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
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).
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
## Model Parsers <a name="model_parsers"></a>
|
111
|
+
|
112
|
+
Since 0.21.0, `Grape::Entity` is not a part of grape-swagger, you need to add `grape-swagger-entity` manually to your Gemfile.
|
113
|
+
Also added support for [representable](https://github.com/apotonick/representable) via `grape-swagger-representable`.
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
# For Grape::Entity ( https://github.com/ruby-grape/grape-entity )
|
117
|
+
gem 'grape-swagger-entity', '~> 0.3'
|
118
|
+
# For representable ( https://github.com/apotonick/representable )
|
119
|
+
gem 'grape-swagger-representable', '~> 0.2'
|
120
|
+
```
|
121
|
+
|
122
|
+
If you are not using Rails, make sure to load the parser inside your application initialization logic, e.g., via `require 'grape-swagger/entity'` or `require 'grape-swagger/representable'`.
|
123
|
+
|
124
|
+
### Custom Model Parsers
|
125
|
+
|
126
|
+
You can create your own model parser, for example for [roar](https://github.com/apotonick/roar).
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
module GrapeSwagger
|
130
|
+
module Roar
|
131
|
+
class Parser
|
132
|
+
attr_reader :model
|
133
|
+
attr_reader :endpoint
|
134
|
+
|
135
|
+
def initialize(model, endpoint)
|
136
|
+
@model = model
|
137
|
+
@endpoint = endpoint
|
138
|
+
end
|
139
|
+
|
140
|
+
def call
|
141
|
+
# Parse your model and return hash with model schema for swagger
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
Then you should register your custom parser.
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
GrapeSwagger.model_parsers.register(GrapeSwagger::Roar::Parser, Roar::Decorator)
|
152
|
+
```
|
153
|
+
|
154
|
+
To control model parsers sequence, you can insert your parser before or after another parser.
|
155
|
+
|
156
|
+
#### insert_before
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
GrapeSwagger.model_parsers.insert_before(GrapeSwagger::Representable::Parser, GrapeSwagger::Roar::Parser, Roar::Decorator)
|
160
|
+
```
|
161
|
+
|
162
|
+
#### insert_after
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
GrapeSwagger.model_parsers.insert_after(GrapeSwagger::Roar::Parser, GrapeSwagger::Representable::Parser, Representable::Decorator)
|
166
|
+
```
|
167
|
+
|
168
|
+
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`.
|
169
|
+
|
170
|
+
|
171
|
+
### CORS
|
172
|
+
|
173
|
+
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.
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
require 'rack/cors'
|
177
|
+
use Rack::Cors do
|
178
|
+
allow do
|
179
|
+
origins '*'
|
180
|
+
resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
|
181
|
+
end
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
185
|
+
Alternatively you can set CORS headers in a Grape `before` block.
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
before do
|
189
|
+
header['Access-Control-Allow-Origin'] = '*'
|
190
|
+
header['Access-Control-Request-Method'] = '*'
|
191
|
+
end
|
192
|
+
```
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
## Configure <a name="configure"></a>
|
197
|
+
|
198
|
+
* [host](#host)
|
199
|
+
* [base_path](#base_path)
|
200
|
+
* [mount_path](#mount_path)
|
201
|
+
* [add_base_path](#add_base_path)
|
202
|
+
* [add_root](#add_root)
|
203
|
+
* [add_version](#add_version)
|
204
|
+
* [doc_version](#doc_version)
|
205
|
+
* [endpoint_auth_wrapper](#endpoint_auth_wrapper)
|
206
|
+
* [swagger_endpoint_guard](#swagger_endpoint_guard)
|
207
|
+
* [token_owner](#token_owner)
|
208
|
+
* [security_definitions](#security_definitions)
|
209
|
+
* [security](#security)
|
210
|
+
* [models](#models)
|
211
|
+
* [tags](#tags)
|
212
|
+
* [hide_documentation_path](#hide_documentation_path)
|
213
|
+
* [info](#info)
|
214
|
+
* [array_use_braces](#array_use_braces)
|
215
|
+
* [api_documentation](#api_documentation)
|
216
|
+
* [specific_api_documentation](#specific_api_documentation)
|
217
|
+
* [consumes](#consumes)
|
218
|
+
* [produces](#produces)
|
219
|
+
|
220
|
+
You can pass a hash with optional configuration settings to ```add_swagger_documentation```.
|
221
|
+
The examples show the default value.
|
222
|
+
|
223
|
+
|
224
|
+
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:
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
add_swagger_documentation \
|
228
|
+
base_path: proc { |request| request.host =~ /^example/ ? '/api-example' : '/api' }
|
229
|
+
```
|
230
|
+
|
231
|
+
|
232
|
+
#### host: <a name="host"></a>
|
233
|
+
Sets explicit the `host`, default would be taken from `request`.
|
234
|
+
```ruby
|
235
|
+
add_swagger_documentation \
|
236
|
+
host: 'www.example.com'
|
237
|
+
```
|
238
|
+
|
239
|
+
|
240
|
+
#### base_path: <a name="base_path"></a>
|
241
|
+
Base path of the API that's being exposed, default would be taken from `request`.
|
242
|
+
```ruby
|
243
|
+
add_swagger_documentation \
|
244
|
+
base_path: nil
|
245
|
+
```
|
246
|
+
|
247
|
+
`host` and `base_path` are also accepting a `proc` or `lambda`
|
248
|
+
|
249
|
+
|
250
|
+
#### mount_path: <a name="mount_path"></a>
|
251
|
+
The path where the API documentation is loaded, default is: `/swagger_doc`.
|
252
|
+
```ruby
|
253
|
+
add_swagger_documentation \
|
254
|
+
mount_path: '/swagger_doc'
|
255
|
+
```
|
256
|
+
|
257
|
+
#### add_base_path: <a name="add_base_path"></a>
|
258
|
+
Add `basePath` key to the documented path keys, default is: `false`.
|
259
|
+
```ruby
|
260
|
+
add_swagger_documentation \
|
261
|
+
add_base_path: true # only if base_path given
|
262
|
+
```
|
263
|
+
|
264
|
+
#### add_root: <a name="add_root"></a>
|
265
|
+
Add root element to all the responses, default is: `false`.
|
266
|
+
```ruby
|
267
|
+
add_swagger_documentation \
|
268
|
+
add_root: true
|
269
|
+
```
|
270
|
+
|
271
|
+
#### add_version: <a name="add_version"></a>
|
272
|
+
|
273
|
+
Add `version` key to the documented path keys, default is: `true`,
|
274
|
+
here the version is the API version, specified by `grape` in [`path`](https://github.com/ruby-grape/grape/#path)
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
add_swagger_documentation \
|
278
|
+
add_version: true
|
279
|
+
```
|
280
|
+
|
281
|
+
|
282
|
+
#### doc_version: <a name="doc_version"></a>
|
283
|
+
|
284
|
+
Specify the version of the documentation at [info section](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#info-object), default is: `'0.0.1'`
|
285
|
+
```ruby
|
286
|
+
add_swagger_documentation \
|
287
|
+
doc_version: '0.0.1'
|
288
|
+
```
|
289
|
+
|
290
|
+
|
291
|
+
#### endpoint_auth_wrapper: <a name="endpoint_auth_wrapper"></a>
|
292
|
+
|
293
|
+
Specify the middleware to use for securing endpoints.
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
add_swagger_documentation \
|
297
|
+
endpoint_auth_wrapper: WineBouncer::OAuth2
|
298
|
+
```
|
299
|
+
|
300
|
+
|
301
|
+
#### swagger_endpoint_guard: <a name="swagger_endpoint_guard"></a>
|
302
|
+
Specify the method and auth scopes, used by the middleware for securing endpoints.
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
add_swagger_documentation \
|
306
|
+
swagger_endpoint_guard: 'oauth2 false'
|
307
|
+
```
|
308
|
+
|
309
|
+
|
310
|
+
#### token_owner: <a name="token_owner"></a>
|
311
|
+
Specify the token_owner method, provided by the middleware, which is typically named 'resource_owner'.
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
add_swagger_documentation \
|
315
|
+
token_owner: 'resource_owner'
|
316
|
+
```
|
317
|
+
|
318
|
+
|
319
|
+
#### security_definitions: <a name="security_definitions"></a>
|
320
|
+
Specify the [Security Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-definitions-object)
|
321
|
+
|
322
|
+
_NOTE: [Swagger-UI is supporting only implicit flow yet](https://github.com/swagger-api/swagger-ui/issues/2406#issuecomment-248651879)_
|
323
|
+
|
324
|
+
```ruby
|
325
|
+
add_swagger_documentation \
|
326
|
+
security_definitions: {
|
327
|
+
api_key: {
|
328
|
+
type: "apiKey",
|
329
|
+
name: "api_key",
|
330
|
+
in: "header"
|
331
|
+
}
|
332
|
+
}
|
333
|
+
```
|
334
|
+
|
335
|
+
#### security: <a name="security"></a>
|
336
|
+
|
337
|
+
Specify the [Security Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#securityRequirementObject)
|
338
|
+
|
339
|
+
```ruby
|
340
|
+
add_swagger_documentation \
|
341
|
+
security: [
|
342
|
+
{
|
343
|
+
api_key: []
|
344
|
+
}
|
345
|
+
]
|
346
|
+
```
|
347
|
+
|
348
|
+
|
349
|
+
#### models: <a name="models"></a>
|
350
|
+
A list of entities to document. Combine with the [grape-entity](https://github.com/ruby-grape/grape-entity) gem.
|
351
|
+
|
352
|
+
These would be added to the definitions section of the swagger file.
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
add_swagger_documentation \
|
356
|
+
models: [
|
357
|
+
TheApi::Entities::UseResponse,
|
358
|
+
TheApi::Entities::ApiError
|
359
|
+
]
|
360
|
+
```
|
361
|
+
|
362
|
+
|
363
|
+
#### tags: <a name="tags"></a>
|
364
|
+
|
365
|
+
A list of tags to document. By default tags are automatically generated
|
366
|
+
for endpoints based on route names.
|
367
|
+
|
368
|
+
```ruby
|
369
|
+
add_swagger_documentation \
|
370
|
+
tags: [
|
371
|
+
{ name: 'widgets', description: 'A description of widgets' }
|
372
|
+
]
|
373
|
+
```
|
374
|
+
|
375
|
+
|
376
|
+
#### hide_documentation_path: (default: `true`) <a name="hide_documentation_path"></a>
|
377
|
+
|
378
|
+
```ruby
|
379
|
+
add_swagger_documentation \
|
380
|
+
hide_documentation_path: true
|
381
|
+
```
|
382
|
+
|
383
|
+
Don't show the `/swagger_doc` path in the generated swagger documentation.
|
384
|
+
|
385
|
+
|
386
|
+
#### info: <a name="info"></a>
|
387
|
+
|
388
|
+
```ruby
|
389
|
+
add_swagger_documentation \
|
390
|
+
info: {
|
391
|
+
title: "The API title to be displayed on the API homepage.",
|
392
|
+
description: "A description of the API.",
|
393
|
+
contact_name: "Contact name",
|
394
|
+
contact_email: "Contact@email.com",
|
395
|
+
contact_url: "Contact URL",
|
396
|
+
license: "The name of the license.",
|
397
|
+
license_url: "www.The-URL-of-the-license.org",
|
398
|
+
terms_of_service_url: "www.The-URL-of-the-terms-and-service.com",
|
399
|
+
}
|
400
|
+
```
|
401
|
+
|
402
|
+
A hash merged into the `info` key of the JSON documentation.
|
403
|
+
|
404
|
+
#### array_use_braces: <a name="array_use_braces"></a>
|
405
|
+
|
406
|
+
```ruby
|
407
|
+
add_swagger_documentation \
|
408
|
+
array_use_braces: true
|
409
|
+
```
|
410
|
+
This setting must be `true` in order for params defined as an `Array` type to submit each element properly.
|
411
|
+
```ruby
|
412
|
+
params do
|
413
|
+
optional :metadata, type: Array[String]
|
414
|
+
end
|
415
|
+
```
|
416
|
+
with `array_use_braces: true`:
|
417
|
+
```
|
418
|
+
metadata[]: { "name": "Asset ID", "value": "12345" }
|
419
|
+
metadata[]: { "name": "Asset Tag", "value": "654321"}
|
420
|
+
```
|
421
|
+
with `array_use_braces: false`:
|
422
|
+
```
|
423
|
+
metadata: {"name": "Asset ID", "value": "123456"}
|
424
|
+
metadata: {"name": "Asset Tag", "value": "654321"}
|
425
|
+
```
|
426
|
+
|
427
|
+
#### api_documentation
|
428
|
+
|
429
|
+
Customize the Swagger API documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description".
|
430
|
+
|
431
|
+
```ruby
|
432
|
+
add_swagger_documentation \
|
433
|
+
api_documentation: { desc: 'Reticulated splines API swagger-compatible documentation.' }
|
434
|
+
```
|
435
|
+
|
436
|
+
#### specific_api_documentation
|
437
|
+
|
438
|
+
Customize the Swagger API specific documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description for specific API".
|
439
|
+
|
440
|
+
```ruby
|
441
|
+
add_swagger_documentation \
|
442
|
+
specific_api_documentation: { desc: 'Reticulated splines API swagger-compatible endpoint documentation.' }
|
443
|
+
```
|
444
|
+
|
445
|
+
#### consumes
|
446
|
+
|
447
|
+
Customize the Swagger API default global `consumes` field value.
|
448
|
+
|
449
|
+
```ruby
|
450
|
+
add_swagger_documentation \
|
451
|
+
consumes: ['application/json', 'application/x-www-form-urlencoded']
|
452
|
+
```
|
453
|
+
|
454
|
+
#### produces
|
455
|
+
|
456
|
+
Customize the Swagger API default global `produces` field value.
|
457
|
+
|
458
|
+
```ruby
|
459
|
+
add_swagger_documentation \
|
460
|
+
produces: ['text/plain']
|
461
|
+
```
|
462
|
+
|
463
|
+
## Routes Configuration <a name="routes"></a>
|
464
|
+
|
465
|
+
* [Swagger Header Parameters](#headers)
|
466
|
+
* [Hiding an Endpoint](#hiding)
|
467
|
+
* [Overriding Auto-Generated Nicknames](#overriding-auto-generated-nicknames)
|
468
|
+
* [Specify endpoint details](#details)
|
469
|
+
* [Overriding the route summary](#summary)
|
470
|
+
* [Overriding the tags](#overriding_the_tags)
|
471
|
+
* [Deprecating routes](#deprecating-routes)
|
472
|
+
* [Overriding the name of the body parameter](#body-param)
|
473
|
+
* [Defining an endpoint as an array](#array)
|
474
|
+
* [Using an options hash](#options)
|
475
|
+
* [Overriding parameter type](#overriding-param-type)
|
476
|
+
* [Overriding data type of the parameter](#overriding-type-of-param)
|
477
|
+
* [Multiple types](#multiple-types)
|
478
|
+
* [Array of data type](#array-type)
|
479
|
+
* [Collection Format](#collection-format)
|
480
|
+
* [Hiding parameters](#hiding-parameters)
|
481
|
+
* [Setting a Swagger default value](#default-value)
|
482
|
+
* [Setting `additionalProperties` for `object`-type parameters](#additional-properties)
|
483
|
+
* [Example parameter value](#param-example)
|
484
|
+
* [Response documentation](#response)
|
485
|
+
* [Changing default status codes](#change-status)
|
486
|
+
* [File response](#file-response)
|
487
|
+
* [Extensions](#extensions)
|
488
|
+
* [Response examples documentation](#response-examples)
|
489
|
+
* [Response headers documentation](#response-headers)
|
490
|
+
* [Adding root element to responses](#response-root)
|
491
|
+
* [Multiple present Response](#multiple-response)
|
492
|
+
|
493
|
+
#### Swagger Header Parameters <a name="headers"></a>
|
494
|
+
|
495
|
+
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.
|
496
|
+
|
497
|
+
```ruby
|
498
|
+
desc "Return super-secret information", {
|
499
|
+
headers: {
|
500
|
+
"XAuthToken" => {
|
501
|
+
description: "Valdates your identity",
|
502
|
+
required: true
|
503
|
+
},
|
504
|
+
"XOptionalHeader" => {
|
505
|
+
description: "Not really needed",
|
506
|
+
required: false
|
507
|
+
}
|
508
|
+
}
|
509
|
+
}
|
510
|
+
```
|
511
|
+
|
512
|
+
|
513
|
+
#### Hiding an Endpoint <a name="hiding"></a>
|
514
|
+
|
515
|
+
You can hide an endpoint by adding ```hidden: true``` in the description of the endpoint:
|
516
|
+
|
517
|
+
```ruby
|
518
|
+
desc 'Hide this endpoint', hidden: true
|
519
|
+
```
|
520
|
+
|
521
|
+
Or by adding ```hidden: true``` on the verb method of the endpoint, such as `get`, `post` and `put`:
|
522
|
+
|
523
|
+
```ruby
|
524
|
+
get '/kittens', hidden: true do
|
525
|
+
```
|
526
|
+
|
527
|
+
Or by using a route setting:
|
528
|
+
|
529
|
+
```ruby
|
530
|
+
route_setting :swagger, { hidden: true }
|
531
|
+
get '/kittens' do
|
532
|
+
```
|
533
|
+
|
534
|
+
Endpoints can be conditionally hidden by providing a callable object such as a lambda which evaluates to the desired
|
535
|
+
state:
|
536
|
+
|
537
|
+
```ruby
|
538
|
+
desc 'Conditionally hide this endpoint', hidden: lambda { ENV['EXPERIMENTAL'] != 'true' }
|
539
|
+
```
|
540
|
+
|
541
|
+
|
542
|
+
#### Overriding Auto-Generated Nicknames <a name="overriding-auto-generated-nicknames"></a>
|
543
|
+
|
544
|
+
You can specify a swagger nickname to use instead of the auto generated name by adding `:nickname 'string'` in the description of the endpoint.
|
545
|
+
|
546
|
+
```ruby
|
547
|
+
desc 'Get a full list of pets', nickname: 'getAllPets'
|
548
|
+
```
|
549
|
+
|
550
|
+
|
551
|
+
#### Specify endpoint details <a name="details"></a>
|
552
|
+
|
553
|
+
To specify further details for an endpoint, use the `detail` option within a block passed to `desc`:
|
554
|
+
|
555
|
+
```ruby
|
556
|
+
desc 'Get all kittens!' do
|
557
|
+
detail 'this will expose all the kittens'
|
558
|
+
end
|
559
|
+
get '/kittens' do
|
560
|
+
```
|
561
|
+
|
562
|
+
|
563
|
+
#### Overriding the route summary <a name="summary"></a>
|
564
|
+
|
565
|
+
To override the summary, add `summary: '[string]'` after the description.
|
566
|
+
|
567
|
+
```ruby
|
568
|
+
namespace 'order' do
|
569
|
+
desc 'This will be your summary',
|
570
|
+
summary: 'Now this is your summary!'
|
571
|
+
get :order_id do
|
572
|
+
...
|
573
|
+
end
|
574
|
+
end
|
575
|
+
```
|
576
|
+
|
577
|
+
|
578
|
+
#### Overriding the tags <a name="overriding_the_tags"></a>
|
579
|
+
|
580
|
+
Tags are used for logical grouping of operations by resources or any other qualifier. To override the
|
581
|
+
tags array, add `tags: ['tag1', 'tag2']` after the description.
|
582
|
+
|
583
|
+
```ruby
|
584
|
+
namespace 'order' do
|
585
|
+
desc 'This will be your summary', tags: ['orders']
|
586
|
+
get :order_id do
|
587
|
+
...
|
588
|
+
end
|
589
|
+
end
|
590
|
+
```
|
591
|
+
|
592
|
+
|
593
|
+
#### Deprecating routes <a name="deprecating-routes"></a>
|
594
|
+
|
595
|
+
To deprecate a route add `deprecated: true` after the description.
|
596
|
+
|
597
|
+
```ruby
|
598
|
+
namespace 'order' do
|
599
|
+
desc 'This is a deprecated route', deprecated: true
|
600
|
+
get :order_id do
|
601
|
+
...
|
602
|
+
end
|
603
|
+
end
|
604
|
+
```
|
605
|
+
|
606
|
+
|
607
|
+
#### Overriding the name of the body parameter <a name="body-param"></a>
|
608
|
+
|
609
|
+
By default, body parameters have a generated name based on the operation. For
|
610
|
+
deeply nested resources, this name can get very long. To override the name of
|
611
|
+
body parameter add `body_name: 'post_body'` after the description.
|
612
|
+
|
613
|
+
```ruby
|
614
|
+
namespace 'order' do
|
615
|
+
desc 'Create an order', body_name: 'post_body'
|
616
|
+
post do
|
617
|
+
...
|
618
|
+
end
|
619
|
+
end
|
620
|
+
```
|
621
|
+
|
622
|
+
|
623
|
+
#### Defining an endpoint as an array <a name="array"></a>
|
624
|
+
|
625
|
+
You can define an endpoint as an array by adding `is_array` in the description:
|
626
|
+
|
627
|
+
```ruby
|
628
|
+
desc 'Get a full list of pets', is_array: true
|
629
|
+
```
|
630
|
+
|
631
|
+
|
632
|
+
#### Using an options hash <a name="options"></a>
|
633
|
+
|
634
|
+
The Grape DSL supports either an options hash or a restricted block to pass settings. Passing the `nickname`, `hidden` and `is_array` options together with response codes is only possible when passing an options hash.
|
635
|
+
Since the syntax differs you'll need to adjust it accordingly:
|
636
|
+
|
637
|
+
```ruby
|
638
|
+
desc 'Get all kittens!', {
|
639
|
+
hidden: true,
|
640
|
+
is_array: true,
|
641
|
+
nickname: 'getKittens',
|
642
|
+
success: Entities::Kitten, # or success
|
643
|
+
failure: [[401, 'KittenBitesError', Entities::BadKitten]] # or failure
|
644
|
+
# also explicit as hash: [{ code: 401, message: 'KittenBitesError', model: Entities::BadKitten }]
|
645
|
+
produces: [ "array", "of", "mime_types" ],
|
646
|
+
consumes: [ "array", "of", "mime_types" ]
|
647
|
+
}
|
648
|
+
get '/kittens' do
|
649
|
+
```
|
650
|
+
|
651
|
+
|
652
|
+
#### Overriding parameter type <a name="overriding-param-type"></a>
|
653
|
+
|
654
|
+
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.
|
655
|
+
|
656
|
+
```ruby
|
657
|
+
params do
|
658
|
+
requires :action, type: Symbol, values: [:PAUSE, :RESUME, :STOP], documentation: { param_type: 'query' }
|
659
|
+
end
|
660
|
+
post :act do
|
661
|
+
...
|
662
|
+
end
|
663
|
+
```
|
664
|
+
|
665
|
+
|
666
|
+
#### Overriding data type of the parameter <a name="overriding-type-of-param"></a>
|
667
|
+
|
668
|
+
You can override type, using the documentation hash.
|
669
|
+
|
670
|
+
```ruby
|
671
|
+
params do
|
672
|
+
requires :input, type: String, documentation: { type: 'integer' }
|
673
|
+
end
|
674
|
+
post :act do
|
675
|
+
...
|
676
|
+
end
|
677
|
+
```
|
678
|
+
|
679
|
+
```json
|
680
|
+
{
|
681
|
+
"in": "formData",
|
682
|
+
"name": "input",
|
683
|
+
"type": "integer",
|
684
|
+
"format": "int32",
|
685
|
+
"required": true
|
686
|
+
}
|
687
|
+
```
|
688
|
+
|
689
|
+
|
690
|
+
#### Multiple types <a name="multiple-types"></a>
|
691
|
+
|
692
|
+
By default when you set multiple types, the first type is selected as swagger type
|
693
|
+
|
694
|
+
```ruby
|
695
|
+
params do
|
696
|
+
requires :action, types: [String, Integer]
|
697
|
+
end
|
698
|
+
post :act do
|
699
|
+
...
|
700
|
+
end
|
701
|
+
```
|
702
|
+
|
703
|
+
```json
|
704
|
+
{
|
705
|
+
"in": "formData",
|
706
|
+
"name": "action",
|
707
|
+
"type": "string",
|
708
|
+
"required": true
|
709
|
+
}
|
710
|
+
```
|
711
|
+
|
712
|
+
|
713
|
+
#### Array of data type <a name="array-type"></a>
|
714
|
+
|
715
|
+
Array types are also supported.
|
716
|
+
|
717
|
+
```ruby
|
718
|
+
params do
|
719
|
+
requires :action_ids, type: Array[Integer]
|
720
|
+
end
|
721
|
+
post :act do
|
722
|
+
...
|
723
|
+
end
|
724
|
+
```
|
725
|
+
|
726
|
+
```json
|
727
|
+
{
|
728
|
+
"in": "formData",
|
729
|
+
"name": "action_ids",
|
730
|
+
"type": "array",
|
731
|
+
"items": {
|
732
|
+
"type": "integer"
|
733
|
+
},
|
734
|
+
"required": true
|
735
|
+
}
|
736
|
+
```
|
737
|
+
|
738
|
+
|
739
|
+
#### Collection format of arrays <a name="collection-format"></a>
|
740
|
+
|
741
|
+
You can set the collection format of an array, using the documentation hash.
|
742
|
+
|
743
|
+
Collection format determines the format of the array if type array is used. Possible values are:
|
744
|
+
* csv - comma separated values foo,bar.
|
745
|
+
* ssv - space separated values foo bar.
|
746
|
+
* tsv - tab separated values foo\tbar.
|
747
|
+
* pipes - pipe separated values foo|bar.
|
748
|
+
* multi - corresponds to multiple parameter instances instead of multiple values for a single instance foo=bar&foo=baz. This is valid only for parameters in "query" or "formData".
|
749
|
+
|
750
|
+
```ruby
|
751
|
+
params do
|
752
|
+
requires :statuses, type: Array[String], documentation: { collectionFormat: 'multi' }
|
753
|
+
end
|
754
|
+
post :act do
|
755
|
+
...
|
756
|
+
end
|
757
|
+
```
|
758
|
+
|
759
|
+
```json
|
760
|
+
{
|
761
|
+
"in": "formData",
|
762
|
+
"name": "statuses",
|
763
|
+
"type": "array",
|
764
|
+
"items": {
|
765
|
+
"type": "string"
|
766
|
+
},
|
767
|
+
"collectionFormat": "multi",
|
768
|
+
"required": true
|
769
|
+
}
|
770
|
+
```
|
771
|
+
|
772
|
+
|
773
|
+
#### Hiding parameters <a name="hiding-parameters"></a>
|
774
|
+
|
775
|
+
Exclude single optional parameter from the documentation
|
776
|
+
|
777
|
+
```ruby
|
778
|
+
not_admins = lambda { |token_owner = nil| token_owner.nil? || !token_owner.admin? }
|
779
|
+
|
780
|
+
params do
|
781
|
+
optional :one, documentation: { hidden: true }
|
782
|
+
optional :two, documentation: { hidden: -> { |t=nil| true } }
|
783
|
+
optional :three, documentation: { hidden: not_admins }
|
784
|
+
end
|
785
|
+
post :act do
|
786
|
+
...
|
787
|
+
end
|
788
|
+
```
|
789
|
+
|
790
|
+
|
791
|
+
#### Setting a Swagger default value <a name="default-value"></a>
|
792
|
+
|
793
|
+
Grape allows for an additional documentation hash to be passed to a parameter.
|
794
|
+
|
795
|
+
```ruby
|
796
|
+
params do
|
797
|
+
requires :id, type: Integer, desc: 'Coffee ID'
|
798
|
+
requires :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', documentation: { default: 72 }
|
799
|
+
end
|
800
|
+
```
|
801
|
+
|
802
|
+
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.
|
803
|
+
|
804
|
+
```ruby
|
805
|
+
params do
|
806
|
+
requires :id, type: Integer, desc: 'Coffee ID'
|
807
|
+
optional :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', default: 72
|
808
|
+
end
|
809
|
+
```
|
810
|
+
|
811
|
+
### Setting `additionalProperties` for `object`-type parameters <a name="additional-properties">
|
812
|
+
|
813
|
+
Use the `additional_properties` option in the `documentation` hash for `object`-type parameters to set [`additionalProperties`](https://swagger.io/specification/v2/#model-with-mapdictionary-properties).
|
814
|
+
|
815
|
+
#### Allow any additional properties
|
816
|
+
```ruby
|
817
|
+
params do
|
818
|
+
optional :thing, type: Hash, documentation: { additional_properties: true }
|
819
|
+
end
|
820
|
+
```
|
821
|
+
|
822
|
+
#### Allow any additional properties of a particular type
|
823
|
+
```ruby
|
824
|
+
params do
|
825
|
+
optional :thing, type: Hash, documentation: { additional_properties: String }
|
826
|
+
end
|
827
|
+
```
|
828
|
+
|
829
|
+
#### Allow any additional properties matching a defined schema
|
830
|
+
```ruby
|
831
|
+
class Entity < Grape::Entity
|
832
|
+
expose :this
|
833
|
+
end
|
834
|
+
|
835
|
+
params do
|
836
|
+
optional :thing, type: Hash, documentation: { additional_properties: Entity }
|
837
|
+
end
|
838
|
+
```
|
839
|
+
|
840
|
+
|
841
|
+
#### Example parameter value <a name="param-example"></a>
|
842
|
+
|
843
|
+
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
|
844
|
+
|
845
|
+
```ruby
|
846
|
+
params do
|
847
|
+
requires :id, type: Integer, documentation: { example: 123 }
|
848
|
+
optional :name, type: String, documentation: { example: 'Buddy Guy' }
|
849
|
+
end
|
850
|
+
```
|
851
|
+
|
852
|
+
#### Expose nested namespace as standalone route
|
853
|
+
|
854
|
+
Use the `nested: false` property in the `swagger` option to make nested namespaces appear as standalone resources.
|
855
|
+
This option can help to structure and keep the swagger schema simple.
|
856
|
+
|
857
|
+
```ruby
|
858
|
+
namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false } do
|
859
|
+
get :order_id do
|
860
|
+
...
|
861
|
+
end
|
862
|
+
end
|
863
|
+
```
|
864
|
+
|
865
|
+
All routes that belong to this namespace (here: the `GET /order_id`) will then be assigned to the `store_order` route instead of the `store` resource route.
|
866
|
+
|
867
|
+
It is also possible to expose a namespace within another already exposed namespace:
|
868
|
+
|
869
|
+
```ruby
|
870
|
+
namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false } do
|
871
|
+
get :order_id do
|
872
|
+
...
|
873
|
+
end
|
874
|
+
namespace 'actions', desc: 'Order actions', nested: false do
|
875
|
+
get 'evaluate' do
|
876
|
+
...
|
877
|
+
end
|
878
|
+
end
|
879
|
+
end
|
880
|
+
```
|
881
|
+
Here, the `GET /order_id` appears as operation of the `store_order` resource and the `GET /evaluate` as operation of the `store_orders_actions` route.
|
882
|
+
|
883
|
+
|
884
|
+
##### With a custom name
|
885
|
+
|
886
|
+
Auto generated names for the standalone version of complex nested resource do not have a nice look.
|
887
|
+
You can set a custom name with the `name` property inside the `swagger` option, but only if the namespace gets exposed as standalone route.
|
888
|
+
The name should not contain whitespaces or any other special characters due to further issues within swagger-ui.
|
889
|
+
|
890
|
+
```ruby
|
891
|
+
namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false, name: 'Store-orders' } do
|
892
|
+
get :order_id do
|
893
|
+
...
|
894
|
+
end
|
895
|
+
end
|
896
|
+
```
|
897
|
+
|
898
|
+
|
899
|
+
#### Response documentation <a name="response"></a>
|
900
|
+
|
901
|
+
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.
|
902
|
+
|
903
|
+
In the following cases, the schema ref would be taken from route.
|
904
|
+
|
905
|
+
```ruby
|
906
|
+
desc 'thing', failure: [ { code: 400, message: 'Invalid parameter entry' } ]
|
907
|
+
get '/thing' do
|
908
|
+
# ...
|
909
|
+
end
|
910
|
+
```
|
911
|
+
|
912
|
+
```ruby
|
913
|
+
desc 'thing' do
|
914
|
+
params Entities::Something.documentation
|
915
|
+
failure [ { code: 400, message: 'Invalid parameter entry' } ]
|
916
|
+
end
|
917
|
+
get '/thing' do
|
918
|
+
# ...
|
919
|
+
end
|
920
|
+
```
|
921
|
+
|
922
|
+
```ruby
|
923
|
+
get '/thing', failure: [
|
924
|
+
{ code: 400, message: 'Invalid parameter entry' },
|
925
|
+
{ code: 404, message: 'Not authorized' },
|
926
|
+
] do
|
927
|
+
# ...
|
928
|
+
end
|
929
|
+
```
|
930
|
+
|
931
|
+
By adding a `model` key, e.g. this would be taken. Setting an empty string will act like an empty body.
|
932
|
+
```ruby
|
933
|
+
get '/thing', failure: [
|
934
|
+
{ code: 400, message: 'General error' },
|
935
|
+
{ code: 403, message: 'Forbidden error', model: '' },
|
936
|
+
{ code: 422, message: 'Invalid parameter entry', model: Entities::ApiError }
|
937
|
+
] do
|
938
|
+
# ...
|
939
|
+
end
|
940
|
+
```
|
941
|
+
If no status code is defined [defaults](/lib/grape-swagger/endpoint.rb#L210) would be taken.
|
942
|
+
|
943
|
+
The result is then something like following:
|
944
|
+
|
945
|
+
```json
|
946
|
+
"responses": {
|
947
|
+
"200": {
|
948
|
+
"description": "get Horses",
|
949
|
+
"schema": {
|
950
|
+
"$ref": "#/definitions/Thing"
|
951
|
+
}
|
952
|
+
},
|
953
|
+
"401": {
|
954
|
+
"description": "HorsesOutError",
|
955
|
+
"schema": {
|
956
|
+
"$ref": "#/definitions/ApiError"
|
957
|
+
}
|
958
|
+
}
|
959
|
+
},
|
960
|
+
```
|
961
|
+
|
962
|
+
|
963
|
+
#### Changing default status codes <a name="change-status"></a>
|
964
|
+
|
965
|
+
The default status codes, one could be found (-> [status codes](lib/grape-swagger/doc_methods/status_codes.rb)) can be changed to your specific needs, to achive it, you have to change it for grape itself and for the documentation.
|
966
|
+
|
967
|
+
```ruby
|
968
|
+
desc 'Get a list of stuff',
|
969
|
+
success: { code: 202, model: Entities::UseResponse, message: 'a changed status code' }
|
970
|
+
get do
|
971
|
+
status 202
|
972
|
+
# your code comes here
|
973
|
+
end
|
974
|
+
```
|
975
|
+
|
976
|
+
```json
|
977
|
+
"responses": {
|
978
|
+
"202": {
|
979
|
+
"description": "ok",
|
980
|
+
"schema": {
|
981
|
+
"$ref": "#/definitions/UseResponse"
|
982
|
+
}
|
983
|
+
}
|
984
|
+
},
|
985
|
+
```
|
986
|
+
|
987
|
+
#### Multiple status codes for response <a name="multiple-status-response"></a>
|
988
|
+
|
989
|
+
Multiple values can be provided for `success` and `failure` attributes in the response.
|
990
|
+
|
991
|
+
```ruby
|
992
|
+
desc 'Attach a field to an entity through a PUT',
|
993
|
+
success: [
|
994
|
+
{ code: 201, model: Entities::UseResponse, message: 'Successfully created' },
|
995
|
+
{ code: 204, message: 'Already exists' }
|
996
|
+
],
|
997
|
+
failure: [
|
998
|
+
{ code: 400, message: 'Bad request' },
|
999
|
+
{ code: 404, message: 'Not found' }
|
1000
|
+
]
|
1001
|
+
put do
|
1002
|
+
# your code comes here
|
1003
|
+
end
|
1004
|
+
```
|
1005
|
+
|
1006
|
+
```json
|
1007
|
+
"responses": {
|
1008
|
+
"201": {
|
1009
|
+
"description": "Successfully created",
|
1010
|
+
"schema": {
|
1011
|
+
"$ref": "#/definitions/UseResponse"
|
1012
|
+
}
|
1013
|
+
},
|
1014
|
+
"204": {
|
1015
|
+
"description": "Already exists"
|
1016
|
+
},
|
1017
|
+
"400": {
|
1018
|
+
"description": "Bad request"
|
1019
|
+
},
|
1020
|
+
"404": {
|
1021
|
+
"description": "Not found"
|
1022
|
+
}
|
1023
|
+
},
|
1024
|
+
```
|
1025
|
+
|
1026
|
+
|
1027
|
+
#### File response <a name="file-response"></a>
|
1028
|
+
|
1029
|
+
Setting `success` to `File` sets a default `produces` of `application/octet-stream`.
|
1030
|
+
|
1031
|
+
```ruby
|
1032
|
+
desc 'Get a file',
|
1033
|
+
success: File
|
1034
|
+
get do
|
1035
|
+
# your file response
|
1036
|
+
end
|
1037
|
+
```
|
1038
|
+
|
1039
|
+
```json
|
1040
|
+
"produces": [
|
1041
|
+
"application/octet-stream"
|
1042
|
+
],
|
1043
|
+
"responses": {
|
1044
|
+
"200": {
|
1045
|
+
"description": "Get a file",
|
1046
|
+
"schema": {
|
1047
|
+
"type": "file"
|
1048
|
+
}
|
1049
|
+
}
|
1050
|
+
}
|
1051
|
+
```
|
1052
|
+
|
1053
|
+
|
1054
|
+
#### Extensions <a name="extensions"></a>
|
1055
|
+
|
1056
|
+
Swagger spec2.0 supports extensions on different levels, for the moment,
|
1057
|
+
the documentation on the root level object and the `info`, `verb`, `path` and `definition` levels are supported.
|
1058
|
+
|
1059
|
+
The documented key would be generated from the `x` + `-` + key of the submitted hash,
|
1060
|
+
for possibilities refer to the [extensions spec](spec/lib/extensions_spec.rb).
|
1061
|
+
To get an overview *how* the extensions would be defined on grape level, see the following examples:
|
1062
|
+
|
1063
|
+
- root object extension, add a `x` key to the root hash when calling ```add_swagger_documentation```:
|
1064
|
+
```ruby
|
1065
|
+
add_swagger_documentation \
|
1066
|
+
x: {
|
1067
|
+
some: 'stuff'
|
1068
|
+
},
|
1069
|
+
info: {
|
1070
|
+
}
|
1071
|
+
```
|
1072
|
+
this would generate:
|
1073
|
+
```json
|
1074
|
+
{
|
1075
|
+
"x-some": "stuff",
|
1076
|
+
"info":{
|
1077
|
+
}
|
1078
|
+
}
|
1079
|
+
```
|
1080
|
+
|
1081
|
+
- `info` extension, add a `x` key to the `info` hash when calling ```add_swagger_documentation```:
|
1082
|
+
```ruby
|
1083
|
+
add_swagger_documentation \
|
1084
|
+
info: {
|
1085
|
+
x: { some: 'stuff' }
|
1086
|
+
}
|
1087
|
+
```
|
1088
|
+
this would generate:
|
1089
|
+
```json
|
1090
|
+
"info":{
|
1091
|
+
"x-some":"stuff"
|
1092
|
+
}
|
1093
|
+
```
|
1094
|
+
|
1095
|
+
- `verb` extension, add a `x` key to the `desc` hash:
|
1096
|
+
```ruby
|
1097
|
+
desc 'This returns something with extension on verb level',
|
1098
|
+
x: { some: 'stuff' }
|
1099
|
+
```
|
1100
|
+
this would generate:
|
1101
|
+
```json
|
1102
|
+
"/path":{
|
1103
|
+
"get":{
|
1104
|
+
"…":"…",
|
1105
|
+
"x-some":"stuff"
|
1106
|
+
}
|
1107
|
+
}
|
1108
|
+
```
|
1109
|
+
|
1110
|
+
- `operation` extension, by setting via route settings::
|
1111
|
+
```ruby
|
1112
|
+
route_setting :x_operation, { some: 'stuff' }
|
1113
|
+
```
|
1114
|
+
this would generate:
|
1115
|
+
```json
|
1116
|
+
"/path":{
|
1117
|
+
"get":{
|
1118
|
+
"…":"…",
|
1119
|
+
"x-some":"stuff"
|
1120
|
+
}
|
1121
|
+
}
|
1122
|
+
```
|
1123
|
+
|
1124
|
+
- `path` extension, by setting via route settings:
|
1125
|
+
```ruby
|
1126
|
+
route_setting :x_path, { some: 'stuff' }
|
1127
|
+
```
|
1128
|
+
this would generate:
|
1129
|
+
```json
|
1130
|
+
"/path":{
|
1131
|
+
"x-some":"stuff",
|
1132
|
+
"get":{
|
1133
|
+
"…":"…",
|
1134
|
+
}
|
1135
|
+
}
|
1136
|
+
```
|
1137
|
+
|
1138
|
+
- `definition` extension, again by setting via route settings,
|
1139
|
+
here the status code must be provided, for which definition the extensions should be:
|
1140
|
+
```ruby
|
1141
|
+
route_setting :x_def, { for: 422, other: 'stuff' }
|
1142
|
+
```
|
1143
|
+
this would generate:
|
1144
|
+
```json
|
1145
|
+
"/definitions":{
|
1146
|
+
"ApiError":{
|
1147
|
+
"x-other":"stuff",
|
1148
|
+
"…":"…",
|
1149
|
+
}
|
1150
|
+
}
|
1151
|
+
```
|
1152
|
+
or, for more definitions:
|
1153
|
+
```ruby
|
1154
|
+
route_setting :x_def, [{ for: 422, other: 'stuff' }, { for: 200, some: 'stuff' }]
|
1155
|
+
```
|
1156
|
+
|
1157
|
+
- `params` extension, add a `x` key to the `documentation` hash :
|
1158
|
+
```ruby
|
1159
|
+
requires :foo, type: String, documentation: { x: { some: 'stuff' } }
|
1160
|
+
```
|
1161
|
+
this would generate:
|
1162
|
+
```json
|
1163
|
+
{
|
1164
|
+
"in": "formData",
|
1165
|
+
"name": "foo",
|
1166
|
+
"type": "string",
|
1167
|
+
"required": true,
|
1168
|
+
"x-some": "stuff"
|
1169
|
+
}
|
1170
|
+
```
|
1171
|
+
|
1172
|
+
#### Response examples documentation <a name="response-examples"></a>
|
1173
|
+
|
1174
|
+
You can also add examples to your responses by using the `desc` DSL with block syntax.
|
1175
|
+
|
1176
|
+
By specifying examples to `success` and `failure`.
|
1177
|
+
|
1178
|
+
```ruby
|
1179
|
+
desc 'This returns examples' do
|
1180
|
+
success model: Thing, examples: { 'application/json' => { description: 'Names list', items: [{ id: '123', name: 'John' }] } }
|
1181
|
+
failure [[404, 'NotFound', ApiError, { 'application/json' => { code: 404, message: 'Not found' } }]]
|
1182
|
+
end
|
1183
|
+
get '/thing' do
|
1184
|
+
...
|
1185
|
+
end
|
1186
|
+
```
|
1187
|
+
|
1188
|
+
The result will look like following:
|
1189
|
+
|
1190
|
+
```json
|
1191
|
+
"responses": {
|
1192
|
+
"200": {
|
1193
|
+
"description": "This returns examples",
|
1194
|
+
"schema": {
|
1195
|
+
"$ref": "#/definitions/Thing"
|
1196
|
+
},
|
1197
|
+
"examples": {
|
1198
|
+
"application/json": {
|
1199
|
+
"description": "Names list",
|
1200
|
+
"items": [
|
1201
|
+
{
|
1202
|
+
"id": "123",
|
1203
|
+
"name": "John"
|
1204
|
+
}
|
1205
|
+
]
|
1206
|
+
}
|
1207
|
+
}
|
1208
|
+
},
|
1209
|
+
"404": {
|
1210
|
+
"description": "NotFound",
|
1211
|
+
"schema": {
|
1212
|
+
"$ref": "#/definitions/ApiError"
|
1213
|
+
},
|
1214
|
+
"examples": {
|
1215
|
+
"application/json": {
|
1216
|
+
"code": 404,
|
1217
|
+
"message": "Not found"
|
1218
|
+
}
|
1219
|
+
}
|
1220
|
+
}
|
1221
|
+
}
|
1222
|
+
```
|
1223
|
+
|
1224
|
+
Failure information can be passed as an array of arrays or an array of hashes.
|
1225
|
+
|
1226
|
+
#### Response headers documentation <a name="response-headers"></a>
|
1227
|
+
|
1228
|
+
You can also add header information to your responses by using the `desc` DSL with block syntax.
|
1229
|
+
|
1230
|
+
By specifying headers to `success` and `failure`.
|
1231
|
+
|
1232
|
+
```ruby
|
1233
|
+
desc 'This returns headers' do
|
1234
|
+
success model: Thing, headers: { 'Location' => { description: 'Location of resource', type: 'string' } }
|
1235
|
+
failure [[404, 'NotFound', ApiError, { 'application/json' => { code: 404, message: 'Not found' } }, { 'Date' => { description: 'Date of failure', type: 'string' } }]]
|
1236
|
+
end
|
1237
|
+
get '/thing' do
|
1238
|
+
...
|
1239
|
+
end
|
1240
|
+
```
|
1241
|
+
|
1242
|
+
The result will look like following:
|
1243
|
+
|
1244
|
+
```json
|
1245
|
+
"responses": {
|
1246
|
+
"200": {
|
1247
|
+
"description": "This returns examples",
|
1248
|
+
"schema": {
|
1249
|
+
"$ref": "#/definitions/Thing"
|
1250
|
+
},
|
1251
|
+
"headers": {
|
1252
|
+
"Location": {
|
1253
|
+
"description": "Location of resource",
|
1254
|
+
"type": "string"
|
1255
|
+
}
|
1256
|
+
}
|
1257
|
+
},
|
1258
|
+
"404": {
|
1259
|
+
"description": "NotFound",
|
1260
|
+
"schema": {
|
1261
|
+
"$ref": "#/definitions/ApiError"
|
1262
|
+
},
|
1263
|
+
"examples": {
|
1264
|
+
"application/json": {
|
1265
|
+
"code": 404,
|
1266
|
+
"message": "Not found"
|
1267
|
+
}
|
1268
|
+
},
|
1269
|
+
"headers": {
|
1270
|
+
"Date": {
|
1271
|
+
"description": "Date of failure",
|
1272
|
+
"type": "string"
|
1273
|
+
}
|
1274
|
+
}
|
1275
|
+
}
|
1276
|
+
}
|
1277
|
+
```
|
1278
|
+
|
1279
|
+
Failure information can be passed as an array of arrays or an array of hashes.
|
1280
|
+
|
1281
|
+
#### Adding root element to responses <a name="response-root"></a>
|
1282
|
+
|
1283
|
+
You can specify a custom root element for a successful response:
|
1284
|
+
|
1285
|
+
```ruby
|
1286
|
+
route_setting :swagger, root: 'cute_kitten'
|
1287
|
+
desc 'Get a kitten' do
|
1288
|
+
http_codes [{ code: 200, model: Entities::Kitten }]
|
1289
|
+
end
|
1290
|
+
get '/kittens/:id' do
|
1291
|
+
end
|
1292
|
+
```
|
1293
|
+
|
1294
|
+
The result will look like following:
|
1295
|
+
|
1296
|
+
```json
|
1297
|
+
"responses": {
|
1298
|
+
"200": {
|
1299
|
+
"description": "Get a kitten",
|
1300
|
+
"schema": {
|
1301
|
+
"type": "object",
|
1302
|
+
"properties": { "cute_kitten": { "$ref": "#/definitions/Kitten" } }
|
1303
|
+
}
|
1304
|
+
}
|
1305
|
+
}
|
1306
|
+
```
|
1307
|
+
|
1308
|
+
If you specify `true`, the value of the root element will be deduced based on the model name.
|
1309
|
+
E.g. in the following example the root element will be "kittens":
|
1310
|
+
|
1311
|
+
```ruby
|
1312
|
+
route_setting :swagger, root: true
|
1313
|
+
desc 'Get kittens' do
|
1314
|
+
is_array true
|
1315
|
+
http_codes [{ code: 200, model: Entities::Kitten }]
|
1316
|
+
end
|
1317
|
+
get '/kittens' do
|
1318
|
+
end
|
1319
|
+
```
|
1320
|
+
|
1321
|
+
The result will look like following:
|
1322
|
+
|
1323
|
+
```json
|
1324
|
+
"responses": {
|
1325
|
+
"200": {
|
1326
|
+
"description": "Get kittens",
|
1327
|
+
"schema": {
|
1328
|
+
"type": "object",
|
1329
|
+
"properties": { "type": "array", "items": { "kittens": { "$ref": "#/definitions/Kitten" } } }
|
1330
|
+
}
|
1331
|
+
}
|
1332
|
+
}
|
1333
|
+
```
|
1334
|
+
#### Multiple present Response <a name="multiple-response"></a>
|
1335
|
+
|
1336
|
+
You can specify a custom multiple response by using the `as` key:
|
1337
|
+
```ruby
|
1338
|
+
desc 'Multiple response',
|
1339
|
+
success: [
|
1340
|
+
{ model: Entities::EnumValues, as: :gender },
|
1341
|
+
{ model: Entities::Something, as: :somethings }
|
1342
|
+
]
|
1343
|
+
end
|
1344
|
+
|
1345
|
+
get '/things' do
|
1346
|
+
...
|
1347
|
+
end
|
1348
|
+
```
|
1349
|
+
The result will look like following:
|
1350
|
+
```json
|
1351
|
+
"responses": {
|
1352
|
+
"200": {
|
1353
|
+
"description": "Multiple response",
|
1354
|
+
"schema":{
|
1355
|
+
"type":"object",
|
1356
|
+
"properties":{
|
1357
|
+
"gender":{
|
1358
|
+
"$ref":"#/definitions/EnumValues"
|
1359
|
+
},
|
1360
|
+
"somethings":{
|
1361
|
+
"$ref":"#/definitions/Something"
|
1362
|
+
}
|
1363
|
+
}
|
1364
|
+
}
|
1365
|
+
}
|
1366
|
+
}
|
1367
|
+
```
|
1368
|
+
You can also specify if the response is an array, with the `is_array` key:
|
1369
|
+
```ruby
|
1370
|
+
desc 'Multiple response with array',
|
1371
|
+
success: [
|
1372
|
+
{ model: Entities::EnumValues, as: :gender },
|
1373
|
+
{ model: Entities::Something, as: :somethings, is_array: true, required: true }
|
1374
|
+
]
|
1375
|
+
end
|
1376
|
+
|
1377
|
+
get '/things' do
|
1378
|
+
...
|
1379
|
+
end
|
1380
|
+
```
|
1381
|
+
The result will look like following:
|
1382
|
+
```json
|
1383
|
+
"responses": {
|
1384
|
+
"200": {
|
1385
|
+
"description": "Multiple response with array",
|
1386
|
+
"schema":{
|
1387
|
+
"type":"object",
|
1388
|
+
"properties":{
|
1389
|
+
"gender":{
|
1390
|
+
"$ref":"#/definitions/EnumValues"
|
1391
|
+
},
|
1392
|
+
"somethings":{
|
1393
|
+
"type":"array",
|
1394
|
+
"items":{
|
1395
|
+
"$ref":"#/definitions/Something"
|
1396
|
+
}
|
1397
|
+
}
|
1398
|
+
},
|
1399
|
+
"required": ["somethings"]
|
1400
|
+
}
|
1401
|
+
}
|
1402
|
+
}
|
1403
|
+
```
|
1404
|
+
|
1405
|
+
## Using Grape Entities <a name="grape-entity"></a>
|
1406
|
+
|
1407
|
+
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.
|
1408
|
+
|
1409
|
+
The following example exposes statuses. And exposes statuses documentation adding :type, :desc and :required.
|
1410
|
+
The documented class/definition name could be set via `#entity_name`.
|
1411
|
+
|
1412
|
+
```ruby
|
1413
|
+
module API
|
1414
|
+
module Entities
|
1415
|
+
class Status < Grape::Entity
|
1416
|
+
expose :text, documentation: { type: 'string', desc: 'Status update text.', required: true }
|
1417
|
+
expose :links, using: Link, documentation: { type: 'link', is_array: true }
|
1418
|
+
expose :numbers, documentation: { type: 'integer', desc: 'favourite number', values: [1,2,3,4] }
|
1419
|
+
end
|
1420
|
+
|
1421
|
+
class Link < Grape::Entity
|
1422
|
+
expose :href, documentation: { type: 'url' }
|
1423
|
+
expose :rel, documentation: { type: 'string'}
|
1424
|
+
|
1425
|
+
def self.entity_name
|
1426
|
+
'LinkedStatus'
|
1427
|
+
end
|
1428
|
+
|
1429
|
+
end
|
1430
|
+
end
|
1431
|
+
|
1432
|
+
class Statuses < Grape::API
|
1433
|
+
version 'v1'
|
1434
|
+
|
1435
|
+
desc 'Statuses index',
|
1436
|
+
entity: API::Entities::Status
|
1437
|
+
get '/statuses' do
|
1438
|
+
statuses = Status.all
|
1439
|
+
type = current_user.admin? ? :full : :default
|
1440
|
+
present statuses, with: API::Entities::Status, type: type
|
1441
|
+
end
|
1442
|
+
|
1443
|
+
desc 'Creates a new status',
|
1444
|
+
entity: API::Entities::Status,
|
1445
|
+
params: API::Entities::Status.documentation
|
1446
|
+
post '/statuses' do
|
1447
|
+
...
|
1448
|
+
end
|
1449
|
+
end
|
1450
|
+
end
|
1451
|
+
```
|
1452
|
+
|
1453
|
+
|
1454
|
+
### Relationships
|
1455
|
+
|
1456
|
+
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`.
|
1457
|
+
|
1458
|
+
|
1459
|
+
#### 1xN
|
1460
|
+
|
1461
|
+
```ruby
|
1462
|
+
module API
|
1463
|
+
module Entities
|
1464
|
+
class Client < Grape::Entity
|
1465
|
+
expose :name, documentation: { type: 'string', desc: 'Name' }
|
1466
|
+
expose :addresses, using: Entities::Address,
|
1467
|
+
documentation: { type: 'Entities::Address', desc: 'Addresses.', param_type: 'body', is_array: true }
|
1468
|
+
end
|
1469
|
+
|
1470
|
+
class Address < Grape::Entity
|
1471
|
+
expose :street, documentation: { type: 'string', desc: 'Street.' }
|
1472
|
+
end
|
1473
|
+
end
|
1474
|
+
|
1475
|
+
class Clients < Grape::API
|
1476
|
+
version 'v1'
|
1477
|
+
|
1478
|
+
desc 'Clients index',
|
1479
|
+
params: Entities::Client.documentation,
|
1480
|
+
success: Entities::Client
|
1481
|
+
get '/clients' do
|
1482
|
+
...
|
1483
|
+
end
|
1484
|
+
end
|
1485
|
+
|
1486
|
+
add_swagger_documentation
|
1487
|
+
end
|
1488
|
+
```
|
1489
|
+
|
1490
|
+
|
1491
|
+
#### 1x1
|
1492
|
+
|
1493
|
+
Note: `is_array` is `false` by default.
|
1494
|
+
|
1495
|
+
```ruby
|
1496
|
+
module API
|
1497
|
+
module Entities
|
1498
|
+
class Client < Grape::Entity
|
1499
|
+
expose :name, documentation: { type: 'string', desc: 'Name' }
|
1500
|
+
expose :address, using: Entities::Address,
|
1501
|
+
documentation: { type: 'Entities::Address', desc: 'Addresses.', param_type: 'body', is_array: false }
|
1502
|
+
end
|
1503
|
+
|
1504
|
+
class Address < Grape::Entity
|
1505
|
+
expose :street, documentation: { type: 'string', desc: 'Street' }
|
1506
|
+
end
|
1507
|
+
end
|
1508
|
+
|
1509
|
+
class Clients < Grape::API
|
1510
|
+
version 'v1'
|
1511
|
+
|
1512
|
+
desc 'Clients index',
|
1513
|
+
params: Entities::Client.documentation,
|
1514
|
+
success: Entities::Client
|
1515
|
+
get '/clients' do
|
1516
|
+
...
|
1517
|
+
end
|
1518
|
+
end
|
1519
|
+
|
1520
|
+
add_swagger_documentation
|
1521
|
+
end
|
1522
|
+
```
|
1523
|
+
|
1524
|
+
#### Inheritance with allOf and discriminator
|
1525
|
+
```ruby
|
1526
|
+
module Entities
|
1527
|
+
class Pet < Grape::Entity
|
1528
|
+
expose :type, documentation: {
|
1529
|
+
type: 'string',
|
1530
|
+
is_discriminator: true,
|
1531
|
+
required: true
|
1532
|
+
}
|
1533
|
+
expose :name, documentation: {
|
1534
|
+
type: 'string',
|
1535
|
+
required: true
|
1536
|
+
}
|
1537
|
+
end
|
1538
|
+
|
1539
|
+
class Cat < Pet
|
1540
|
+
expose :huntingSkill, documentation: {
|
1541
|
+
type: 'string',
|
1542
|
+
description: 'The measured skill for hunting',
|
1543
|
+
default: 'lazy',
|
1544
|
+
values: %w[
|
1545
|
+
clueless
|
1546
|
+
lazy
|
1547
|
+
adventurous
|
1548
|
+
aggressive
|
1549
|
+
]
|
1550
|
+
}
|
1551
|
+
end
|
1552
|
+
end
|
1553
|
+
```
|
1554
|
+
|
1555
|
+
Should generate this definitions:
|
1556
|
+
```json
|
1557
|
+
{
|
1558
|
+
"definitions": {
|
1559
|
+
"Pet": {
|
1560
|
+
"type": "object",
|
1561
|
+
"discriminator": "petType",
|
1562
|
+
"properties": {
|
1563
|
+
"name": {
|
1564
|
+
"type": "string"
|
1565
|
+
},
|
1566
|
+
"petType": {
|
1567
|
+
"type": "string"
|
1568
|
+
}
|
1569
|
+
},
|
1570
|
+
"required": [
|
1571
|
+
"name",
|
1572
|
+
"petType"
|
1573
|
+
]
|
1574
|
+
},
|
1575
|
+
"Cat": {
|
1576
|
+
"description": "A representation of a cat",
|
1577
|
+
"allOf": [
|
1578
|
+
{
|
1579
|
+
"$ref": "#/definitions/Pet"
|
1580
|
+
},
|
1581
|
+
{
|
1582
|
+
"type": "object",
|
1583
|
+
"properties": {
|
1584
|
+
"huntingSkill": {
|
1585
|
+
"type": "string",
|
1586
|
+
"description": "The measured skill for hunting",
|
1587
|
+
"default": "lazy",
|
1588
|
+
"enum": [
|
1589
|
+
"clueless",
|
1590
|
+
"lazy",
|
1591
|
+
"adventurous",
|
1592
|
+
"aggressive"
|
1593
|
+
]
|
1594
|
+
},
|
1595
|
+
"petType": {
|
1596
|
+
"type": "string",
|
1597
|
+
"enum": ["Cat"]
|
1598
|
+
}
|
1599
|
+
},
|
1600
|
+
"required": [
|
1601
|
+
"huntingSkill",
|
1602
|
+
"petType"
|
1603
|
+
]
|
1604
|
+
}
|
1605
|
+
]
|
1606
|
+
}
|
1607
|
+
}
|
1608
|
+
}
|
1609
|
+
```
|
1610
|
+
|
1611
|
+
|
1612
|
+
|
1613
|
+
|
1614
|
+
## Securing the Swagger UI <a name="oauth"></a>
|
1615
|
+
|
1616
|
+
The Swagger UI on Grape could be secured from unauthorized access using any middleware, which provides certain methods:
|
1617
|
+
|
1618
|
+
- some guard method, which could receive as argument a string or an array of authorization scopes;
|
1619
|
+
- a *before* method to be run in the Grape controller for authorization purpose;
|
1620
|
+
- a set of methods which will process the access token received in the HTTP request headers (usually in the
|
1621
|
+
'HTTP_AUTHORIZATION' header) and try to return the owner of the token.
|
1622
|
+
|
1623
|
+
Below are some examples of securing the Swagger UI on Grape installed along with Ruby on Rails:
|
1624
|
+
|
1625
|
+
- The WineBouncer and Doorkeeper gems are used in the examples;
|
1626
|
+
- 'rails' and 'wine_bouncer' gems should be required prior to 'grape-swagger' in boot.rb;
|
1627
|
+
- This works with a fresh PR to WineBouncer which is yet unmerged - [WineBouncer PR](https://github.com/antek-drzewiecki/wine_bouncer/pull/64).
|
1628
|
+
|
1629
|
+
This is how to configure the grape_swagger documentation:
|
1630
|
+
|
1631
|
+
```ruby
|
1632
|
+
add_swagger_documentation base_path: '/',
|
1633
|
+
title: 'My API',
|
1634
|
+
doc_version: '0.0.1',
|
1635
|
+
hide_documentation_path: true,
|
1636
|
+
endpoint_auth_wrapper: WineBouncer::OAuth2, # This is the middleware for securing the Swagger UI
|
1637
|
+
swagger_endpoint_guard: 'oauth2 false', # this is the guard method and scope
|
1638
|
+
token_owner: 'resource_owner' # This is the method returning the owner of the token
|
1639
|
+
```
|
1640
|
+
|
1641
|
+
The guard method should inject the Security Requirement Object into the endpoint's route settings (see Grape::DSL::Settings.route_setting method).
|
1642
|
+
|
1643
|
+
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. the
|
1644
|
+
access_token is being retreiving from the HTTP request, but the 'false' scope is for skipping authorization and
|
1645
|
+
showing the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
|
1646
|
+
wouldn't be displayed at all to unauthorized users.
|
1647
|
+
|
1648
|
+
Further on, the guard could be used, where necessary, for endpoint access protection. Put it prior to the endpoint's method:
|
1649
|
+
|
1650
|
+
```ruby
|
1651
|
+
resource :users do
|
1652
|
+
oauth2 'read, write'
|
1653
|
+
get do
|
1654
|
+
render_users
|
1655
|
+
end
|
1656
|
+
|
1657
|
+
oauth2 'admin'
|
1658
|
+
post do
|
1659
|
+
User.create!...
|
1660
|
+
end
|
1661
|
+
end
|
1662
|
+
```
|
1663
|
+
|
1664
|
+
And, finally, if you want to not only restrict the access, but to completely hide the endpoint from unauthorized
|
1665
|
+
users, you could pass a lambda to the :hidden key of a endpoint's description:
|
1666
|
+
|
1667
|
+
```ruby
|
1668
|
+
not_admins = lambda { |token_owner = nil| token_owner.nil? || !token_owner.admin? }
|
1669
|
+
|
1670
|
+
resource :users do
|
1671
|
+
desc 'Create user', hidden: not_admins
|
1672
|
+
oauth2 'admin'
|
1673
|
+
post do
|
1674
|
+
User.create!...
|
1675
|
+
end
|
1676
|
+
end
|
1677
|
+
```
|
1678
|
+
|
1679
|
+
The lambda is checking whether the user is authenticated (if not, the token_owner is nil by default), and has the admin
|
1680
|
+
role - only admins can see this endpoint.
|
1681
|
+
|
1682
|
+
|
1683
|
+
|
1684
|
+
## Example <a name="example"></a>
|
1685
|
+
|
1686
|
+
Go into example directory and run it: `$ bundle exec rackup`
|
1687
|
+
go to: `http://localhost:9292/swagger_doc` to get it
|
1688
|
+
|
1689
|
+
For request examples load the [postman file]()
|
1690
|
+
|
1691
|
+
#### Grouping the API list using Namespace
|
1692
|
+
|
1693
|
+
Use namespace for grouping APIs
|
1694
|
+
|
1695
|
+
![grape-swagger-v2-new-corrected](https://cloud.githubusercontent.com/assets/1027590/13516020/979cfefa-e1f9-11e5-9624-f4a6b17a3c8a.png)
|
1696
|
+
|
1697
|
+
#### Example Code
|
1698
|
+
|
1699
|
+
```ruby
|
1700
|
+
class NamespaceApi < Grape::API
|
1701
|
+
namespace :hudson do
|
1702
|
+
desc 'Document root'
|
1703
|
+
get '/' do
|
1704
|
+
end
|
1705
|
+
|
1706
|
+
desc 'This gets something.',
|
1707
|
+
detail: '_test_'
|
1708
|
+
|
1709
|
+
get '/simple' do
|
1710
|
+
{ bla: 'something' }
|
1711
|
+
end
|
1712
|
+
end
|
1713
|
+
|
1714
|
+
namespace :download do
|
1715
|
+
desc 'download files',
|
1716
|
+
success: File,
|
1717
|
+
produces: ['text/csv']
|
1718
|
+
get ':id' do
|
1719
|
+
# file response
|
1720
|
+
end
|
1721
|
+
end
|
1722
|
+
end
|
1723
|
+
…
|
1724
|
+
|
1725
|
+
```
|
1726
|
+
|
1727
|
+
|
1728
|
+
|
1729
|
+
## Rake Tasks <a name="rake"></a>
|
1730
|
+
|
1731
|
+
Add these lines to your Rakefile, and initialize the Task class with your Api class.
|
1732
|
+
|
1733
|
+
```ruby
|
1734
|
+
require 'grape-swagger/rake/oapi_tasks'
|
1735
|
+
GrapeSwagger::Rake::OapiTasks.new(::Api::Base)
|
1736
|
+
```
|
1737
|
+
|
1738
|
+
You may initialize with the class name as a string if the class is not yet loaded at the time Rakefile is parsed:
|
1739
|
+
```ruby
|
1740
|
+
require 'grape-swagger/rake/oapi_tasks'
|
1741
|
+
GrapeSwagger::Rake::OapiTasks.new('::Api::Base')
|
1742
|
+
```
|
1743
|
+
|
1744
|
+
#### OpenApi/Swagger Documentation
|
1745
|
+
|
1746
|
+
```
|
1747
|
+
rake oapi:fetch
|
1748
|
+
params:
|
1749
|
+
- store={ true | file_name.json } – save as JSON (optional)
|
1750
|
+
- resource=resource_name – get only for this one (optional)
|
1751
|
+
```
|
1752
|
+
For mutliversion API it creates several files with following naming: file_name_`API_VERSION`.json
|
1753
|
+
|
1754
|
+
#### OpenApi/Swagger Validation
|
1755
|
+
|
1756
|
+
**requires**: `npm` and `swagger-cli` to be installed
|
1757
|
+
|
1758
|
+
|
1759
|
+
```
|
1760
|
+
rake oapi:validate
|
1761
|
+
params:
|
1762
|
+
- resource=resource_name – get only for this one (optional)
|
1763
|
+
```
|
1764
|
+
|
1765
|
+
|
1766
|
+
## Contributing to grape-swagger
|
1767
|
+
|
1768
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
1769
|
+
|
1770
|
+
## Copyright and License
|
1771
|
+
|
1772
|
+
Copyright (c) 2012-2016 Tim Vandecasteele, ruby-grape and contributors. See [LICENSE.txt](LICENSE.txt) for details.
|