class ThreeScaleToolbox::OpenAPI::Swagger

Swagger object

Constants

META_SCHEMA_PATH

Attributes

raw[R]

Public Class Methods

build(raw, validate: true) click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 37
def self.build(raw, validate: true)
  self.validate(raw) if validate

  new(raw)
end
new(raw) click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 114
def initialize(raw)
  @raw = raw
end
validate(raw) click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 32
def self.validate(raw)
  meta_schema = JSON.parse(File.read(META_SCHEMA_PATH))
  JSON::Validator.validate!(meta_schema, raw)
end

Public Instance Methods

base_path() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 57
def base_path
  raw['basePath']
end
description() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 49
def description
  raw.dig('info', 'description')
end
host() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 61
def host
  raw['host']
end
operations() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 69
def operations
  @operations ||= parse_operations
end
scheme() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 65
def scheme
  Array(raw['schemes']).first
end
security() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 73
def security
  @security ||= parse_security
end
service_backend_version() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 77
def service_backend_version
  # default authentication mode if no security requirement
  return '1' if security.nil?

  case security[:type]
  when 'oauth2'
    'oidc'
  when 'apiKey'
    '1'
  else
    raise ThreeScaleToolbox::Error, "Unexpected security scheme type #{security[:type]}"
  end
end
set_oauth2_urls(spec, sec_scheme_id, authorization_url, token_url) click to toggle source

Update given spec with urls It is expected identified security scheme to be oauth2 type

# File lib/3scale_toolbox/openapi/swagger.rb, line 102
def set_oauth2_urls(spec, sec_scheme_id, authorization_url, token_url)
  sec_scheme_obj = spec.dig('securityDefinitions', sec_scheme_id)
  if sec_scheme_obj.nil? || sec_scheme_obj['type'] != 'oauth2'
    raise ThreeScaleToolbox::Error, "Expected security scheme {#{sec_scheme_id}} not found or not oauth2"
  end

  sec_scheme_obj['authorizationUrl'] = authorization_url if %w[implicit accessCode].include?(sec_scheme_obj['flow'])
  sec_scheme_obj['tokenUrl'] = token_url if %w[password application accessCode].include?(sec_scheme_obj['flow'])
end
set_server_url(spec, url) click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 91
def set_server_url(spec, url)
  URI(url).tap do |uri|
    spec['host'] = "#{uri.host}:#{uri.port}"
    spec['schemes'] = [uri.scheme]
    spec['basePath'] = uri.path
  end
end
title() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 45
def title
  raw.dig('info', 'title')
end
version() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 53
def version
  raw.dig('info', 'version')
end

Private Instance Methods

convert_flow(flow_name) click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 174
def convert_flow(flow_name)
  return nil if flow_name.nil?

  case flow_name
  when 'implicit'
    :implicit_flow_enabled
  when 'password'
    :direct_access_grants_enabled
  when 'application'
    :service_accounts_enabled
  when 'accessCode'
    :standard_flow_enabled
  else
    raise ThreeScaleToolbox::Error, "Unexpected security flow field #{flow_name}"
  end
end
fetch_security_definition(name) click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 160
def fetch_security_definition(name)
  security_definitions.fetch(name) do |el|
    raise ThreeScaleToolbox::Error, "Swagger parsing error: #{el} not found in security definitions"
  end
end
global_security_requirements() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 140
def global_security_requirements
  @global_security_requirements ||= parse_global_security_reqs
end
parse_global_security_reqs() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 144
def parse_global_security_reqs
  security_requirements.flat_map do |sec_req|
    sec_req.map do |sec_item_name, sec_item|
      sec_def = fetch_security_definition(sec_item_name)
      {
        id: sec_item_name,
        type: sec_def['type'],
        name: sec_def['name'],
        in_f: sec_def['in'],
        flow: convert_flow(sec_def['flow']),
        scopes: sec_item
      }
    end
  end
end
parse_operations() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 118
def parse_operations
  raw['paths'].flat_map do |path, path_obj|
    path_obj.flat_map do |method, operation|
      next unless %w[get head post put patch delete trace options].include? method

      {
        verb: method,
        path: path,
        description: operation['description'],
        operation_id: operation['operationId']
      }
    end.compact
  end
end
parse_security() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 133
def parse_security
  raise ThreeScaleToolbox::Error, 'Invalid OAS: multiple security requirements' \
    if global_security_requirements.size > 1

  global_security_requirements.first
end
security_definitions() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 170
def security_definitions
  raw['securityDefinitions'] || {}
end
security_requirements() click to toggle source
# File lib/3scale_toolbox/openapi/swagger.rb, line 166
def security_requirements
  raw['security'] || []
end