Module: OpenapiRuby::Middleware::Coercion

Defined in:
lib/openapi_ruby/middleware/coercion.rb

Class Method Summary collapse

Class Method Details

.coerce_boolean(value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/openapi_ruby/middleware/coercion.rb', line 39

def coerce_boolean(value)
  case value.downcase
  when "true", "1", "yes"
    true
  when "false", "0", "no"
    false
  else
    value
  end
end

.coerce_params(params, parameters) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/openapi_ruby/middleware/coercion.rb', line 26

def coerce_params(params, parameters)
  return params unless params.is_a?(Hash) && parameters.is_a?(Array)

  params.each_with_object({}) do |(key, value), result|
    param_spec = parameters.find { |p| p["name"] == key.to_s }
    result[key] = if param_spec && param_spec["schema"]
      coerce_value(value, param_spec["schema"])
    else
      value
    end
  end
end

.coerce_value(value, schema) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/openapi_ruby/middleware/coercion.rb', line 8

def coerce_value(value, schema)
  return value unless value.is_a?(String) && schema.is_a?(Hash)

  type = schema["type"]
  case type
  when "integer"
    Integer(value)
  when "number"
    Float(value)
  when "boolean"
    coerce_boolean(value)
  else
    value
  end
rescue ArgumentError, TypeError
  value
end