Class: OpenAPIArrangement::Schema::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi/arrangement/schema.rb

Overview

Schema, reference to it, name, and what it refers to.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, name, schema) ⇒ Info

Returns a new instance of Info.



57
58
59
60
61
62
63
# File 'lib/openapi/arrangement/schema.rb', line 57

def initialize(ref, name, schema)
  @ref = ref
  @name = name
  @schema = schema
  @direct_refs = {}
  self.class.gather_refs(@direct_refs, schema)
end

Instance Attribute Details

#direct_refsObject (readonly)

Returns the value of attribute direct_refs.



55
56
57
# File 'lib/openapi/arrangement/schema.rb', line 55

def direct_refs
  @direct_refs
end

#nameObject (readonly)

Returns the value of attribute name.



55
56
57
# File 'lib/openapi/arrangement/schema.rb', line 55

def name
  @name
end

#refObject (readonly)

Returns the value of attribute ref.



55
56
57
# File 'lib/openapi/arrangement/schema.rb', line 55

def ref
  @ref
end

#schemaObject (readonly)

Returns the value of attribute schema.



55
56
57
# File 'lib/openapi/arrangement/schema.rb', line 55

def schema
  @schema
end

#unseen_refsObject (readonly)

Returns the value of attribute unseen_refs.



55
56
57
# File 'lib/openapi/arrangement/schema.rb', line 55

def unseen_refs
  @unseen_refs
end

Class Method Details

.gather_array_refs(refs, items, required) ⇒ Object

Adds all refs found in the array to refs with given required state.



76
77
78
79
80
81
82
# File 'lib/openapi/arrangement/schema.rb', line 76

def self.gather_array_refs(refs, items, required)
  items.each do |s|
    r = s['$ref']
    next if r.nil?
    refs[r] = required || refs.fetch(r, false)
  end
end

.gather_refs(refs, schema) ⇒ Object

For any key '$ref' adds to refs whether referred type is required. Requires that there are no in-lined schemas, openapi-addschemas has been run.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/openapi/arrangement/schema.rb', line 86

def self.gather_refs(refs, schema)
  # This implies types mixed together according to examples. Needs mixed type.
  # AND. Also, mixing may fail. Adds a new schema, do in openapi-oftypes.
  items = schema['allOf']
  return gather_array_refs(refs, items, true) unless items.nil?
  # As long as one schema is fulfilled, it is ok. OR, first that fits.
  items = schema['anyOf'] if items.nil?
  # oneOf implies selection between different types. No multiple matches. XOR.
  # Needs to ensure that later types do not match.
  # Should check if there is enough difference to ensure single match.
  # Use separate program run after addschemas to create allOf mixed schema
  # and verify the others can be dealt with.
  items = schema['oneOf'] if items.nil?
  return gather_array_refs(refs, items, false) unless items.nil?
  # Defaults below handle it if "type" is not "object".
  reqs = schema.fetch('required', [])
  schema.fetch('properties', {}).each do |name, spec|
    r = spec['$ref']
    next if r.nil?
    refs[r] = reqs.include?(name) || refs.fetch(r, false)
  end
  schema.fetch('patternProperties', {}).each_value do |spec|
    r = spec['$ref']
    next if r.nil?
    refs[r] = refs.fetch(r, false)
  end
  spec = schema['additionalProperties']
  return unless spec.is_a?(Hash)
  r = spec['$ref']
  refs[r] = refs.fetch(r, false) unless r.nil?
end

Instance Method Details

#mark_as_seen(seen) ⇒ Object

Sets references that require forward declaration to be used.



66
67
68
# File 'lib/openapi/arrangement/schema.rb', line 66

def mark_as_seen(seen)
  @unseen_refs = Set.new(@direct_refs.keys) - seen
end

#to_sObject



70
71
72
73
# File 'lib/openapi/arrangement/schema.rb', line 70

def to_s
  v = @direct_refs.keys.sort.map { |k| "#{k}:#{@direct_refs[k] ? 'req' : 'opt'}" }
  "#{@ref}: #{v.join(' ')}"
end