179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/rubst_api/model.rb', line 179
def for(type, options = {})
schema =
if type.is_a?(Array)
non_nil = type.reject { |item| item == NilClass }
non_nil.length == 1 ? self.for(non_nil.first) : { anyOf: non_nil.map { |item| self.for(item) } }
elsif type.is_a?(Hash) && type.key?(:array) then { type: "array", items: self.for(type[:array]) }
elsif type.is_a?(Class) && type <= Model then { "$ref": "#/components/schemas/#{type.name.split("::").last}" }
else
{ String => { type: "string" }, Integer => { type: "integer" }, Float => { type: "number" },
Numeric => { type: "number" }, TrueClass => { type: "boolean" }, FalseClass => { type: "boolean" },
Array => { type: "array", items: {} }, Hash => { type: "object" },
Date => { type: "string", format: "date" }, Time => { type: "string", format: "date-time" } }[type] || {}
end
schema.merge(options.slice(:title, :description, :minimum, :maximum, :min_length, :max_length, :pattern, :enum).compact)
end
|