Class: ReeSwagger::BuildSerializerSchema

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

Instance Method Summary collapse

Instance Method Details

#call(mapper, fields_filters = nil) ⇒ Object



11
12
13
14
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
# File 'lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_serializer_schema.rb', line 11

def call(mapper, fields_filters = nil)
  if mapper.type
    return get_serializer_definition(mapper.type, method(:call).to_proc)
  end

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

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

    swagger_field = {}

    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

  {
    type: 'object',
    properties: properties
  }
end