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
- #_build_object_from_schema(ret, name_to_class, dt_schema) ⇒ Object
- #_build_schema_value(ret, name_to_class, entry) ⇒ Object
- #_build_struct(ret, name_to_class, structclass) ⇒ Object
- #_flatten_any_of(typehash_array) ⇒ Object
- #_security_schemes(descriptor) ⇒ Object
- #_struct_ref(structclass) ⇒ Object
-
#_struct_schemas(descriptor) ⇒ Object
Only Dry::Struct.
- #register_literal_type(type, oapi3_value) ⇒ Object
Methods included from SchemaDefaultTypes
Instance Method Details
#_build_object_from_schema(ret, name_to_class, dt_schema) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 68 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
81 82 83 84 85 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 81 def _build_schema_value(ret, name_to_class, entry) registered_type = @type_registry[entry] if !registered_type.nil? registered_type elsif entry.is_a?(Dry::Types::Sum::Constrained) if entry.left.type.primitive == NilClass # it's a nullable field _build_schema_value(ret, name_to_class, entry.right).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) # this just unwraps the default value _build_schema_value(ret, name_to_class, entry.type) elsif entry.is_a?(Dry::Types::Definition) primitive = entry.primitive if primitive.ancestors.include?(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.ancestors.include?(Hash) _build_object_from_schema(ret, name_to_class, entry.member_types) elsif primitive.ancestors.include?(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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 43 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 138 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
77 78 79 |
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 77 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 |
# File 'lib/modern/doc_generator/open_api3/schemas.rb', line 30 def _struct_schemas(descriptor) ret = {} name_to_class = {} descriptor.root_schemas \ .select { |type_or_structclass| type_or_structclass.is_a?(Class) } \ .each do |structclass| _build_struct(ret, name_to_class, structclass) 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 |