Class: GrapeOpenapi3::Builders::ParameterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_openapi3/builders/parameter_builder.rb

Overview

Converts a route’s params hash into OpenAPI 3.0 ‘parameters` + `requestBody`.

Param location is determined (per top-level param) in this priority order:

1. Explicit: documentation: { param_type: "path|query|header|body|formData" }
2. Name matches a {segment} in the path → path
3. type: File → formData (multipart)
4. HTTP method GET/DELETE → query
5. HTTP method POST/PUT/PATCH → body (requestBody JSON)

Nested params:

Grape flattens nested params into bracketed keys, e.g.
  "address"        => { type: "Hash" }
  "address[street]"=> { type: "String", required: true }
  "tags"           => { type: "Array" }
  "tags[label]"    => { type: "String" }
These are reassembled into nested object / array-of-object schemas in the
request body. Location is decided by the TOP-LEVEL key; children inherit it.

Constant Summary collapse

BODY_METHODS =
%w[post put patch].freeze
PATH_SEGMENT =
/\{(\w+)\}/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route_data) ⇒ ParameterBuilder

Returns a new instance of ParameterBuilder.



30
31
32
33
34
35
# File 'lib/grape_openapi3/builders/parameter_builder.rb', line 30

def initialize(route_data)
  @route      = route_data
  @path       = route_data[:path]
  @method     = route_data[:http_method]
  @path_names = @path.scan(PATH_SEGMENT).flatten.to_set
end

Class Method Details

.call(route_data) ⇒ Object



26
27
28
# File 'lib/grape_openapi3/builders/parameter_builder.rb', line 26

def self.call(route_data)
  new(route_data).call
end

Instance Method Details

#callObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/grape_openapi3/builders/parameter_builder.rb', line 37

def call
  inline   = []  # path / query / header params (flat)
  json     = {}  # body params (full bracket keys) → application/json
  formdata = {}  # formData params → multipart/form-data

  grouped_by_top.each do |top, info|
    loc = location(top, info[:root] || {})

    case loc
    when :path, :query, :header
      inline << build_parameter(top, info[:root] || {}, loc)
    when :form
      formdata.merge!(info[:entries])
    when :body
      json.merge!(info[:entries])
    end
  end

  {
    parameters:   inline,
    request_body: build_request_body(json, formdata),
  }
end