ruby-grape / ruby-grape/grape-swagger
Easy way to document only declared params
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 1.1k
- Forks
- 479
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 6
Description
I'm using Grape Entity Swagger to document all my parameters inside the Entities instead of the endpoints like this:
# my endpoint does not document any parameter...
params = params do
requires :name, type: String
end
desc: 'Creates a product',
success: { code: 201, model: Product },
params: Product.params_doc(params)
post '/products' do
# ...
end
And to achieve this I'm using a custom method params_doc that returns only the declared params' documentation.
class CustomEntity < Grape::Entity
def self.params_doc(params)
declared_params = params.instance_variable_get('@declared_params').flatten
declared_params.reduce({}) do |memo, param|
key = param.is_a?(Hash) ? param.keys.first : param
memo[key] = documentation[key]
memo
end
end
end
class Product < CustomEntity
expose :id, documentation: { type: 'string', desc: 'The ID of the Product' }
expose :name, documentation: { type: 'string', desc: 'foo bar' }
end
This works as expected and only documents the :name parameter for the endpoint. However this feels hacky and have caused some trouble if params block is not specified before the desc call. So I'm wondering if there's an easier way to achieve this.
Any help is appreciated.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how Grape Entity documentation is converted into Swagger parameters when an endpoint uses params and desc, using the Product and CustomEntity examples in the issue. Add a supported way to include only declared parameters without relying on params_doc or declaration order, then verify the generated documentation contains only :name for the example endpoint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api, documentation
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100