如何使用ruby在swagger ui中填充http_code 400/401的响应模型?

时间:2015-10-05 09:02:12

标签: ruby swagger swagger-ui

如何使用ruby在swagger ui中填充http_code 400/401的响应模型?我想为401错误添加响应模型。我的代码看起来像这样:     user_entity.rb:

module something
  module V1
    class UserEntity < Grape::Entity
      expose :id, documentation: { type: "String", desc: "User id" }
      expose :phone, documentation: { type: "String", desc: "Registered phone number" }
      expose :email, documentation: { type: "String", desc: "Email" }
      expose :created_at, documentation: { type: "String", desc: "format:yyyy-mm-ddThh:mm:ss.364+(gmt) for eg:\"2015-10-04T15:33:39.364+04:00\"" }
      expose :updated_at, documentation: { type: "String", desc: "format:yyyy-mm-ddThh:mm:ss.364+(gmt) for eg:\"2015-10-04T15:33:39.364+04:00\"" }

      expose :access_token,  if: lambda { |object, options| options[:show_access_token] == true }
      expose :access_token_expires,  if: lambda { |object, options| options[:show_access_token] == true }


      private
      def id
        object.id.to_s
      end
    end
  end
end

user_resource.rb:

module something
  module V1

    class UsersResource < Grape::API
      include something::V1::Defaults


      resource :users, desc: 'Operations about users' do

        desc "Returns all users", {
          headers: {
            "Authorization" => {description: "pass the access token as Bearer",
                                required: true
                               }
          },
           http_codes: [
            [401, 'Not authorized: The access token does not exist or is invalid'],
            [200, 'Success',UserEntity]
          ],

          detail: 'This endpoint returns all the users that have successfully registered with a valid phone number.'

        }
        get do
          User.all.to_a
        end

       .....

1 个答案:

答案 0 :(得分:0)

我自己想通了。我刚刚在defaults.rb中添加了我需要的所有错误代码,并将其添加到我的http_codes