Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple uploaded files examples #117

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def adjust_params(value)
value[key] = v.original_filename
elsif v.is_a?(Hash)
adjust_params(v)
elsif v.is_a?(Array)
result = v.map do |item|
item.is_a?(ActionDispatch::Http::UploadedFile) ? item.original_filename : item
end
value[key] = result
end
end
end
Expand Down
35 changes: 28 additions & 7 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ class TablesUpdateTest < ActionDispatch::IntegrationTest
openapi!

test 'returns a table' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: {
nested: { image: image, caption: 'Some caption' },
}
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { name: 'test' }
assert_response 200
end
end
Expand Down Expand Up @@ -141,6 +135,33 @@ class ImageTest < ActionDispatch::IntegrationTest
get '/images'
assert_response 200
end

test 'returns a image payload with upload' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
post '/images/upload', params: { image: image }
assert_response 200
end

test 'returns a image payload with upload nested' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
post '/images/upload_nested', params: { nested_image: { image: image, caption: 'Some caption' } }
assert_response 200
end

test 'returns a image payload with upload multiple' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
post '/images/upload_multiple', params: { images: [image, image] }
assert_response 200
end
end

class ExtraRoutesTest < ActionDispatch::IntegrationTest
Expand Down
24 changes: 21 additions & 3 deletions spec/rails/app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class ImagesController < ApplicationController
def show
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack('m').first
send_data png, type: 'image/png', disposition: 'inline'
send_image
end

def index
Expand All @@ -14,4 +12,24 @@ def index
]
render json: list
end

def upload
send_image
end

def upload_nested
send_image
end

def upload_multiple
send_image
end

private

def send_image
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack('m').first
send_data png, type: 'image/png', disposition: 'inline'
end
end
8 changes: 7 additions & 1 deletion spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

defaults format: 'json' do
resources :tables, only: [:index, :show, :create, :update, :destroy]
resources :images, only: [:index, :show]
resources :images, only: [:index, :show] do
collection do
post 'upload'
post 'upload_nested'
post 'upload_multiple'
end
end
resources :users, only: [:show, :create]

get '/test_block' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['A TEST']] }
Expand Down
173 changes: 152 additions & 21 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -455,36 +455,20 @@
],
"requestBody": {
"content": {
"multipart/form-data": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"properties": {
"nested": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
},
"caption": {
"type": "string"
}
},
"required": [
"image",
"caption"
]
"name": {
"type": "string"
}
},
"required": [
"nested"
"name"
]
},
"example": {
"nested": {
"image": "test.png",
"caption": "Some caption"
}
"name": "test"
}
}
}
Expand Down Expand Up @@ -797,6 +781,153 @@
}
}
}
},
"/images/upload_multiple": {
"post": {
"summary": "upload_multiple",
"tags": [
"Image"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"images": {
"type": "array",
"items": {
"type": "string",
"format": "binary"
}
}
},
"required": [
"images"
]
},
"example": {
"images": [
"test.png",
"test.png"
]
}
}
}
},
"responses": {
"200": {
"description": "returns a image payload with upload multiple",
"content": {
"image/png": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
},
"/images/upload_nested": {
"post": {
"summary": "upload_nested",
"tags": [
"Image"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"nested_image": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
},
"caption": {
"type": "string"
}
},
"required": [
"image",
"caption"
]
}
},
"required": [
"nested_image"
]
},
"example": {
"nested_image": {
"image": "test.png",
"caption": "Some caption"
}
}
}
}
},
"responses": {
"200": {
"description": "returns a image payload with upload nested",
"content": {
"image/png": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
},
"/images/upload": {
"post": {
"summary": "upload",
"tags": [
"Image"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
}
},
"required": [
"image"
]
},
"example": {
"image": "test.png"
}
}
}
},
"responses": {
"200": {
"description": "returns a image payload with upload",
"content": {
"image/png": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
}
}
}
Loading