rswag: RSwag + Oauth/Doorkeeper authorization not working

Hello, having issues using rswag with autorization, currently using doorkeeper+devise with password flow.

My swagger_helper.rb

require 'rails_helper'

RSpec.configure do |config|
  # Specify a root folder where Swagger JSON files are generated
  # NOTE: If you're using the rswag-api to serve API descriptions, you'll need
  # to ensure that it's configured to serve Swagger from the same folder
  config.swagger_root = Rails.root.join('swagger').to_s

  # Define one or more Swagger documents and provide global metadata for each one
  # When you run the 'rswag:specs:swaggerize' rake task, the complete Swagger will
  # be generated at the provided relative path under swagger_root
  # By default, the operations defined in spec files are added to the first
  # document below. You can override this behavior by adding a swagger_doc tag to the
  # the root example_group in your specs, e.g. describe '...', swagger_doc: 'v2/swagger.json'
  config.swagger_docs = {
    'v1/swagger.yaml' => {
      swagger: '2.0',
      info: {
        title: 'API V1',
        version: 'v1'
      },
      paths: {},
        
      securityDefinitions: {
        oauth: {
          type: :oauth2,
          flows: 'password',
          tokenUrl: 'https://localhost:3000/api/oauth/token',
          scopes: [ 'read', 'write' ]
        }
      }
    }
  }

  # Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
  # The swagger_docs configuration option has the filename including format in
  # the key, this may want to be changed to avoid putting yaml in json files.
  # Defaults to json. Accepts ':json' and ':yaml'.
  config.swagger_format = :yaml
end

And a dummy spec.

require 'swagger_helper'

describe 'Status API' do

  path '/api/status' do

    get 'Get Status' do
      security [ oauth: [ "read" ] ]
        
      tags 'Status'
      consumes 'application/json'
      parameter name: :blog, in: :body, schema: {
        type: :object,
        properties: {
          title: { type: :string },
          content: { type: :string }
        },
        required: [ 'title', 'content' ]
      }

      response '201', 'blog created' do
        let(:blog) { { title: 'foo', content: 'bar' } }
        run_test!
      end

      response '422', 'invalid request' do
        let(:blog) { { title: 'foo' } }
        run_test!
      end
    end
  end
end

Basically when clicking on authorize button on swagge ui i’m stucked with no option to enter user/password and the Authorize button not working.

auth

Looking at the browser console i see a javascript error console

One thing i need to mention is that i had to change on the autogenerated swagger_helper.rb the line openapi: '3.0.1' with swagger: '2.0',

Got that one from this #257 . Without that change i was not getting the main Authorize button on the UI.

Anybody use this combination before? Thanks

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

I’m experiencing trouble with this, too, not really sure how to use the tokenUrl key in swagger_helper. I’m using Devise, Doorkeeper, OAuth2, password flow:

  config.swagger_docs = {
    'v1/swagger.json' => {
      swagger: '2.0',
      info: {
        title: 'API V1',
        version: 'v1'
      },
      paths: {},
      securityDefinitions: {
        token: {
          type: :oauth2,
          in: :header,
          name: 'Authorization',
          flow: 'password',
          tokenUrl: 'https://localhost:3000/oauth/token',
          scopes: {}
        }
      }
    }
  }

and

post 'Creates a user' do
        tags 'Users'
        consumes 'application/json', 'application/xml'
        security [ { token: [] } ] # I've tried this with and without the object wrap, both fail
  
        parameter name: :user, in: :body, schema: {
          type: :object,
          properties: {
            id: { type: :string },
            ...
            zip: { type: :string }
          },
          required: [
            "id",
            ...
            "zip"
          ]
        }
  
        response '201', 'user created' do
          let(:user) do
            { 
              "user": params.as_json # defined as a hash above
            }
          end
          run_test!
        end

spec result:

  1) Users API when requesting the right api version /api/v1/users post user created returns a 201 response
     Failure/Error: example.run
     
     NoMethodError:
       undefined method `Authorization' for #<RSpec::ExampleGroups::UsersAPI::WhenRequestingTheRightApiVersion::ApiV1Users::Post::UserCreated:0x00005585610aa550>

Any rspec examples that includes oauth flow?

I’m not allowed to share the code that I currently have working (BigCorp would crush my bones)

Instead, let me see if I can whip up an example that I can open source for devise + rswag as I see this issue popped open yesterday too: https://github.com/rswag/rswag/issues/259