ruby-grape / grape-swagger-rails

Swagger UI as Rails Engine for grape-swagger gem.
MIT License
251 stars 196 forks source link

Issue with do blocks #100

Closed echan00 closed 4 years ago

echan00 commented 4 years ago

I'd like to add header parameters to the endpoint below:

      desc "Send basic GET request to test API.\nRequest should respond with 'OK'" do
        summary 'Test API'
        detail 'Send basic GET request to test API'
        failure [ { code: 400, message: 'Invalid request.' }]
        success message: '-'
      end                  
      get '/translate' do
        status 200
      end

I see in the documentation this is how you do it with one line:

desc "Return super-secret information", {
  headers: {
    "XAuthToken" => {
      description: "Valdates your identity",
      required: true
    },
    "XOptionalHeader" => {
      description: "Not really needed",
      required: false
    }
  }
}

Since I have other information in the endpoint, how do I do this in a do block? I've been looking at this all day but can't figure out the correct syntax.

dblock commented 4 years ago

Did you figure it out @echan00? For the next person.

echan00 commented 4 years ago

I didn't figure out how to implement the do block syntax, but the one liner version that will do the same thing is below:

      desc "Process an array of texts.", {
        headers: {
          "api_key" => {description: "API Key (provided)", required: true},
          "api_secret" => {description: 'API Secret (provided)', required: true},
        },          
        summary: 'Process texts',
        detail:  "Process an array of source texts by providing authentication details and source ID. \n
        All texts must be encoded UTF-8",
        failure: [ { code: 400, message: 'Invalid request.'}, { code: 401, message: 'Unauthorized. Invalid API key or secret.' } ],
        success: {message: '-'}
      }      
      params do
        requires :text, type: Array[String], desc: 'Source Texts (array of strings)', documentation: { default: "Hello World" }
        requires :src_id, type: Integer, desc: 'Source ID', documentation: { default: 3 }
      end