Class: ReeSwagger::BuildRequestBodySchema

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_request_body_schema.rb

Instance Method Summary collapse

Instance Method Details

#call(mapper, path_params = [], fields_filters = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_request_body_schema.rb', line 15

def call(mapper, path_params = [], fields_filters = nil)
  if mapper.type
    return get_caster_definition(mapper.type, method(:call).to_proc)
  end

  required_fields = []

  properties = mapper.fields.each_with_object({}) do |(_name, field), acc|
    next unless fields_filters.nil? || fields_filters.all? { _1.allow?(field.name) }

    next if path_params.include?(field.name)

    if field.type == mapper
      acc[field.name] = {}
      next
    end

    swagger_field = {}

    required_fields << field.name.to_s if !field.optional
    field_mapper = field.type

    nested_fields_filters = fields_filters&.filter_map { _1.filter_for(field.name) }

    if field.fields_filter
      nested_fields_filters = if nested_fields_filters
        nested_fields_filters + [field.fields_filter]
      else
        [field.fields_filter]
      end
    end

    swagger_type = call(field_mapper, [], nested_fields_filters)

    swagger_field.merge!(swagger_type) if swagger_type

    description = field.doc
    swagger_field[:description] = description if description

    swagger_field[:nullable] = true if field.null

    acc[field.name] = swagger_field
  end

  return if properties.empty?

  obj = {
    type: 'object',
    properties: properties,
  }
  obj[:required] = required_fields if required_fields.size != 0
  obj
end