class OpenApi::DSL::SchemaObj

github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#schemaObject

Constants

SELF_MAPPING

Attributes

processed[RW]
type[RW]

Public Class Methods

new(type = nil, schema) click to toggle source
# File lib/oas_objs/schema_obj.rb, line 17
def initialize(type = nil, schema)
  self.merge!(schema)
  self.processed = { type: nil, format: nil, **schema.except(:type, :range, :enum!, *SELF_MAPPING.values.flatten) }
  self.type = type || self[:type]
end

Public Instance Methods

additional_properties() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 57
def additional_properties
  return if processed[:type] != 'object'
  default = Config.additional_properties_default_value_of_type_object
  return { additionalProperties: default } if _addProp.nil? && !default.nil?

  value = _addProp.in?([true, false]) ? _addProp : SchemaObj.new(_addProp, { }).process
  { additionalProperties: value }
end
desc() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 28
def desc
  return unless (result = @bang_enum.present? ? auto_generate_desc : _desc)
  { description: result }
end
enum() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 66
def enum
  self._enum = str_range_to_a(_enum) if _enum.is_a?(Range)
  # Support this writing for auto generating desc from enum.
  #   enum!: {
  #     'all_desc': :all,
  #     'one_desc': :one
  # }
  if (@bang_enum = self[:enum!])
    self._enum ||= @bang_enum.is_a?(Hash) ? @bang_enum.values : @bang_enum
  end
  { enum: _enum }
end
format() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 102
def format
  { format: self[:format] || self[:is_a] } unless processed[:format]
end
length() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 79
def length
  return unless _length
  self._length = str_range_to_a(_length) if _length.is_a?(Range)

  if _length.is_a?(Array)
    min, max = [ _length.first&.to_i, _length.last&.to_i ]
  else
    min, max = _length[/ge_(.*)/, 1]&.to_i, _length[/le_(.*)/, 1]&.to_i
  end

  processed[:type] == 'array' ? { minItems: min, maxItems: max } : { minLength: min, maxLength: max }
end
other() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 106
def other
  {
      pattern:  _pattern.is_a?(String) ? _pattern : _pattern&.inspect&.delete('/'),
      example:  ExampleObj.new(self[:example]).process,
      examples: ExampleObj.new(self[:examples], self[:exp_params], multiple: true).process
  }
end
process() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 23
def process
  processed.merge!(recg_schema_type)
  reducing(additional_properties, enum, length, range, format, other, desc)
end
range() click to toggle source
# File lib/oas_objs/schema_obj.rb, line 92
def range
  (range = self[:range]) or return
  {
               minimum: range[:gt] || range[:ge],
      exclusiveMinimum: range[:gt].present? || nil,
               maximum: range[:lt] || range[:le],
      exclusiveMaximum: range[:lt].present? || nil
  }
end
recg_schema_type(t = self.type) click to toggle source
# File lib/oas_objs/schema_obj.rb, line 33
def recg_schema_type(t = self.type)
  t = t.class.in?([Hash, Array, Symbol]) ? t : t.to_s.downcase
  if t.is_a? Hash
    hash_type(t)
  elsif t.is_a? Array
    array_type(t)
  elsif t.is_a? Symbol
    RefObj.new(:schema, t).process
  elsif t.in? %w[ float double int32 int64 ]
    { type: t['int'] ? 'integer' : 'number', format: t }
  elsif t.in? %w[ binary base64 uri ]
    { type: 'string', format: t }
  elsif t == 'file' # TODO
    { type: 'string', format: Config.file_format }
  elsif t == 'datetime'
    { type: 'string', format: 'date-time' }
  elsif t[/{=>.*}/]
    self[:values_type] = t[3..-2]
    { type: 'object' }
  else # other string
    { type: t }
  end
end