rspec_api_documentation - How to have multiple examples

3.4k Views Asked by At

My goal is to show multiple examples of parameters and responses using rspec_api_documentation with rswag-ui or by adding swagger-ui directly to the project. I'm having some trouble generating the correct open_api.json with my config and am wondering what I am doing wrong.

Gems:

rspec_api_documentation config for tests:

# spec/acceptance_helper.rb
require 'spec_helper'
require 'rspec_api_documentation'
require 'rspec_api_documentation/dsl'

RspecApiDocumentation.configure do |config|
  config.app = Rails.application

  config.api_name = 'My API'
  config.api_explanation = 'API Description'

  config.configurations_dir = Rails.root.join('public', 'docs', 'api', 'config')
  config.docs_dir = Rails.root.join('public', 'docs', 'api', 'generated')

  config.format = :open_api

  API_VERSIONS.each do |version|
    config.define_group(version) do |config|
      config.docs_dir = Rails.root.join('public', 'docs', 'api', 'generated', version.to_s)
    end
  end

  config.client_method = :client

  config.io_docs_protocol = 'https'

  config.request_headers_to_include = nil
  config.request_body_formatter = :json

  config.response_headers_to_include = []
  config.response_body_formatter = Proc.new { |response_content_type, response_body|
    response_body
  }
end

OpenAPI config:

swagger: '2.0'
info:
  title: My Api
  description: Blah.
  termsOfService: 'http://open-api.io/terms/'
  contact:
    name: API Support
    url: 'http://www.open-api.io/support'
    email: [email protected]
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
  version: 1.0.0
host: 'localhost:3000'
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/json

Example tests:

require 'documentation_helper'

resource 'Sessions' do
  header 'Accept', 'application/json'
  header 'Content-Type', 'application/json'

  explanation 'Endpoints to start and end API sessions for a user'

  route '/api/v1/users/sign_in', 'Sign In' do
    route_summary 'Starts a new session for a user'
    route_description 'Given valid credentials, will create a new API session for a user'

    post 'Signing in a user', document: :v1  do
      let(:user) { FactoryBot.create(:user) }

      parameter :login, 'The username or email of the user', scope: :user, required: true
      parameter :password, 'The password for the user', scope: :user, required: true

      example '401 - No user object', document: :v1 do
        request = { login: user.email, password: user.password }

        do_request(request)

        expect(status).to eq(401)
        expect(json.keys.size).to eq(1)
        expect(json['error']).to eq(I18n.t('devise.failure.unauthenticated'))
      end

      example '401 - No login', document: :v1 do
        request = { user: { password: user.password } }

        do_request(request)

        expect(status).to eq(401)
        expect(json.keys.size).to eq(1)
        expect(json['error']).to eq(I18n.t('devise.failure.unauthenticated'))
      end
    end
  end
end

Generated output:

{
  "swagger": "2.0",
  "info": {
    "title": "My Api",
    "description": "Blah.",
    "termsOfService": "http://open-api.io/terms/",
    "contact": {
      "name": "API Support",
      "url": "http://www.open-api.io/support",
      "email": "[email protected]"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    },
    "version": "1.0.0"
  },
  "host": "localhost:3000",
  "schemes": [
    "http",
    "https"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/api/v1/users/sign_in": {
      "post": {
        "tags": [
          "Sessions"
        ],
        "summary": "Starts a new session for a user",
        "description": "Given valid credentials, will create a new API session for a user",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "body",
            "in": "body",
            "description": "",
            "required": false,
            "schema": {
              "type": "object",
              "properties": {
                "user": {
                  "type": "object",
                  "properties": {
                    "login": {
                      "type": "string",
                      "description": "The username or email of the user"
                    },
                    "password": {
                      "type": "string",
                      "description": "The password for the user"
                    }
                  },
                  "required": [
                    "login",
                    "password"
                  ]
                }
              }
            }
          }
        ],
        "responses": {
          "401": {
            "description": "No login",
            "schema": {
              "type": "object",
              "properties": {
              }
            },
            "headers": {
            },
            "examples": {
              "application/json": {
                "error": "You need to sign in or sign up before continuing."
              }
            }
          }
        },
        "deprecated": false,
        "security": [

        ]
      }
    }
  },
  "tags": [
    {
      "name": "Sessions",
      "description": "Endpoints to start and end API sessions for a user"
    }
  ]
}

enter image description here Desired output (at least I think):

{
  "swagger": "2.0",
  "info": {
    "title": "My Api",
    "description": "Blah.",
    "termsOfService": "http://open-api.io/terms/",
    "contact": {
      "name": "API Support",
      "url": "http://www.open-api.io/support",
      "email": "[email protected]"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    },
    "version": "1.0.0"
  },
  "host": "localhost:3000",
  "schemes": [
    "http",
    "https"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/api/v1/users/sign_in": {
      "post": {
        "tags": [
          "Sessions"
        ],
        "summary": "Starts a new session for a user",
        "description": "Given valid credentials, will create a new API session for a user",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "body",
            "in": "body",
            "description": "",
            "required": false,
            "schema": {
              "type": "object",
              "properties": {
                "user": {
                  "type": "object",
                  "properties": {
                    "login": {
                      "type": "string",
                      "description": "The username or email of the user"
                    },
                    "password": {
                      "type": "string",
                      "description": "The password for the user"
                    }
                  },
                  "required": [
                    "login",
                    "password"
                  ]
                }
              }
            }
          }
        ],
        "responses": {
          "401": {
            "description": "Invalid params",
            "schema": {
              "type": "object",
              "properties": {
              }
            },
            "headers": {
            },
            "examples": {
              "No password": {
                "error": "You need to sign in or sign up before continuing."
              },
              "No login": {
                "error": "You need to sign in or sign up before continuing."
              }
            }
          }
        },
        "deprecated": false,
        "security": [

        ]
      }
    }
  },
  "tags": [
    {
      "name": "Sessions",
      "description": "Endpoints to start and end API sessions for a user"
    }
  ]
}

enter image description here

1

There are 1 best solutions below

0
On

Sorry I'm a year late to the party, but I just ran into this myself. I'm currently trying to hook up our documentation to postman via converting the open_api output into open_api v3 using swagger-codegen.

After going through the code for rspec_api_documentation, it looks like it uses a hash as the type to build up the document and the key that it uses is dependendent on the path + http_method. So if you're trying to have multiple examples that use the same key but have different payloads, it's going to take the first one thanks to this: https://github.com/zipmark/rspec_api_documentation/blob/d3892cc7388460a98476734963face5a7a2ac158/lib/rspec_api_documentation/open_api/node.rb#L65

The way that I got around this was by passing a dummy query param to the path in order to make a unique key. Example:

        post "/api/v2/foos?example=example1" do
          example "example1" do
            do_request(payload1)
          end
        end

        post "/api/v2/foos?example=example2" do
          example "example2" do
            do_request(payload2)
          end
        end

It's not entirely ideal, because I'd rather have the examples grouped together under the same request, but it at least gets me the documentation that I want (minus the dummy param).

Hope this workaround helps. If I find a way to submit a patch so that examples get appended instead of skipped, I'll post a link to the PR.