ruby-grape / grape-swagger Goto Github PK
View Code? Open in Web Editor NEWAdd OAPI/swagger v2.0 compliant documentation to your grape API
License: MIT License
Add OAPI/swagger v2.0 compliant documentation to your grape API
License: MIT License
NameError (uninitialized class variable @@hide_format in #Class:0x00000006debf88):
grape-swagger (0.6.0) lib/grape-swagger.rb:68:in `block (2 levels) in setup'
i'm using mruby 1.9.2-p290, i dont know whether this will work on higher version, but simply i dont think initialize a class variable in Class.new(Grape::API) can be reached in helpers do(which means Module.new)
Because of the following code in grape-swagger.rb, around line 126:
paramType = path.match(":#{param}") ? 'path' : (method == 'POST') ? 'body' : 'query'
you cannot have a nice swagger UI for a PUT
method with a text box for body. Should the above be amended to be something like this:
paramType = path.match(":#{param}") ?
'path' : (method == 'POST' || method == 'PUT') ? 'body' : 'query'
or should we introduce a DSL way to specify param type of 'body', something along the lines of
params do
optional :body, body: true, desc: 'This parameter is the body of the request'
end
I am willing to submit a pull request once an acceptable approach is determined..
Take the following example:
params do
optional :preferences, type: Array do
requires :key
requires :value
end
requires :name, type: Hash do
requires :first_name
requires :last_name
end
end
In swagger-ui, there would be an empty text field for preferences
and name
. Is this suppoed to be this way ? Tested using the github version of grape
and grape-swagger
.
Thanks
Hi,
Great work with this project, I've been using it for a wile to document my grape api's, unfortunately the latest version of swagger-ui throws This API is using a deprecated version of Swagger!
Planning for a new version?
Regards,
I am trying to serve the docs from a relative path for an app using HTTPS. The Swagger JSON returns something like
{
"apiVersion":"v1",
"swaggerVersion":"1.2",
"resourcePath":"",
"apis":[{
"path":"/auth/user.{format}",
"operations":[{
"produces":["application/xml","application/json", "application/vnd.api+json","text/plain"],
"notes":"",
"summary":"",
"nickname":"GET-auth-user---format-",
"httpMethod":"GET",
"parameters":[]
}
]}],
"basePath":"https://accounts.kyck.dev:80/api"}
https
and port 80 are in direct conflict. I have tried various options with add_swagger_documentation
, including include_base_url: false
and root_base_path: false
Not sure what I am doing wrong, but would love some help.
Hello,
I currently have our Grape API mounted at /api
, but the swagger_doc.json file being generated does not reflect that fact - it's assuming we have it mounted at /
, which causes discovery problems for swagger-UI.
The generated doc looks like (formatted for readability):
{
"apiVersion":"0.1",
"swaggerVersion":"1.1",
"basePath":"http://localhost:3000",
"operations":[],
"apis":[
{
"path":"/swagger_doc/sites.{format}"
},
{
"path":"/swagger_doc/users.{format}"
},
{
"path":"/swagger_doc/photos.{format}"
},
{
"path":"/swagger_doc/.{format}"
}
]
}
I believe the basePath should be http://localhost:3000/api
Additionally, the apiVersion in the generated doc is 0.1
, but the specified version of our API is v1
, so I'm not sure what's going on there, but that's also incorrect.
Hello,
why can't I use the allowMultiple Fieldname described here:
https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#524-parameter-object
Actually I want the multiple option box from:
http://petstore.swagger.wordnik.com/#!/pet/findPetsByStatus
There are predefined values for a parameter with an enum "available, pending, sold":
{
"path":"/pet/findByStatus",
"operations":[
{
"method":"GET",
"summary":"Finds Pets by status",
"notes":"Multiple status values can be provided with comma seperated strings",
"type":"array",
"items":{
"$ref":"Pet"
},
"nickname":"findPetsByStatus",
"authorizations":{
},
"parameters":[
{
"name":"status",
"description":"Status values that need to be considered for filter",
"defaultValue":"available",
"required":true,
"type":"string",
"paramType":"query",
"allowMultiple":true,
"enum":[
"available",
"pending",
"sold"
]
}
],
"responseMessages":[
{
"code":400,
"message":"Invalid status value"
}
]
}
]
},
Hi. I am just updated from 0.2.0 to 0.3.0. As soon I run "rails s" I am getting now this exception:
.rvm/gems/ruby-1.9.3-p392@versioneye/gems/grape-swagger-0.3.0/lib/grape-swagger.rb:8:in singletonclass': undefined method
mount' for class `Class' (NameError)
Any ideas?
I'm hoping this isn't an extremely stupid question...
I loaded up grape-swagger, and set some basic options, and for the most part it is working great! However, I've been stuck trying to figure out how to generate the documentation in a way to tell swagger-ui that my api expects post-data of a json.
Using the same swagger-ui code, I can point to the petstore example api, and see that it looks like it is acting the way I would like, with the param type as body, and they content type as application/json. Inspecting the example petstore endpoint swagger at http://petstore.swagger.wordnik.com:80/api/pet, I suspect that the "consumes: "application/json", and parameter paramType: "body", are the bits that I am not figuring out how to render, and would make the swagger-ui work this way.
Is there any chance someone could give an example of how to do this?
Thanks!
mount V2 => '/api' (routes.rb)
version 'v2', :using => :path, :vendor => 'hal'
add_swagger_documentation(:base_path => "http://#{APP_CONFIG['default_hosts']["#{Rails.env}"]}/api/v2") if Rails.env.development?
Request URL
http://0.0.0.0:3000/api/v2/{version}/seller/53.json
I just tried to update my app to the latest grape, but it fails. My app has the API mounted in /api
and consists of several mounted grape classes
module API
class Base < Grape::API
default_format :json
mount API::Assets => '/assets'
add_swagger_documentation(
hide_documentation_path: true,
)
end
end
The /swagger_doc/
is missing in the path for the sub modules.
Do I need to change something to make it work again? Is this a bug?
How to have my custom datatype Model & Model Schema visible in Swagger UI similar to: http://petstore.swagger.wordnik.com/#!/user/createUser_post_1 ?
With https://github.com/wordnik/swagger-core/wiki/Datatypes , seems like model API has to be defined somewhere.
Is there any code example with this implementation?
Hello,
I need some help regarding the Usage section in the documentation.
Should I put the following code in a file called root.rb and place it under the app/api folder?
I tried that but whe I hit http://localhost:3000/swagger_docs.json I got an error saying that "/swagger_docs.json" does not exist. Do I need to define another route in routes.rb?
require 'grape-swagger'
module API
class Root < Grape::API
mount API::Cats
mount API::Dogs
mount API::Pirates
add_swagger_documentation
end
end
Also, if I only have one set of API, do I still need to do this?
Or can I just put add_swagger_documentation line in the individual API files?
In version 0.5.0 , the POST methods are working properly
but in version 0.6.0 , i am getting TypeError : can't convert nil into Hash for the POST methods
Full trace:
rack (1.4.5) lib/rack/request.rb:221:in merge' rack (1.4.5) lib/rack/request.rb:221:in
params'
grape (0.5.0) lib/grape/http/request.rb:6:in params' grape (0.5.0) lib/grape/endpoint.rb:172:in
params'
grape (0.5.0) lib/grape/endpoint.rb:395:in block in run' grape (0.5.0) lib/grape/endpoint.rb:394:in
each'
grape (0.5.0) lib/grape/endpoint.rb:394:in run' grape (0.5.0) lib/grape/endpoint.rb:164:in
block in call!'
grape (0.5.0) lib/grape/middleware/base.rb:24:in call' grape (0.5.0) lib/grape/middleware/base.rb:24:in
call!'
grape (0.5.0) lib/grape/middleware/base.rb:18:in call' grape (0.5.0) lib/grape/middleware/base.rb:24:in
call!'
grape (0.5.0) lib/grape/middleware/base.rb:18:in call' grape (0.5.0) lib/grape/middleware/error.rb:26:in
block in call!'
grape (0.5.0) lib/grape/middleware/error.rb:25:in catch' grape (0.5.0) lib/grape/middleware/error.rb:25:in
call!'
grape (0.5.0) lib/grape/middleware/base.rb:18:in call' rack (1.4.5) lib/rack/head.rb:9:in
call'
rack (1.4.5) lib/rack/builder.rb:134:in call' grape (0.5.0) lib/grape/endpoint.rb:165:in
call!'
grape (0.5.0) lib/grape/endpoint.rb:155:in call' rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in
block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in block in recognize' rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in
optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in recognize' rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in
call'
grape (0.5.0) lib/grape/api.rb:480:in call' grape (0.5.0) lib/grape/api.rb:49:in
call!'
grape (0.5.0) lib/grape/api.rb:45:in call' journey (1.0.4) lib/journey/router.rb:68:in
block in call'
journey (1.0.4) lib/journey/router.rb:56:in each' journey (1.0.4) lib/journey/router.rb:56:in
call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in
call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in
call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in call' newrelic_rpm (3.6.5.130) lib/new_relic/rack/error_collector.rb:12:in
call'
newrelic_rpm (3.6.5.130) lib/new_relic/rack/agent_hooks.rb:22:in call' newrelic_rpm (3.6.5.130) lib/new_relic/rack/browser_monitoring.rb:16:in
call'
newrelic_rpm (3.6.5.130) lib/new_relic/rack/developer_mode.rb:28:in call' rack-google-analytics (0.11.0) lib/rack/google-analytics.rb:18:in
_call'
rack-google-analytics (0.11.0) lib/rack/google-analytics.rb:15:in call' warden (1.2.1) lib/warden/manager.rb:35:in
block in call'
warden (1.2.1) lib/warden/manager.rb:34:in catch' warden (1.2.1) lib/warden/manager.rb:34:in
call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in call' rack (1.4.5) lib/rack/etag.rb:23:in
call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in call' actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in
call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in call' actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in
call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in context' rack (1.4.5) lib/rack/session/abstract/id.rb:205:in
call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in call' activerecord (3.2.13) lib/active_record/query_cache.rb:64:in
call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in call' actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in
block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in _run__153453904__call__699551650__callbacks' activesupport (3.2.13) lib/active_support/callbacks.rb:405:in
__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in _run_call_callbacks' activesupport (3.2.13) lib/active_support/callbacks.rb:81:in
run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in call' actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in
call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in call' actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in
call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in call' railties (3.2.13) lib/rails/rack/logger.rb:32:in
call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in block in call' activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in
tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in call' actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in
call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in call' rack (1.4.5) lib/rack/runtime.rb:17:in
call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in call' rack (1.4.5) lib/rack/lock.rb:15:in
call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in call' railties (3.2.13) lib/rails/engine.rb:479:in
call'
railties (3.2.13) lib/rails/application.rb:223:in call' railties (3.2.13) lib/rails/railtie/configurable.rb:30:in
method_missing'
rack-cors (0.2.8) lib/rack/cors.rb:54:in call' rack (1.4.5) lib/rack/content_length.rb:14:in
call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in call' thin (1.5.1) lib/thin/connection.rb:81:in
block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in catch' thin (1.5.1) lib/thin/connection.rb:79:in
pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in process' thin (1.5.1) lib/thin/connection.rb:39:in
receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in run_machine' eventmachine (1.0.3) lib/eventmachine.rb:187:in
run'
thin (1.5.1) lib/thin/backends/base.rb:63:in start' thin (1.5.1) lib/thin/server.rb:159:in
start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in run' rack (1.4.5) lib/rack/server.rb:268:in
start'
railties (3.2.13) lib/rails/commands/server.rb:70:in `start'
can you help me identify the problem
How can I hide the swagger_doc route in the generated documentation?
hi there I have the following grape-entites
module HQ
module Entities
class Client < Grape::Entity
expose :created_by_id, documentation: {type: Integer, desc: "Created by id"}
expose :updated_by_id, documentation: {type: Integer, desc: "Updated by id"}
expose :first_name, documentation: {type: String, desc: 'First Name.'}
expose :last_name, documentation: {type: String, desc: 'Last Name.'}
expose :zipcode, documentation: {type: String, desc: 'Zip Code.'}
expose :emails, using: HQ::Entities::Email, documentation: {type: ::Item, desc: 'Emails.'}
end
end
end
module HQ
module Entities
class Email < Grape::Entity
expose :address, documentation: {type: Integer, desc: "Email"}
end
end
end
the problem that I need that swagger generate the UI with the complex form I means client has many emails
expose :emails, using: HQ::Entities::Email, documentation: {type: Array, desc: 'Emails.'}
what type I need to use? I added array but it show me just as string field like this
Hi,
Does anyone knows how to use grape-swagger to generate documentation for complex response data types?
For example:
I have the following grape entity
class CreatedCustomer < BaseEntity
expose :customer, using: Entities::Customer, documentation: { type: 'Customer'}
expose :token, using: Entities::AuthResult, documentation: { type: 'AuthResult'}
When I generate documentation for CreatedCustomer response there is no $ref inside CreatedCustomer model which is as far as I understood the way to reference complex models in swagger. Moreover the only model generated by grape-swagger is CreatedCustomer.
Generated model looks like this:
"CreatedCustomer":
{"id":"CreatedCustomer","name":"CreatedCustomer",
"properties":{"customer":{"type":"Customer"}
}}
While i would expect something like this(ignoring second field for sake of example)
"CreatedCustomer":
{"id":"CreatedCustomer","name":"CreatedCustomer",
"properties":{"customer":{"$ref":"Customer"}
}}
"Customer":
{"id":"Customer", "name":"Customer",
"properties":{"email":{"type":"string"}
}}
I'm having a hard time getting it to work correctly. Anybody has a demo app I can take a look at?
Take for example the following :
params do
requires :id, type: Integer
optional :text, type: String, regexp: /^[a-z]+$/
group :media do
requires :url
end
optional :audio do
requires :format, type: Symbol, values: [:mp3, :wav, :aac, :ogg], default: :mp3
end
end
put ':id' do
# params[:id] is an Integer
end
When using swagger-ui, the parameter format
is always required, even though audio
is optional.
Currently if a route returns an entity that nests other entities, the nested entities do not display. By allowing entity to be an array the nested entities can be included.
after send resque,genera Request URL http://0.0.0.0:9000/v1/users/111.json?api_key=special-key,and my console display "Status: 200, Content-Length: 40, Response Time: 20.12ms",but grape-swagger show Response Code is 0.I access http://0.0.0.0:9000/v1/users/111.json?api_key=special-key in my browser, it show correct date " {"id":55500,"qq":null,"icompany_id":901}"
Hi (again)
not sure if this is the right place to set the issue, but, what I have to do to describe the endpoints and being able to select the response content_type (actually, json or xml, jsonp later) from swagger-ui? I've seen the documentation(s, for grape and swagger) and I didn't find anything. Even trying
content_type :json, 'application/json'
content_type :xml, 'application/xml'
I didn't got anything. Using grape, grape-swagger with RoR 3.1.3
Cheers
w i l l y
I have some issues with this project. It doesn't work very well with swagger-ui. I am getting wrong urls from grape-swagger. I posted the bug report here:
swagger-api/swagger-ui#130 (comment)
I appreciate any kind of help.
Hi, im stucked... It seems that POST params are not displayed/parsed right. (GET is working)
An example:
desc "POST example with param"
params do
requires :id, :type => Integer, :desc => "id."
end
post :test do
User.find_by_id(params[:id])
end
This endpoint works as expected with this command: (it return a JSON object of selected user)
curl -X POST -d '{"id": 1}' http://localhost:3000/api/v1/users/test.json -H 'Accept: application/json' -H 'Content-Type:application/json' -s
But the swagger-UI renders a textarea instead a simple input field and seems not to send the data in JSON.
The API yells:
"undefined method `each' for 1:Fixnum"
Inspecting, it tells me, that the body is just "1", shouldn't this be {"id": 1}?
The generated definition:
{
"path": "/v1/users/test",
"operations": [
{
"notes": null,
"summary": "POST example with param",
"nickname": "POST--version-users-test---format-",
"httpMethod": "POST",
"tags": null,
"errorResponses": null,
"deprecated": null,
"responseClass": null,
"parameters": [
{
"paramType": "body",
"name": "id",
"description": "id.",
"dataType": "Integer",
"required": true,
"allowableValues": null,
"defaultValue": null,
"allowMultiple": null
}
]
}
]
}
Or (hopefully...) did I miss something? Thanks and regards, Klaas
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??
I want to mount my route api on /api/
for some reason grape-swagger doesn't pick this up by default. I assume it is because of request.base_url
.
There is a handy config option called :base_path
but sadly this is a simple variable and wont have access to the request object and since I do not want to hard code the hostname it wont work.
I tried setting this to a lambda function with out much joy as well.
I hacked around this by adding a :base_path_suffix
config option see drubin@93608a5
add_swagger_documentation({
:base_path_suffix=> '/api',
:api_version=>'v1',
:hide_documentation_path=>true,
:markdown=>true
}
)
If any one has a better work around? or can point me in why request
would not work as part of a lambda function it would be great.
Endpoints expand very slow after updating swagger-ui for enabling versioning.
No, really? A bunch of damn useful pull requests, hanging there for months, and not a single comment given. Tim, I know you're alive, give some love to this repo, will you?
Swagger supports 3 types of parameters: query, body and header. Since it is common to place and authentication token in the header of an app, or to even use basic authentication with a token, we need a way to move this into swagger.
To get this going I think the simplest way to start is placing headers in the same block with notes and just describing them.
In the future I am thinking we need to add a special header field specifically for basic auth into swagger-ui because even though you can add the Authorization
header you still need to Base64 encode either your token or your username/password combo.
I have a working version of the former in a branch on my fork. The latter needs more thought.
Both need to have discussions on how this should properly be moved into Grape - if it is providing a facility for required parameters then it should also handle prescribing basic auth or required headers.
I add grape-swagger to my app,but find error.It's singletonclass': undefined method
mount' for class `Class' (NameError),error info,wo try to much way ,but don't solve it .so I hope you help me
Currently we have to accept the nickname that is auto generated for each operation. It would be nice to be able to specify a nickname in the endpoint directly.
I have a fix for this and will be submitting a pull request shortly.
/Leon
Hi,
I am a little unclear on how to set the :mount_path and/or :base_path options on add_swagger_documentation. Here’s my context:
Rails’ routes.rb:
require 'grape'
Blog::Application.routes.draw do
mount Blog::API => "/api"
end
The API code:
module Blog
class API < Grape::API
mount Blog::API1
add_swagger_documentation
end
end
module Blog
class API1 < Grape::API
format :json
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
end
resource :weblogs do
……and so on ……
So, for add_swagger_documentation, how do I set the :mount_path and/or :base_path options?
Many thanks, D
https://github.com/tim-vandecasteele/grape-swagger/blob/master/lib/grape-swagger.rb#L143
This will have loads of implications in a existing rails application
def try(*a, &b)
if a.empty? && block_given?
yield self
else
__send__(*a, &b)
end
end
I don't run Rails but Goliath. How should I mount API's when I don't have the mount method?
I tried PadrinoEatsGrape but it's pretty confusing and files are missing (boot.rb etc).
I tried the Rack way e.g 'run API_FOO' in a config.ru file but:
gems/grape-swagger-0.2.0/lib/grape-swagger.rb:6:in singletonclass': undefined method
mount' for class `Class' (NameError)
Should I try to find a rack middleware that gives the mount method?
My environment still requires sticking to v.1.8.7. When trying to rack up my app, I get error messages for 1.9-style hash declarations:
/home/uriel/.rvm/gems/ruby-1.8.7-p352/gems/backports-2.6.5/lib/backports/tools.rb:314:in `require_without_backports': /home/uriel/.rvm/gems/ruby-1.8.7-p352/gems/grape-swagger-0.3.0/lib/grape-swagger.rb:71: odd number list for Hash (SyntaxError)
apiVersion: api_version,
^
/home/uriel/.rvm/gems/ruby-1.8.7-p352/gems/grape-swagger-0.3.0/lib/grape-swagger.rb:71: syntax error, unexpected ':', expecting '}'
apiVersion: api_version,
^
/home/uriel/.rvm/gems/ruby-1.8.7-p352/gems/grape-swagger-0.3.0/lib/grape-swagger.rb:72: syntax error, unexpected ':', expecting '='
swaggerVersion: "1.1",
^
/home/uriel/.rvm/gems/ruby-1.8.7-p352/gems/grape-swagger-0.3.0/lib/grape-swagger.rb:72: syntax error, unexpected ',', expecting kEND
/home/uriel/.rvm/gems/ruby-1.8.7-p352/gems/grape-swagger-0.3.0/lib/grape-swagger.rb:73: syntax error, unexpected ',', expecting kEND
/home/uriel/.rvm/gems/ruby-1.8.7-p352/gems/grape-swagger-0.3.0/lib/grape-swagger.rb:75: syntax error, unexpected ':', expecting kEND
apis: routes_array
Hi,
I have a Rails 3 application with a REST API built using Grape. Some relevant code excerpts are as follows:
routes.rb
require 'grape'
Blog::Application.routes.draw do
mount Blog::API => "/"
end
api.rb
require 'grape'
require 'grape-swagger'
module Blog
class API < Grape::API
mount Blog::API1
add_swagger_documentation
end
end
api1.rb
require 'grape'
module Blog
class API1 < Grape::API
format :json
resource :weblogs do
get do
Weblog.all
end
……………………..
The API works fine when I test it with curl. I’ve deployed it to Cloud Foundry if you want to test it. The URL is http://blog-api.cloudfoundry.com and a sample curl request would be:
curl -i http://blog-api.cloudfoundry.com/weblogs
I’ve followed your instructions for adding grape-swagger and then try to explore it using the online swagger demo. Everything renders ok, however, when I test any part of my api using the demo, the response body is empty and the response code is 0; although the log file on the server-side shows the request being handled. Any thoughts as to what is wrong?
Many thanks, Diarmuid
Any plans to update this lib in that way that it works together with Grape 4.0.0 ?
Last release (v0.3.0) was in October 2012. Can you release an update that works with grape 0.3.x? Current master branch depends on grape 0.2.0.
I believe I have issues with 0.3.0 gem which were fixed in commits some time ago.
Thanks!
I am unclear about how to specify the API version.
When I set version 'v1'
then I still see "apiVersion":"0.1"
in the JSON, which was a surprise for me.
More details:
In this implementation grape-swagger sets the apiVersion to 0.1:
class Root < Grape::API
version 'v1', :using => :path
add_swagger_documentation
[...]
end
{"apiVersion":"0.1","swaggerVersion":"1.1",...}
In this implementation grape-swagger sets the apiVersion to v1
class Root < Grape::API
version 'v1', :using => :path
add_swagger_documentation :api_version => "v1"
[...]
end
{"apiVersion":"v1","swaggerVersion":"1.1",...}
The 2nd implementation example looks redundant to me. I might just not understand how this is meant to be used.
Is this the intended behavior?
I want to make only some routes visible to all users and some only to people who have an API key. I don't want to show them in Swagger at all if they don't provide a valid API key (this would be useful for admin API or unpublished features).
Is it possible? I couldn't find a solution for this.
In the combination of grape, grape-swagger, and swagger UI, it is sometimes difficult to figure out where the correct place for documenting certain features is.
Here my understanding of the components, just to double check that I got this right:
Now my question:
Swagger supports the documentation of the HTTP status codes that are generated by an API. I couldn't find any explanation in either of the relevant repositories about how to create a grape API such that grape-swagger can pick up the status codes automatically.
I read the source of grape-swagger and from there derive that this is how it works:
get '/', :http_codes => [
[400, "Invalid parameter entry"],
] do
...
end
This leads to this JSON snippet in the generated swagger specs:
"operations": [
{
"errorResponses": [
{
"reason": "Invalid parameter entry ",
"code": 400
}
],
And it is visualized in swagger UI like this:
Any recommendation on where this feature should be documented?
This is a question, rather than an issue. Sorry for abusing the project issues for this purpose but I didn't see a better way to contact you.
My question is if you came a across a gem that can generate Swagger-compliant JSON for pure Rails APIs? I am currently using plain ActionControllers and ActiveModel::Serializer for my API, and I haven't found a way yet to marry that with Swagger nicely.
Out of curiosity: Why do you favor grape for API building?
Thanks a lot :)
One of the API Endpoints expects an file upload. The api works fine, but swagger-ui does not render an file upload. Instead of that it renders an input filed. What I have to do to get a file upload rendered?
Many Thanks in advance.
In our Rails app we are mounting two separate Grape APIs, e.g. /api/partner/v1 and /api/clients/v1. Now when I call add_swagger_documentation
for each of these APIs, two separate swagger_doc
entry points are mounted correctly. However querying these two entry points returns the same API documentation. E.g. querying /api/partner/v1/swagger_doc.json returns the documentation for API::Partner but querying /api/clients/v1/swagger_doc.json also returns the documentation for API::Partner.
I've been doing some debugging on this issue and it seems that the @@target_class
class variable points to the same API class in the get @@mount_path
definition:
get @@mount_path do
Rails.logger.debug("XXXX self class: #{self.class}, #{self.class.object_id}")
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
routes = @@target_class::combined_routes
Rails.logger.debug("XXXX routes for #{@@target_class}: #{routes}")
This is despite the fact that two separate documentation classes get created with separate class variable. Somehow these class variables get merged into a single new instance of the Grape::Endpoint class. Here is what I see in the logs:
Started GET "/api/client/v1/client_api_docs.json" for 127.0.0.1 at 2013-03-06 01:38:24 +0800
XXXX self class: Grape::Endpoint, 70097079507800
XXXX routes for API::Client: {"search"=>[version=v1, method=GET, path=/:version/search(.:format)]}
Started GET "/api/partner/v1/partner_api_docs.json" for 127.0.0.1 at 2013-03-06 01:19:14 +0800
XXXX self class: Grape::Endpoint, 70097079507800
XXXX routes for API::Client: {"client_api_docs"=>[version=v1, method=GET, path=/:version/client_api_docs(.:format), version=v1, method=GET, path=/:version/client_api_docs/:name(.:format)], "search"=>[version=v1, method=GET, path=/:version/search(.:format)]}
Maybe this data shouldn't be stored in class variables?
I created a repository (https://github.com/robertogyn19/grape-swagger-header) explaining the problem and how to reproduce it. If anyone has the same problem or can help me. I'm not even sure if the problem is in the grape-swagger.
The swagger-ui version: [Master](https://github.com/wordnik/swagger-ui/tree/4601f6270235489581acd80548620716506311a5 /)
Problem with header param on request
I'm having a problem to send the api_key parameter in the header of the request. Something like:
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "header"));
The swagger makes an OPTIONSrequest to the server and request / response headers is something like:
Request Headers:
OPTIONS /doc HTTP/1.1
Host: localhost:9292
Access-Control-Request-Method: GET
Origin: http://localhost:3001
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.114 Safari/537.36
Access-Control-Request-Headers: api_key
Accept: */*
Referer: http://localhost:3001/
Response Headers:
HTTP/1.1 204 No Content
Access-Control-Request-Headers: Content-Type, api_key
Access-Control-Request-Method: GET, POST, DELETE, PUT, PATCH, OPTIONS
Access-Control-Allow-Origin: *
Allow: OPTIONS, GET, HEAD
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-06-27)
Date: Wed, 20 Nov 2013 17:00:58 GMT
Connection: Keep-Alive
However I get an error message:
OPTIONS http://localhost:9292/doc Request header field Api_key is not allowed by Access-Control-Allow-Headers.
From what I read, thoroughly, this error is due to CORS, but api_key parameter is Access-Control-Request-Headers.
To check I executed:
$curl -i -X OPTIONS -H "api_key: '123'" -u roberto:pass localhost:9292/doc.json
HTTP/1.1 204 No Content
Access-Control-Request-Headers: Content-Type, api_key, Authorization
Access-Control-Request-Method: GET, POST, DELETE, PUT, PATCH, OPTIONS
Access-Control-Allow-Origin: *
Allow: OPTIONS, GET, HEAD
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-06-27)
Date: Wed, 20 Nov 2013 17:41:36 GMT
Connection: Keep-Alive
To reproduce the error, follow these steps:
1. git clone [email protected]:robertogyn19/grape-swagger-header.git
Start server (localhost:9292)
2.1. cd grape-swagger-header/grape-swagger
2.2. gem install bundler (if not installed)
2.3. bundle install
2.4. bundle exec rackup
Start swagger client (localhost:3001)
3.1. cd grape-swagger-header/swagger-ui
3.2. npm install
3.3. node app.js
4. Open a browser on http://localhost:3001
5. Put any value on api_key box and click Explore
If you need any other information, let me know.
Hi there
yes, I've read the 'Usage' section of the readme and checking the previous issues, and I have a quite opposite issue than Issue #19.
I have the API built using Rails 3 and the swagger-ui, both in the same machine, API responding on port 3003 (by running rails -p 3003 app-api), swagger-ui in the localhost/~user/Sites and, in addtition, in another_computer/~user/public_html, for remote testing purposes. In both cases, the API funcitons are listed ok, but it is impossible to make requests, I always get in debugger the (in)famous
submitting http://localhost:3003/api/uniprot/status.json
XMLHttpRequest cannot load http://localhost:3003/api/uniprot/status.json. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
I have at the top of the file defining the API:
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
header['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT, PATCH, DELETE'
header['Access-Control-Allow-Headers'] = 'true'
end
It is more than it is indicated at the docs, but after reading a couple of forum I just wanted to be sure.
And it is working OK if I use the online swagger-ui service...
I know I am missing something but I can't figure out what. can anybody light me up to solve this (annoying) issue??
Cheers and sorry if this is an silly (and repeating) issue.
w i l l y
I tried this recently without much luck as a couple of the fields were missing from the swagger description emitted by grape-swagger. Foolishly I didn't document this at the time so I'm not sure which fields these were. It may be the case that adding these fields isn't possible with the current grape DSL. I will investigate further as I require this functionality but have added this issue for now as a stake in the ground.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.