mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 07:16:40 +00:00
Adds request_body_multipart method which enables schema properties to be written for multipart upload body
Will inspect the provided hash and add the property file_name to the parameters collection so upload and 3.0 output will work properly
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
require 'hashie'
|
||||
|
||||
module Rswag
|
||||
module Specs
|
||||
@@ -75,6 +76,30 @@ module Rswag
|
||||
end
|
||||
end
|
||||
|
||||
def request_body_multipart(schema:, description: nil)
|
||||
content_hash = { 'multipart/form-data' => { schema: schema }}
|
||||
request_body(description: description, content: content_hash)
|
||||
|
||||
schema.extend(Hashie::Extensions::DeepLocate)
|
||||
file_properties = schema.deep_locate -> (_k, v, _obj) { v == :binary }
|
||||
|
||||
hash_locator = []
|
||||
|
||||
file_properties.each do |match|
|
||||
hash_match = schema.deep_locate -> (_k, v, _obj) { v == match }
|
||||
hash_locator.concat(hash_match) unless hash_match.empty?
|
||||
end
|
||||
|
||||
property_hashes = hash_locator.flat_map do |locator|
|
||||
locator.select { |_k,v| file_properties.include?(v) }
|
||||
end
|
||||
|
||||
property_hashes.each do |property_hash|
|
||||
file_name = property_hash.keys.first
|
||||
parameter name: file_name, in: :formData, type: :file, required: true
|
||||
end
|
||||
end
|
||||
|
||||
def parameter(attributes)
|
||||
if attributes[:in] && attributes[:in].to_sym == :path
|
||||
attributes[:required] = true
|
||||
|
||||
@@ -44,7 +44,7 @@ module Rswag
|
||||
value[:requestBody][:content]['application/json'].merge!(schema: schema_param[:schema])
|
||||
end
|
||||
|
||||
value[:parameters].reject! { |p| p[:in] == :body }
|
||||
value[:parameters].reject! { |p| p[:in] == :body || p[:in] == :formData }
|
||||
value[:parameters].each { |p| p.delete(:type) }
|
||||
value[:headers].each { |p| p.delete(:type)} if value[:headers]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user