Skip to content

Use x-example for non-body parameters #951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ def parse_additional_properties(definitions, settings)
end

def document_example(settings)
example = settings[:example]
@parsed_param[:example] = example if example
return unless settings.key?(:example)

key = @parsed_param[:in] == 'body' ? :example : :'x-example'
@parsed_param[key] = settings[:example]
end

def param_type(value_type, consumes)
Expand Down
56 changes: 44 additions & 12 deletions spec/swagger_v2/params_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ def app
Class.new(Grape::API) do
format :json

helpers do
params :common_params do
requires :id, type: Integer, documentation: { example: 123 }
optional :name, type: String, documentation: { example: 'Person' }
optional :obj, type: 'Object', documentation: { example: { 'foo' => 'bar' } }
end
end

params do
requires :id, type: Integer, documentation: { example: 123 }
optional :name, type: String, documentation: { example: 'Person' }
optional :obj, type: 'Object', documentation: { example: { 'foo' => 'bar' } }
use :common_params
end
get '/endpoint_with_examples/:id' do
{ 'declared_params' => declared(params) }
end

get '/endpoint_with_examples' do
params do
use :common_params
end
post '/endpoint_with_examples/:id' do
{ 'declared_params' => declared(params) }
end

Expand All @@ -27,14 +39,34 @@ def app
JSON.parse(last_response.body)
end

specify do
expect(subject['paths']['/endpoint_with_examples']['get']['parameters']).to eql(
[
{ 'in' => 'query', 'name' => 'id', 'type' => 'integer', 'example' => 123, 'format' => 'int32', 'required' => true },
{ 'in' => 'query', 'name' => 'name', 'type' => 'string', 'example' => 'Person', 'required' => false },
{ 'in' => 'query', 'name' => 'obj', 'type' => 'Object', 'example' => { 'foo' => 'bar' }, 'required' => false }
]
)
describe 'examples for non-body parameters ' do
specify do
expect(subject['paths']['/endpoint_with_examples/{id}']['get']['parameters']).to eql(
[
{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'x-example' => 123, 'format' => 'int32', 'required' => true },
{ 'in' => 'query', 'name' => 'name', 'type' => 'string', 'x-example' => 'Person', 'required' => false },
{ 'in' => 'query', 'name' => 'obj', 'type' => 'Object', 'x-example' => { 'foo' => 'bar' }, 'required' => false }
]
)
end
end

describe 'examples for body parameters' do
specify do
expect(subject['paths']['/endpoint_with_examples/{id}']['post']['parameters']).to eql(
[
{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'x-example' => 123, 'format' => 'int32', 'required' => true },
{ 'in' => 'body', 'name' => 'postEndpointWithExamplesId', 'schema' => { '$ref' => '#/definitions/postEndpointWithExamplesId' }, 'required' => true }
]
)
expect(subject['definitions']['postEndpointWithExamplesId']).to eql(
'type' => 'object',
'properties' => {
'name' => { 'type' => 'string', 'example' => 'Person' },
'obj' => { 'type' => 'Object', 'example' => { 'foo' => 'bar' } }
}
)
end
end
end
end
Loading