Class: GrapeOAS::ApiModelBuilders::RequestParamsSupport::NestedParamsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_oas/api_model_builders/request_params_support/nested_params_builder.rb

Overview

Reconstructs nested parameter structures from Grape’s flat bracket notation. Grape exposes nested params as flat keys like “address”, “address”. This class converts them back to proper nested schemas.

Constant Summary collapse

BRACKET_PATTERN =
/\[([^\]]+)\]/
MAX_NESTING_DEPTH =
10

Instance Method Summary collapse

Constructor Details

#initialize(schema_builder:) ⇒ NestedParamsBuilder

Returns a new instance of NestedParamsBuilder.



13
14
15
# File 'lib/grape_oas/api_model_builders/request_params_support/nested_params_builder.rb', line 13

def initialize(schema_builder:)
  @schema_builder = schema_builder
end

Instance Method Details

#build(flat_params, path_params: []) ⇒ ApiModel::Schema

Builds a nested schema from flat bracket-notation params.

Parameters:

  • flat_params (Hash)

    the flat params from Grape route (name => spec)

  • path_params (Array<String>) (defaults to: [])

    names of path parameters to exclude

Returns:



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
# File 'lib/grape_oas/api_model_builders/request_params_support/nested_params_builder.rb', line 22

def build(flat_params, path_params: [])
  schema = ApiModel::Schema.new(type: Constants::SchemaTypes::OBJECT)

  # Separate top-level params from nested bracket params
  top_level, nested = partition_params(flat_params)

  # Group nested params by their root key
  nested_groups = group_nested_params(nested)

  top_level.each do |name, spec|
    next if path_params.include?(name)
    next if ParamLocationResolver.explicit_non_body_param?(spec)
    next if ParamLocationResolver.hidden_parameter?(spec)

    child_schema = @schema_builder.build(spec)

    # Check if this param has nested children
    if nested_groups.key?(name)
      nested_children = nested_groups[name]
      child_schema = build_nested_children(spec, nested_children, depth: 0)
    end

    required = spec[:required] || false
    schema.add_property(name, child_schema, required: required)
  end

  schema
end