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:
Jay Danielian
2019-07-17 20:07:30 -04:00
parent c820bb75e0
commit aa133b90fc
6 changed files with 62 additions and 11 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -18,5 +18,6 @@ Gem::Specification.new do |s|
s.add_dependency 'activesupport', '>= 3.1', '< 6.0'
s.add_dependency 'json-schema', '~> 2.2'
s.add_dependency 'railties', '>= 3.1', '< 6.0'
s.add_dependency 'hashie'
s.add_development_dependency 'guard-rspec'
end