Charrette
Charrette

Reputation: 720

rake swagger:docs only generating api-docs.json

I'm using rails 5.0.0.1 and swagger-docs 0.2.9

I'm simply following the installation steps on the github project, so I have no idea why this is not working

Here is my config/initializers/swagger_docs.rb:

Swagger::Docs::Config.register_apis(
  {
    "1.0" => {
      # the extension used for the API                                                               
      :api_extension_type => :json,
      # the output location where your .json files are written to                                    
      :api_file_path => "public",
      # the URL base path to your API                                                                
      :base_path => "http://localhost:3000",
      # if you want to delete all .json files at each generation                                     
      :clean_directory => false,
      # add custom attributes to api-docs                                                            
      :attributes => {
        :info => {
          "title" => "API Documentation",
          "termsOfServiceUrl" => "http://helloreverb.com/terms/",
          "contact" => "[email protected]",
        }
      }
    }
  })

here is my controller :

class UsersController < ApplicationController

  swagger_controller :users, 'Users management'

  before_action :authenticate_user!

  before_action :set_user, only: [:show]

  swagger_api :show do
    summary "Return a user profile information"
    param :path, :id, :integer, :required
    response :ok
  end
  def show
    render json: @user, status: :ok
  end

  private

  def set_user
    @user = User.find(params[:id])
  end
end

And when I run rake swagger:docs the only generated file is api-docs.json under the public folder

Why is users.json not generated?

I've been trying with different versions of rails, I tried installing swagger-docs both from the Gemfile and from the command line, nothing is working

Upvotes: 1

Views: 907

Answers (1)

Charrette
Charrette

Reputation: 720

The problem was from the API mode of rails 5.

ApplicationController is now inheriting from ActionController::API instead of ActionController::Base,that's why it was not generating json files.

I found a solution by adding in config/initializers/swagger-docs.rb

Swagger::Docs::Config.base_api_controller = ActionController::API

Upvotes: 2

Related Questions