Class: OpenApi::Schema

Inherits:
Object
  • Object
show all
Includes:
EquatableAsContent
Defined in:
lib/open_api/schema.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EquatableAsContent

#==

Constructor Details

#initialize(nullable: false, discriminator: nil, read_only: false, write_only: false, xml: nil, external_docs: nil, example: nil, deprecated: false, **other_fields_hash) ⇒ Schema

Returns a new instance of Schema.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/open_api/schema.rb', line 7

def initialize(nullable: false, discriminator: nil, read_only: false, write_only: false, xml: nil, external_docs: nil, example: nil, deprecated: false, **other_fields_hash)
  self.nullable = nullable
  self.discriminator = discriminator
  self.read_only = read_only
  self.write_only = write_only
  self.xml = xml
  self.external_docs = external_docs
  self.example = example
  self.deprecated = deprecated
  self.other_fields_hash = other_fields_hash

  other_fields_hash.keys.each { |name| new_field(name) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/open_api/schema.rb', line 21

def method_missing(mid, *args)
  len = args.length
  if mname = mid[/.*(?==\z)/m]
    if len != 1
      raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
    end
    new_field(mname)
    other_fields_hash[mname.to_sym] = args[0]
  elsif len == 0
    if other_fields_hash.key?(mname.to_s)
      new_field(mname)
      other_fields_hash[mname]
    end
  else
    begin
      super
    rescue NoMethodError => err
      err.backtrace.shift
      raise
    end
  end
end

Instance Attribute Details

#deprecatedObject

Returns the value of attribute deprecated.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def deprecated
  @deprecated
end

#discriminatorObject

Returns the value of attribute discriminator.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def discriminator
  @discriminator
end

#exampleObject

Returns the value of attribute example.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def example
  @example
end

#external_docsObject

Returns the value of attribute external_docs.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def external_docs
  @external_docs
end

#nullableObject

Returns the value of attribute nullable.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def nullable
  @nullable
end

#read_onlyObject

Returns the value of attribute read_only.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def read_only
  @read_only
end

#write_onlyObject

Returns the value of attribute write_only.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def write_only
  @write_only
end

#xmlObject

Returns the value of attribute xml.



5
6
7
# File 'lib/open_api/schema.rb', line 5

def xml
  @xml
end

Class Method Details

.load(hash) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/open_api/schema.rb', line 76

def self.load(hash)
  other_fields_hash = hash.reject { |key|
    key.to_sym.in?([:nullable, :discriminator, :readOnly, :writeOnly, :xml, :externalDocs, :example, :deprecated])
  }.map { |k, v|
    loaded_value =
      case k.to_sym
      when :items then Reference.load(v)
      when :properties then v.map { |k, v| [k, Reference.new(v) || Schema.new(v)] }.to_h
      else
        v
      end

    [k, loaded_value]
  }.to_h

  new(
    nullable: hash["nullable"].nil? ? false : hash["nullable"],
    discriminator: hash["discriminator"],
    read_only: hash["readOnly"].nil? ? false : hash["readOnly"],
    write_only: hash["writeOnly"].nil? ? false : hash["writeOnly"],
    xml: Xml.load(hash["xml"]),
    external_docs: ExternalDocumentation.load(hash["externalDocs"]),
    example: Example.load(hash["example"]),
    deprecated: hash["deprecated"].nil? ? false : hash["deprecated"],
    **other_fields_hash.symbolize_keys,
  )
end

Instance Method Details

#new_field(name) ⇒ Object



44
45
46
47
48
# File 'lib/open_api/schema.rb', line 44

def new_field(name)
  name = name.to_sym
  define_singleton_method(name) { other_fields_hash[name] }
  define_singleton_method("#{name}=") { |value| other_fields_hash[name] = value }
end

#serializable_hashObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/open_api/schema.rb', line 50

def serializable_hash
  converted_other_fields_hash = other_fields_hash.map { |k, v|
    value =
      case k.to_sym
      when :items then v.serializable_hash
      when :properties then v.map { |k, v| [k.to_s, v.serializable_hash] }.to_h
      else
        v.is_a?(Array) ? v.map { |_v| _v.try(:serializable_hash) || _v&.to_s } : v&.to_s
      end
    [k.to_s, value]
  }.to_h

  {
    "nullable" => nullable == false ? nil : nullable,
    "discriminator" => discriminator&.serializable_hash,
    "readOnly" => read_only == false ? nil : read_only,
    "writeOnly" => write_only == false ? nil : write_only,
    "xml" => xml&.serializable_hash,
    "externalDocs" => external_docs&.serializable_hash,
    "example" => example,
    "deprecated" => deprecated == false ? nil : deprecated,
  }
    .merge(converted_other_fields_hash)
    .compact
end