Skip to content

Commit 077a274

Browse files
authored
Add Extensions for Params (#785)
* Add Extensions for Params * Add Documentation for `x:` in params * Remove require `pry`
1 parent 498246e commit 077a274

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#785](https://github.com/ruby-grape/grape-swagger/pull/785): Add extensions for params - [@MaximeRDY](https://github.com/MaximeRDY).
67
* [#782](https://github.com/ruby-grape/grape-swagger/pull/782): Allow passing class name as string for rake task initializer - [@misdoro](https://github.com/misdoro).
78

89

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,20 @@ or, for more definitions:
10841084
route_setting :x_def, [{ for: 422, other: 'stuff' }, { for: 200, some: 'stuff' }]
10851085
```
10861086

1087+
- `params` extension, add a `x` key to the `documentation` hash :
1088+
```ruby
1089+
requires :foo, type: String, documentation: { x: { some: 'stuff' } }
1090+
```
1091+
this would generate:
1092+
```json
1093+
{
1094+
"in": "formData",
1095+
"name": "foo",
1096+
"type": "string",
1097+
"required": true,
1098+
"x-some": "stuff"
1099+
}
1100+
```
10871101

10881102
#### Response examples documentation <a name="response-examples"></a>
10891103

lib/grape-swagger/doc_methods/parse_params.rb

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def call(param, settings, path, route, definitions)
2626
document_range_values(settings) unless value_type[:is_array]
2727
document_required(settings)
2828
document_additional_properties(settings)
29+
document_add_extensions(settings)
2930

3031
@parsed_param
3132
end
@@ -62,6 +63,10 @@ def document_type_and_format(settings, data_type)
6263
@parsed_param[:format] = settings[:format] if settings[:format].present?
6364
end
6465

66+
def document_add_extensions(settings)
67+
GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(settings, @parsed_param)
68+
end
69+
6570
def document_array_param(value_type, definitions)
6671
if value_type[:documentation].present?
6772
param_type = value_type[:documentation][:param_type]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe '#532 allow custom format' do
6+
let(:app) do
7+
Class.new(Grape::API) do
8+
namespace :issue_784 do
9+
params do
10+
requires :logs, type: String, documentation: { format: 'log', x: { name: 'Log' } }
11+
optional :phone_number, type: Integer, documentation: { format: 'phone_number', x: { name: 'PhoneNumber' } }
12+
end
13+
14+
post do
15+
present params
16+
end
17+
end
18+
19+
add_swagger_documentation format: :json
20+
end
21+
end
22+
23+
subject do
24+
get '/swagger_doc'
25+
JSON.parse(last_response.body)
26+
end
27+
28+
let(:parameters) { subject['paths']['/issue_784']['post']['parameters'] }
29+
30+
specify do
31+
expect(parameters).to eql(
32+
[
33+
{ 'in' => 'formData', 'name' => 'logs', 'type' => 'string', 'format' => 'log', 'required' => true, 'x-name' => 'Log' },
34+
{ 'in' => 'formData', 'name' => 'phone_number', 'type' => 'integer', 'format' => 'phone_number', 'required' => false, 'x-name' => 'PhoneNumber' }
35+
]
36+
)
37+
end
38+
end

0 commit comments

Comments
 (0)