Class: GrapeOpenapi3::Builders::ParameterBuilder
- Inherits:
-
Object
- Object
- GrapeOpenapi3::Builders::ParameterBuilder
- 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 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)
Params marked as formData OR containing a File field produce a multipart/form-data requestBody. All other body params produce JSON.
Constant Summary collapse
- BODY_METHODS =
%w[post put patch].freeze
- PATH_SEGMENT =
/\{(\w+)\}/
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(route_data) ⇒ ParameterBuilder
constructor
A new instance of ParameterBuilder.
Constructor Details
#initialize(route_data) ⇒ ParameterBuilder
Returns a new instance of ParameterBuilder.
24 25 26 27 28 29 |
# File 'lib/grape_openapi3/builders/parameter_builder.rb', line 24 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
20 21 22 |
# File 'lib/grape_openapi3/builders/parameter_builder.rb', line 20 def self.call(route_data) new(route_data).call end |
Instance Method Details
#call ⇒ Object
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 |
# File 'lib/grape_openapi3/builders/parameter_builder.rb', line 31 def call inline = [] # path / query / header params json = {} # body params → application/json requestBody formdata = {} # formData params → multipart/form-data requestBody @route[:params].each do |name, definition| name_s = name.to_s.sub(/\[\]$/, "") # strip Grape's array brackets defn = definition.is_a?(Hash) ? definition : {} loc = location(name_s, defn) case loc when :path, :query, :header inline << build_parameter(name_s, defn, loc) when :form formdata[name_s] = defn when :body json[name_s] = defn end end { parameters: inline, request_body: build_request_body(json, formdata), } end |