-
Notifications
You must be signed in to change notification settings - Fork 471
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
How to use it in Rails? #11
Comments
If you follow the example, you still need to mount MyApplication::Application.routes.draw do
mount API::Root => '/'
end |
Oh cool. So I created an initializer called swagger.rb and put the block in there and add the line above in the routes.rb. Now I see the json page, but the apis array is empty except for the paths to swagger_doc.json. This is what the files look like: # lib/v1.rb
class V1 < Grape::API
...
end
# config/initializers/swagger.rb
require 'grape-swagger'
module API
class Root < Grape::API
mount V1
add_swagger_documentation
end
end |
Does the Is your Grape API itself accessible? |
Yup, I can access the grape API just as normal. Here is an example of an endpoint from resource :account do
desc "Update user's credit card id"
post "/:id/update_credit_card_id" do
authenticate!
...
end
end |
It just occurred to me that this issue might be caused by my use of grape-rabl. |
I found the reason for this bug. Apparently, Grape does this weird thing where, if you specify a version, i.e. Without a version number: {"accounts"=>
[version=, method=POST, path=/accounts/:id/update_credit_card_id(.:format)],
"app"=>[version=, method=GET, path=/app(.:format)],
"purchases"=>[version=, method=GET, path=/purchases/:id(.:format)],
"tokens"=>
[version=, method=GET, path=/tokens(.:format),
version=, method=DELETE, path=/tokens(.:format)],
"categories"=>[version=, method=GET, path=/categories(.:format)],
"events"=>
[version=, method=GET, path=/events/:id/purchase_preview(.:format),
version=, method=POST, path=/events/:id/buy(.:format),
version=, method=GET, path=/events/latest(.:format),
version=, method=GET, path=/events/near(.:format)]} With a version number: {":version"=>
[version=v1, method=POST, path=/:version/accounts/:id/update_credit_card_id(.:format),
version=v1, method=GET, path=/:version/app(.:format),
version=v1, method=GET, path=/:version/purchases/:id(.:format),
version=v1, method=GET, path=/:version/tokens(.:format),
version=v1, method=DELETE, path=/:version/tokens(.:format),
version=v1, method=GET, path=/:version/categories(.:format),
version=v1, method=GET, path=/:version/events/:id/purchase_preview(.:format),
version=v1, method=POST, path=/:version/events/:id/buy(.:format),
version=v1, method=GET, path=/:version/events/latest(.:format),
version=v1, method=GET, path=/:version/events/near(.:format)]} Pretty annoying. Should I open a ticket with them? |
Nice debugging. The combined routes is an instance variable generated by grape-swagger, so the bug is in grape-swagger itself. To define the proper 'parent resource', grape swagger looks at the first element of the path (in this case, A workaround might be to mount all your version v1-APIs on a v1-root-API and add the swagger documentation to each version of your API, although I should try it to know if it would actually work. Do you have a proposal how you would like the splitup in swagger? |
You don't need to do regex matching to get the path here. You can get that and the name of the resource from the @options instance variable in mounts::routes.each do |route|
resource = route.instance_variable_get("@options")[:namespace] || 'global'
@combined_routes[resource] ||= []
@combined_routes[resource] << route
end I fixed it using that and adding a nested map loop in the endpoint for swagger_doc.json. This is not ideal though because nickname is coming out looking like this: nickname: "POST--version-accounts--id-update_credit_card_id---format-", I'll send you a pull request so you can evaluate it. |
Coming back to this after a couple of months break. Another thing I realize it needs to have is automated reloading. Putting |
The automatic reloading is probably due to an issue with grape itself. Check ruby-grape/grape#131 for a workaround. |
Hi, Many thanks, Diarmuid |
Could you open a new ticket with some sample code? Hard to say without an actual example. |
Can someone help me understand how I can configure grape-swagger on a Rails app? I followed the instructions, but I'm still having trouble. swagger_doc.json is throwing a routing error.
...help??
The text was updated successfully, but these errors were encountered: