|
| 1 | +require 'swagger_helper' |
| 2 | + |
| 3 | +RSpec.describe 'Api::Openapi', type: :request do |
| 4 | + path '/openapi.json' do |
| 5 | + get('Fetch the OpenAPI description of this API') do |
| 6 | + tags 'Discovery' |
| 7 | + description <<~DESC |
| 8 | + Returns this document. No authentication is required. |
| 9 | +
|
| 10 | + `/api/openapi.json` is an alias for the same resource, and the YAML |
| 11 | + representation is available at `/openapi.yaml` (aliased at |
| 12 | + `/api/openapi.yaml`). |
| 13 | + DESC |
| 14 | + security [] |
| 15 | + produces 'application/json' |
| 16 | + |
| 17 | + response(200, 'successful') do |
| 18 | + schema type: :object, |
| 19 | + description: 'An OpenAPI 3.0 document describing the Hackatime API.', |
| 20 | + properties: { |
| 21 | + openapi: { type: :string, example: '3.0.1' }, |
| 22 | + info: { type: :object }, |
| 23 | + paths: { type: :object }, |
| 24 | + components: { type: :object }, |
| 25 | + servers: { type: :array, items: { type: :object } } |
| 26 | + }, |
| 27 | + required: %w[openapi info paths] |
| 28 | + |
| 29 | + run_test! do |response| |
| 30 | + body = JSON.parse(response.body) |
| 31 | + expect(body['openapi']).to eq('3.0.1') |
| 32 | + expect(body.dig('info', 'title')).to eq('Hackatime API') |
| 33 | + expect(body['paths']).to be_a(Hash) |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + path '/openapi.yaml' do |
| 40 | + get('Fetch the OpenAPI description of this API as YAML') do |
| 41 | + tags 'Discovery' |
| 42 | + description 'The YAML representation of `/openapi.json`. No authentication is required.' |
| 43 | + security [] |
| 44 | + produces 'application/yaml' |
| 45 | + |
| 46 | + response(200, 'successful') do |
| 47 | + schema type: :string, description: 'An OpenAPI 3.0 document, serialised as YAML.' |
| 48 | + |
| 49 | + run_test! do |response| |
| 50 | + body = YAML.safe_load(response.body, aliases: true) |
| 51 | + expect(body['openapi']).to eq('3.0.1') |
| 52 | + expect(body.dig('info', 'title')).to eq('Hackatime API') |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments