Top Level Namespace

Defined Under Namespace

Modules: Rswag, Schemas

Instance Method Summary collapse

Instance Method Details

#define_tags(name, consumes: 'application/json', produces: 'application/json') ⇒ Object

wrap the repeatative 4 lines in a single method for easier maintenance usage: define_tag ‘Airports’ usage: define_tag ‘Blah’, consumes: ‘multipart/form-data’



36
37
38
39
40
41
# File 'lib/rswag/helpers/request_spec_helpers.rb', line 36

def define_tags(name, consumes: 'application/json', produces: 'application/json')
  tags name
  security [Rswag::Helpers::SecurityScheme.security]
  consumes consumes
  produces produces
end

#error_detail_array(response) ⇒ Object

helper methods



62
63
64
# File 'lib/rswag/helpers/rspec_matchers.rb', line 62

def error_detail_array(response)
  JSON.parse(response.body)['errors'].collect { |h| h['detail'] }
end

#parsed_response(response, key: 'data') ⇒ Object

This file defines some general purpose methods, being loaded from spec/swagger_helper.rb Added a separate file to keep spec/swagger_helper.rb slim so that upgrades are painless



6
7
8
# File 'lib/rswag/helpers/request_spec_helpers.rb', line 6

def parsed_response(response, key: 'data')
  key ? JSON.parse(response.body)[key] : JSON.parse(response.body)
end

#parsed_response_errors(response) ⇒ Object



10
11
12
# File 'lib/rswag/helpers/request_spec_helpers.rb', line 10

def parsed_response_errors(response)
  parsed_response(response, key: 'errors')
end

#run_test_and_generate_example!(&block) ⇒ Object

‘SWAGGER_DRY_RUN=0 RAILS_ENV=test rails rswag` should run specs and auto generate examples



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rswag/helpers/request_spec_helpers.rb', line 15

def run_test_and_generate_example!(&block)
  after do |example|
    if response&.body&.present?
      example.[:response][:content] = {
        'application/json' => {
          example: JSON.parse(response.body, symbolize_names: true)
        }
      }
    end
  end

  run_test!(&block)
end

#sample_uuidObject



29
30
31
# File 'lib/rswag/helpers/request_spec_helpers.rb', line 29

def sample_uuid
  @sample_uuid ||= '52c8e67a-0589-4d4b-8732-041e7b9b44b4'
end

#schema_option(name) ⇒ Object

wrap the verbose schema option in a method



59
60
61
# File 'lib/rswag/helpers/request_spec_helpers.rb', line 59

def schema_option(name)
  { '$ref' => "#/components/schemas/#{name}" }
end

#schema_ref(name) ⇒ Object

wrap the verbose schema definition in a method, as by convention schemas are expected to defined within /components/schemas in swagger_helper.rb example: schema_ref ‘Something’ will generate schema ‘’$ref’: ‘#/components/schemas/Something’‘ example: schema_ref anyOf: [’Something’, ‘SomethingElse’]

will generate schema schema anyOf: [{ '$ref' => '#/components/schemas/Something' }, { '$ref' => '#/components/schemas/SomethingElse' }]

Use anyOf or oneOf or allOf



49
50
51
52
53
54
55
56
# File 'lib/rswag/helpers/request_spec_helpers.rb', line 49

def schema_ref(name)
  if name.is_a?(String)
    schema '$ref': "#/components/schemas/#{name}"
  elsif i[anyOf oneOf allOf].any? { |k| name.key?(k) }
    schema_names = name.values.flatten.collect { |schema_name| { '$ref' => "#/components/schemas/#{schema_name}" }}
    schema name.keys.first => schema_names
  end
end