Ivan Santos
Ivan Santos

Reputation: 636

How I can add an external API documentation Into Swagger using grape-swagger?

How I can add an external API documentation?

For example, I'm using doorkeeper, POST /api/v1/token That's not a grape endpoint. How can I add this endpoint into swagger ?

Upvotes: 0

Views: 2096

Answers (1)

calfzhou
calfzhou

Reputation: 1360

I'm facing the same problem today. I'm using Grape for my API, and using Doorkeeper as OAuth 2 provider. Doorkeeper provides several API endpoints, such as POST /oauth/authorize, POST /oauth/token.

I add a dummy oauth api class to my APIs, describing each end point using desc and params. Of course, I have to manually list all parameters (requires or optional, name, type, desc, values, etc.). But I leave the implementation blank. When user call these apis, the requests will be routed to Doorkeeper to perform the real action.

For example, my code about POST /oauth/token endpoint:

module API
  class Oauth < Grape::API
    resources :oauth do
      # POST /oauth/token
      desc 'Requires for an access token'
      params do
        requires :grant_type,
                 type: String,
                 values: %w(client_credentials authorization_code)
        optional :code,
                 type: String
        requires :client_id,
                 type: String
        requires :client_secret,
                 type: String
        optional :redirect_uri,
                 type: String,
                 default: 'urn:ietf:wg:oauth:2.0:oob'
      end
      post :token do
      end
    end # resources :oauth

    add_swagger_documentation mount_path: 'oauth/swagger_doc',
                              api_version: '',
                              format: :json,
                              hide_format: true,
                              hide_documentation_path: true
  end
end

And the generated swagger doc: enter image description here

Upvotes: 3

Related Questions