class SwaggerYard::OpenAPI

Public Instance Methods

components() click to toggle source
# File lib/swagger_yard/openapi.rb, line 27
def components
  {
    "schemas" => models(specification.model_objects),
    "securitySchemes" => security_defs(specification.security_objects)
  }
end
definitions() click to toggle source
# File lib/swagger_yard/openapi.rb, line 19
def definitions
  {
    "paths" => paths(specification.path_objects),
    "tags" => tags(specification.tag_objects),
    "components" => components
  }
end
map_security(h) click to toggle source
# File lib/swagger_yard/openapi.rb, line 93
def map_security(h)
  h = Hash[h.map { |k, v| [k.to_s, v] }] # quick-n-dirty stringify keys
  case type = h['type'].to_s
  when 'apiKey', 'http'
    h
  when 'oauth2'
    # convert from swagger2-style oauth2
    if (authUrl = h.delete('authorizationUrl')) && (flow = h.delete('flow'))
      { 'type' => 'oauth2', 'flows' => {
          flow => { 'authorizationUrl' => authUrl } } }.tap do |result|
        (h.keys - ['type']).each do |t|
          result['flows'][flow][t] = h[t]
        end
        result['flows'][flow]['scopes'] = {} unless result['flows'][flow]['scopes']
      end
    else
      h
    end
  else
    { 'type' => 'http', 'scheme' => type }.tap do |result|
      result['bearerFormat'] = h['format'] if h['format']
    end
  end.tap do |result|
    result['description'] = h['description'] unless h['description'].nil? || h['description'].empty?
  end
end
metadata() click to toggle source
# File lib/swagger_yard/openapi.rb, line 11
def metadata
  {
    'openapi' => '3.0.0',
    'info' => Info.new.to_h,
    'servers' => [{'url' => SwaggerYard.config.api_base_path}]
  }
end
model_path() click to toggle source
# File lib/swagger_yard/openapi.rb, line 7
def model_path
  '#/components/schemas/'
end
operation(op) click to toggle source
Calls superclass method
# File lib/swagger_yard/openapi.rb, line 48
def operation(op)
  op_hash = super
  if body_param = op.parameters.detect { |p| p.param_type == 'body' }
    op_hash['requestBody'] = {
      'description' => body_param.description,
      'content' => {
        'application/json' => {
          'schema' => body_param.type.schema_with(model_path: model_path)
        }
      }
    }
  end
  op_hash
end
parameters(params) click to toggle source
# File lib/swagger_yard/openapi.rb, line 34
def parameters(params)
  params.select { |param| param.param_type != 'body' }.map do |param|
    { "name"        => param.name,
      "description" => param.description,
      "required"    => param.required,
      "in"          => param.param_type
    }.tap do |h|
      schema = param.type.schema_with(model_path: model_path)
      h["schema"] = schema
      h["explode"] = true if !Array(param.allow_multiple).empty? && schema["items"]
    end
  end
end
response(resp, op) click to toggle source
# File lib/swagger_yard/openapi.rb, line 63
def response(resp, op)
  {}.tap do |h|
    h['description'] = resp && resp.description || op.summary || ''
    if resp && resp.type && (schema = resp.type.schema_with(model_path: model_path))
      h['content'] = { 'application/json' => { 'schema' => schema } }
      h['content']['application/json']['example'] = resp.example if resp.example
    end
  end
end
security(obj) click to toggle source
# File lib/swagger_yard/openapi.rb, line 80
def security(obj)
  case obj.type
  when /api_?key/i
    { 'type' => 'apiKey', 'name' => obj.key, 'in' => obj.name }
  when /bearer/i
    { 'type' => obj.type, 'name' => obj.name, 'format' => obj.key }
  else
    { 'type' => obj.type, 'name' => obj.name }
  end.tap do |result|
    result['description'] = obj.description if obj.description && !obj.description.empty?
  end
end
security_defs(security_objects) click to toggle source
Calls superclass method
# File lib/swagger_yard/openapi.rb, line 73
def security_defs(security_objects)
  defs = super
  Hash[defs.map do |name, d|
         [name, map_security(d)]
       end]
end
to_h() click to toggle source
# File lib/swagger_yard/openapi.rb, line 3
def to_h
  metadata.merge(definitions)
end