grape-swagger icon indicating copy to clipboard operation
grape-swagger copied to clipboard

How to add custom headers to all apis in swagger doc

Open chiwenchen opened this issue 4 years ago • 3 comments

there is a way to add headers field in one api as document mentions, like below

class API::V1::Teachers < Grape::API
  resource :teachers do
    desc "get all teachers information" do
      headers XAuthToken: {
            description: 'Validates your identity',
            required: true
          },
          XOptionalHeader: {
            description: 'Not really needed',
            required: false
          }
    end
    success: API::V1::Entities::Teachers::Base
    get '' do
      teachers = Teacher.all

      present :teachers, teachers, with: API::V1::Entities::Teachers::Base
    end
  end
end

but is there a way to add same headers in all apis under certain resources or namespace?

chiwenchen avatar Oct 15 '20 10:10 chiwenchen

I'd be really interested to see the ability to do this

tansaku avatar May 28 '21 14:05 tansaku

I started with a little something like this:

class API::V1::Teachers < Grape::API
  resource :teachers do
    desc "get all teachers information" do
      headers: SWAGGER_HEADERS 
    end
    success: API::V1::Entities::Teachers::Base
    get '' do
      teachers = Teacher.all

      present :teachers, teachers, with: API::V1::Entities::Teachers::Base
    end
  end
end

SWAGGER_HEADERS = XAuthToken: {
            description: 'Validates your identity',
            required: true
          },
          XOptionalHeader: {
            description: 'Not really needed',
            required: false
          }

tansaku avatar May 28 '21 14:05 tansaku

ah, I see someone suggested similar in https://github.com/ruby-grape/grape/issues/2119

tansaku avatar May 28 '21 14:05 tansaku