Module: Modern::DocGenerator::OpenAPI3::Schemas

Includes:
SchemaDefaultTypes
Included in:
Modern::DocGenerator::OpenAPI3
Defined in:
lib/modern/doc_generator/open_api3/schemas.rb,
lib/modern/doc_generator/open_api3/security_schemes.rb

Instance Method Summary collapse

Methods included from SchemaDefaultTypes

#_register_default_types!

Instance Method Details

#_build_object_from_schema(ret, name_to_class, dt_schema) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 75

def _build_object_from_schema(ret, name_to_class, dt_schema)
  {
    type: "object",
    properties: dt_schema.map do |k, v|
      [k, _build_schema_value(ret, name_to_class, v)]
    end.to_h
  }
end

#_build_schema_value(ret, name_to_class, entry) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 88

def _build_schema_value(ret, name_to_class, entry)
  registered_type = @type_registry[entry]

  if !registered_type.nil?
    registered_type
  elsif entry.is_a?(Class) && entry < Dry::Struct
    _build_struct(ret, name_to_class, entry)
    _struct_ref(entry)
  elsif entry.is_a?(Dry::Types::Array::Member) && entry.options.key?(:member)
    _build_schema_value(ret, name_to_class, entry.options[:member])
  elsif entry.is_a?(Dry::Types::Sum)
    if entry.left.type.primitive == NilClass
      # it's a nullable field
      _build_schema_value(ret, name_to_class, entry.right).merge(nullable: true)
    elsif entry.right.type.primitive == NilClass
      # it's a backwards nullable field
      _build_schema_value(ret, name_to_class, entry.left).merge(nullable: true)
    else
      {
        anyOf: _flatten_any_of(
          [
            _build_schema_value(ret, name_to_class, entry.left),
            _build_schema_value(ret, name_to_class, entry.right)
          ]
        )
      }
    end
  elsif entry.is_a?(Dry::Types::Constrained)
    # TODO: dig deeper into the actual behavior of Constrained (dry-logic)
    #       This is probably a can of worms. More:
    #       http://dry-rb.org/gems/dry-types/constraints/

    _build_schema_value(ret, name_to_class, entry.type)
  elsif entry.is_a?(Dry::Types::Default) || entry.is_a?(Dry::Struct::Constructor) || entry.is_a?(Dry::Types::Constructor)
    # this just unwraps the underlying value
    _build_schema_value(ret, name_to_class, entry.type)
  elsif entry.is_a?(Dry::Types::Enum)
    _build_schema_value(ret, name_to_class, entry.type).merge(enum: entry.values)
  elsif entry.is_a?(Dry::Types::Definition)
    primitive = entry.primitive

    if primitive < Dry::Struct
      # TODO: make sure I'm understanding this correctly
      #       It feels weird to have to oneOf a $ref, but I can't figure out a
      #       syntax that doesn't require it.
      _build_struct(ret, name_to_class, primitive)

      {
        oneOf: [
          _struct_ref(primitive)
        ]
      }
    elsif primitive <= Hash
      _build_object_from_schema(ret, name_to_class, entry.member_types)
    elsif primitive <= Array
      {
        type: "array",
        items: _build_schema_value(ret, name_to_class, entry.member)
      }
    else
      raise "unrecognized primitive definition '#{primitive.name}'; probably needs a literal."
    end
  else
    raise "Unrecognized schema class: #{entry.class.name}: #{entry.inspect}"
  end
end

#_build_struct(ret, name_to_class, structclass) ⇒ Object



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

def _build_struct(ret, name_to_class, structclass)
  # TODO: allow overriding the name of the struct in #/components/schemas
  #       This is actually trickier than it looks, because we need to also
  #       make it referenceable in responses/contents. It probably means an
  #       indirect mapping of classes to names and back again.

  raise "not actually a Dry::Struct class" \
    unless structclass.ancestors.include?(Dry::Struct)

  name = structclass.name.split("::").last

  if name_to_class[name] == structclass
    name
  else
    if !name_to_class[name].nil?
      raise "Duplicate schema name: '#{name}'. Only one class, regardless " \
            "of namespace, can be called this."
    end

    ret[name] = _build_object_from_schema(ret, name_to_class, structclass.schema)
  end

  name # necessary for recursive calls in _build_schema_value
end

#_flatten_any_of(typehash_array) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 155

def _flatten_any_of(typehash_array)
  # This is hacky, but it removes the incidence of something like this:
  #
  # :exsub => {
  #   :anyOf => [
  #       [0] {
  #           :oneOf => [
  #               [0] {
  #                   :$ref => "#/components/schemas/ExclusiveSubA"
  #               }
  #           ]
  #       },
  #       [1] {
  #           :oneOf => [
  #               [0] {
  #                   :$ref => "#/components/schemas/ExclusiveSubB"
  #               }
  #           ]
  #       }
  #   ]
  # }

  typehash_array.map do |typehash|
    if typehash.length == 1 && typehash.first.first == :oneOf
      typehash.first.last
    else
      typehash
    end
  end.flatten
end

#_security_schemes(descriptor) ⇒ Object



7
8
9
10
11
# File 'lib/modern/doc_generator/open_api3/security_schemes.rb', line 7

def _security_schemes(descriptor)
  descriptor.securities_by_name.map do |name, security|
    [name, security.to_openapi3.compact]
  end.to_h
end

#_struct_ref(structclass) ⇒ Object



84
85
86
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 84

def _struct_ref(structclass)
  { "$ref": "#/components/schemas/#{structclass.name.split('::').last}" }
end

#_struct_schemas(descriptor) ⇒ Object

Only Dry::Struct



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 30

def _struct_schemas(descriptor)
  ret = {}
  name_to_class = {}

  schema_entries =
    descriptor.root_schemas.partition do |type_or_structclass|
      type_or_structclass.is_a?(Class)
    end

  schema_entries.first.each do |structclass|
    _build_struct(ret, name_to_class, structclass)
  end

  schema_entries.last.each do |dt|
    _build_schema_value(ret, name_to_class, dt)
  end

  ret
end

#register_literal_type(type, oapi3_value) ⇒ Object



23
24
25
26
27
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 23

def register_literal_type(type, oapi3_value)
  raise "`type` must be a Dry::Types::Type." unless type.is_a?(Dry::Types::Type)

  @type_registry[type] = oapi3_value
end