Class: GrapeOpenapi3::TypeMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_openapi3/type_mapper.rb

Overview

Maps Ruby/Grape types to OpenAPI 3.0 schema hashes. Handles Class objects, Array types like [String], and boolean markers.

Constant Summary collapse

BOOLEAN_CLASS_NAMES =
%w[Boolean Grape::API::Boolean].freeze
PRIMITIVES =
{
  "String"     => { "type" => "string" },
  "Integer"    => { "type" => "integer" },
  "Float"      => { "type" => "number", "format" => "float" },
  "BigDecimal" => { "type" => "number", "format" => "double" },
  "Numeric"    => { "type" => "number" },
  "Date"       => { "type" => "string", "format" => "date" },
  "DateTime"   => { "type" => "string", "format" => "date-time" },
  "Time"       => { "type" => "string", "format" => "date-time" },
  "TrueClass"  => { "type" => "boolean" },
  "FalseClass" => { "type" => "boolean" },
  "File"       => { "type" => "string", "format" => "binary" },
  "Hash"       => { "type" => "object" },
  "Array"      => { "type" => "array", "items" => {} },
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ TypeMapper

Returns a new instance of TypeMapper.



29
30
31
# File 'lib/grape_openapi3/type_mapper.rb', line 29

def initialize(type)
  @type = type
end

Class Method Details

.call(type) ⇒ Object



25
26
27
# File 'lib/grape_openapi3/type_mapper.rb', line 25

def self.call(type)
  new(type).call
end

Instance Method Details

#callObject



33
34
35
36
37
38
39
40
# File 'lib/grape_openapi3/type_mapper.rb', line 33

def call
  return { "type" => "boolean" } if boolean?
  return array_schema               if @type.is_a?(Array)
  return string_array_schema        if string_array?

  name = @type.is_a?(Class) ? @type.name.to_s : @type.to_s
  (PRIMITIVES[name] || { "type" => "string" }).dup
end