Skip to content

Commit

Permalink
Support more options in desc block
Browse files Browse the repository at this point in the history
Fix ruby-grape/grape-swagger#705.

Support `summary`, `hidden`, `deprecated`, `is_array`, `nickname`,
`produces`, `consumes`, `tags` options in desc block.
  • Loading branch information
darren987469 committed Sep 15, 2018
1 parent 41b7519 commit 795a7fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,13 @@ version 'v1', using: :param, parameter: 'v'

## Describing Methods

You can add a description to API methods and namespaces.
You can add a description to API methods and namespaces. The description would be used by [grape-swagger][grape-swagger] to generate swagger compliant documentation.

Note: Description block is only for documentation and won't affects API behavior.

```ruby
desc 'Returns your public timeline.' do
summary 'summary'
detail 'more details'
params API::Entities::Status.documentation
success API::Entities::Entity
Expand All @@ -463,7 +466,13 @@ desc 'Returns your public timeline.' do
description: 'Not really needed',
required: false
}

hidden false
deprecated false
is_array true
nickname 'nickname'
produces ['array', 'of', 'mime_types']
consumes ['array', 'of', 'mime_types']
tags ['tag1', 'tag2']
end
get :public_timeline do
Status.limit(20)
Expand All @@ -476,6 +485,9 @@ end
* `failure`: (former http_codes) A definition of the used failure HTTP Codes and Entities
* `named`: A helper to give a route a name and find it with this name in the documentation Hash
* `headers`: A definition of the used Headers
* Other options can be found in [grape-swagger][grape-swagger]

[grape-swagger]: https://github.com/ruby-grape/grape-swagger

## Parameters

Expand Down
10 changes: 9 additions & 1 deletion lib/grape/dsl/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,21 @@ def unset_description_field(field)
def desc_container
Module.new do
include Grape::Util::StrictHashConfiguration.module(
:summary,
:description,
:detail,
:params,
:entity,
:http_codes,
:named,
:headers
:headers,
:hidden,
:deprecated,
:is_array,
:nickname,
:produces,
:consumes,
:tags
)

def config_context.success(*args)
Expand Down
18 changes: 17 additions & 1 deletion spec/grape/dsl/desc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Dummy

it 'can be set with a block' do
expected_options = {
summary: 'summary',
description: 'The description',
detail: 'more details',
params: { first: :param },
Expand All @@ -34,10 +35,18 @@ class Dummy
XOptionalHeader: {
description: 'Not really needed',
required: false
}]
}],
hidden: false,
deprecated: false,
is_array: true,
nickname: 'nickname',
produces: ['array', 'of', 'mime_types'],
consumes: ['array', 'of', 'mime_types'],
tags: ['tag1', 'tag2']
}

subject.desc 'The description' do
summary 'summary'
detail 'more details'
params(first: :param)
success Object
Expand All @@ -51,6 +60,13 @@ class Dummy
description: 'Not really needed',
required: false
}]
hidden false
deprecated false
is_array true
nickname 'nickname'
produces ['array', 'of', 'mime_types']
consumes ['array', 'of', 'mime_types']
tags ['tag1', 'tag2']
end

expect(subject.namespace_setting(:description)).to eq(expected_options)
Expand Down

0 comments on commit 795a7fa

Please sign in to comment.