Class: Lacerda::Conversion::DataStructure::Member::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/lacerda/conversion/data_structure/member/type.rb

Instance Method Summary collapse

Constructor Details

#initialize(type_definition, is_required, scope) ⇒ Type

Returns a new instance of Type.



6
7
8
9
10
11
# File 'lib/lacerda/conversion/data_structure/member/type.rb', line 6

def initialize(type_definition, is_required, scope)
  @type_definition = type_definition
  @scope = scope
  @type_name = type_definition['element']
  @is_required = is_required
end

Instance Method Details

#array_typeObject

bui As there are specied types in the array, ‘nil` should not be a valid value and therefore required should be true.



33
34
35
36
37
38
39
40
# File 'lib/lacerda/conversion/data_structure/member/type.rb', line 33

def array_type
  return unless array?
  if nested_types.size == 1 && PRIMITIVES.include?(nested_types.first)
    primitive(nested_types.first, true) 
  else
    oneOf(nested_types, true)
  end
end

#object?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/lacerda/conversion/data_structure/member/type.rb', line 13

def object?
  @type_name == 'object'
end

#to_hashObject

A type is transformed to json schema either as a primitive:

{ "type" => ["string"] }
{ "type" => ["string", "null"] }

Or as a oneOf. $ref are always within a oneOf

{"oneOf"=>[{"$ref"=>"#/definitions/tag"}, {"type"=>"null"}]}


22
23
24
25
26
27
28
# File 'lib/lacerda/conversion/data_structure/member/type.rb', line 22

def to_hash
  if PRIMITIVES.include?(@type_name)
    primitive(@type_name, required?)
  else
    oneOf([@type_name], required?)
  end
end