Class: OpenApi::DSL::SchemaObj

Inherits:
Hash
  • Object
show all
Includes:
SchemaObjHelpers, Helpers
Defined in:
lib/oas_objs/schema_obj.rb

Overview

Constant Summary collapse

SELF_MAPPING =
{
    _enum:    i[ enum in  values  allowable_values ],
    _length:  i[ length   lth     size             ],
    _pattern: i[ pattern  regexp                   ],
    _desc:    i[ desc     description  d           ],
    _addProp: i[ additional_properties add_prop values_type ],
}.each do |key, aliases|
  define_method(key)       { self[key] ||= self.values_at(*aliases).compact.first }
  define_method("#{key}=") { |value| self[key] = value }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#fusion, #reducing, #truly_present?, #value_present

Methods included from SchemaObjHelpers

#array_type, #auto_generate_desc, #hash_type, #obj_type, #str_range_to_a

Constructor Details

#initialize(type = nil, schema) ⇒ SchemaObj

Returns a new instance of SchemaObj.



17
18
19
20
21
# 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

Instance Attribute Details

#processedObject

Returns the value of attribute processed.



15
16
17
# File 'lib/oas_objs/schema_obj.rb', line 15

def processed
  @processed
end

#typeObject

Returns the value of attribute type.



15
16
17
# File 'lib/oas_objs/schema_obj.rb', line 15

def type
  @type
end

Instance Method Details

#additional_propertiesObject



55
56
57
58
59
60
61
62
# File 'lib/oas_objs/schema_obj.rb', line 55

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

#descObject



28
29
30
31
# File 'lib/oas_objs/schema_obj.rb', line 28

def desc
  return unless (result = @bang_enum.present? ? auto_generate_desc : _desc)
  { description: result }
end

#enumObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/oas_objs/schema_obj.rb', line 64

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

#formatObject



100
101
102
# File 'lib/oas_objs/schema_obj.rb', line 100

def format
  { format: self[:format] || self[:is_a] } unless processed[:format]
end

#lengthObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oas_objs/schema_obj.rb', line 77

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

#otherObject



104
105
106
107
108
109
110
# File 'lib/oas_objs/schema_obj.rb', line 104

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

#processObject



23
24
25
26
# 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

#rangeObject



90
91
92
93
94
95
96
97
98
# File 'lib/oas_objs/schema_obj.rb', line 90

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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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
  case t
  when Hash then hash_type(t)
  when Array then array_type(t)
  when Symbol then RefObj.new(:schema, t).process
  when *%w[ float double int32 int64 ]
    { type: t['int'] ? 'integer' : 'number', format: t }
  when *%w[ date binary base64 uri ]
    { type: 'string', format: t }
  when 'datetime'
    { type: 'string', format: 'date-time' }
  when 'file' # TODO
    { type: 'string', format: Config.file_format }
  when /{=>.*}/
    self[:values_type] = t[3..-2]
    { type: 'object' }
  else # other string
    { type: t }
  end
end