Module: GrapeOAS::Introspectors::EntityIntrospectorSupport::NestingMerger

Defined in:
lib/grape_oas/introspectors/entity_introspector_support/nesting_merger.rb

Overview

Merges duplicate-key nesting exposure branches into a single schema, preserving properties from all branches.

Constant Summary collapse

MAX_MERGE_DEPTH =

Grape nesting rarely exceeds 3-4 levels

10

Class Method Summary collapse

Class Method Details

.merge(accum, current, depth = 0) ⇒ ApiModel::Schema

Returns merged schema.

Parameters:

  • accum (ApiModel::Schema, nil)

    accumulated schema from previous branches

  • current (ApiModel::Schema)

    schema from the current branch

  • depth (Integer) (defaults to: 0)

    current recursion depth (guarded by MAX_MERGE_DEPTH)

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/grape_oas/introspectors/entity_introspector_support/nesting_merger.rb', line 16

def merge(accum, current, depth = 0)
  return current unless accum
  return accum if current.equal?(accum)

  # Unwrap array schemas to merge their items, then re-wrap
  if array_of_objects?(accum) && array_of_objects?(current)
    merged_items = merge(accum.items, current.items, depth + 1)
    merged_array = ApiModel::Schema.new(type: Constants::SchemaTypes::ARRAY, items: merged_items)
    (merged_array, accum)
    (merged_array, current)
    return merged_array
  end

  return accum unless current&.type == Constants::SchemaTypes::OBJECT
  return current unless accum.type == Constants::SchemaTypes::OBJECT

  merge_object_schemas(accum, current, depth)
end