ruby-grape / ruby-grape/grape-swagger

Unable to load all the api end points or call api in grape api swagger.

Open
#897 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
1.1k
Forks
479
Avg merge
2d 14h
Merged PRs (30d)
6

Description

I have implemented Grape swagger to load all the Grape APIs to save them as documentation and manipulate data using these API. Unfortunately, I am unable to load any API or redirect it. Even when I tried to check routes, I have seen it is unable to mount with the api's under the resources folder. I need some help to solve the issue.

The is the code structure:

Screenshot 2023-05-09 at 6 32 20 PM

app/api/api.rb

require 'grape-swagger'

module API
  class Base < Grape::API
    include API::ExceptionHandling
    version 'v1', using: :path
    format :json
    prefix :api

    add_swagger_documentation(
      api_version: 'v1',
      hide_documentation_path: true,
      mount_path: '/api/v1/swagger_doc',
      hide_format: true,
      markdown: false,
    )

    include API::Resources
    include CanCan::Ability
    include Grape::Kaminari

    mount API::V1::Resources::Base

   route :any, '*path' do
      error!({ error: 'Not Found', details: "No such route '#{request.path}'", status: 404 }, 404)
    end 
  end
end

app/api/api/v1/resources/base.rb

require 'grape-swagger'

module API
  module V1
    module Resources
      class Base < Grape::API
        include Grape::Kaminari

        version 'v1', using: :accept_version_header
        format :json

        params do
          use :pagination, per_page: 2, max_per_page: 3
        end

        mount Resources::Users
        mount Resources::Orders
        mount Resources::Authors
        mount Resources::Books
        mount Resources::Genres

        add_swagger_documentation format: :json,
                                  hide_documentation_path: true,
                                  info: {
                                    title: 'GRAPE API',
                                    description: 'API to manage ' + 'GRAPE API',
                                  },
                                  api_version: 'v1',
                                  version: true,
                                  doc_version: '1.0.0',
                                  mount_path: 'docs'
      end
    end
  end
end

app/api/api/v1/resources/books.rb

module API
  module V1
    module Resources
      class Books < Resources::Base

        helpers do
          def find_book
            Book.find(params[:id])
          end
        end

        resource :books do
          desc 'Get all books'
          get do
            books = Book.all
            present paginate(books), with: Entities::Book
          end

          desc 'Get a specific book'
          params do
            requires :id, type: Integer, desc: 'ID of the book'
          end
          get ':id', root: 'book' do
            present find_book, with: Entities::Book
          end

          route_param :id, type: Integer do
            desc 'Get authors of a specific book'
            get :book_authors do
              authors = find_book.authors
              present paginate(authors), with: Entities::Author
            end
          end

          desc 'Create a book'
          params do
            requires :book, type: Hash do
              requires :name, type: String, desc: 'Name of the book'
              requires :price, type: Float, desc: 'Price of the book'
              requires :total_copies, type: Integer, desc: 'Total Copies of the book'
            end
          end
          post do
            Book.create!(params[:book])
          end

          desc 'Update a book'
          params do
            requires :book, type: Hash do
              optional :name, type: String, allow_blank: false
              optional :price, type: Float, allow_blank: false
              optional :total_copies, type: Integer
            end
          end
          patch ':id' do
            book = find_book
            book.update!(params[:book])
            present book, with: Entities::Book, message: 'Book Successfully Updated'
          end

          desc 'Delete a book'
          params do
            requires :id, type: Integer
          end
          delete ':id' do
            book = find_book
            if book.destroy
              present book, with: Entities::Book, message: 'Book Successfully Deleted'
            else
              error = { error: book.errors.messages, message: 'Book Failed to Delete' }
              present error
            end
          end
        end
      end
    end
  end
end

config/routes.rb

Rails.application.routes.draw do
  constraints Clearance::Constraints::SignedIn.new { |user| user.admin? } do
    require 'sidekiq/web'
    mount Sidekiq::Web => '/sidekiq'
  end

  # scope '(:locale)', locale: /en|bn/ do
  #   mount API::Base, at: '/'
  #   mount GrapeSwaggerRails::Engine => '/swagger'
  # end

  mount API::Base => '/api'
  mount GrapeSwaggerRails::Engine => '/swagger'
end

config/initializers/swagger.rb

GrapeSwaggerRails.options.app_name = 'GRAPE API'
GrapeSwaggerRails.options.url = 'api/docs.json'
GrapeSwaggerRails.options.before_action do
  GrapeSwaggerRails.options.app_url = request.protocol + request.host_with_port + '/'
#   GrapeSwaggerRails.options.api_key_default_value = request.cookies['token']
end
GrapeSwaggerRails.options.headers['Accept-Version'] = 'v1'
# GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
GrapeSwaggerRails.options.hide_api_key_input = true

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the mounts in config/routes.rb, app/api/api.rb, and app/api/api/v1/resources/base.rb, then inspect the Books resource and config/initializers/swagger.rb. Run the route listing and request the documented endpoints to identify which API or documentation mount is unavailable. Done means the resources load and the Swagger documentation can be opened and used.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.