Skip to content

Commit

Permalink
fix multiple images
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yohei committed May 21, 2023
1 parent 27cc49e commit 7dfe7fc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
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
18 changes: 18 additions & 0 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ 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: { image: image }
assert_response 200
end

test 'returns a table with nested' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
Expand All @@ -113,6 +122,15 @@ class TablesUpdateTest < ActionDispatch::IntegrationTest
}
assert_response 200
end

test 'returns a table with array' 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: { images: [image, image] }
assert_response 200
end
end

class TablesDestroyTest < ActionDispatch::IntegrationTest
Expand Down
43 changes: 38 additions & 5 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -474,24 +474,57 @@
"image",
"caption"
]
},
"image": {
"type": "string",
"format": "binary"
},
"images": {
"type": "array",
"items": {
"type": "string",
"format": "binary"
}
},
"nested_image": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
},
"caption": {
"type": "string"
}
},
"required": [
"image",
"caption"
]
}
},
"required": [
"nested"
]
}
},
"example": {
"nested": {
"image": "test.png",
"caption": "Some caption"
},
"image": "test.png",
"images": [
"test.png",
"test.png"
],
"nested_image": {
"image": "test.png",
"caption": "Some caption"
}
}
}
}
},
"responses": {
"200": {
"description": "returns a table",
"description": "returns a table with array",
"content": {
"application/json": {
"schema": {
Expand Down
30 changes: 27 additions & 3 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,39 @@ paths:
required:
- image
- caption
required:
- nested
image:
type: string
format: binary
images:
type: array
items:
type: string
format: binary
nested_image:
type: object
properties:
image:
type: string
format: binary
caption:
type: string
required:
- image
- caption
example:
nested:
image: test.png
caption: Some caption
image: test.png
images:
- test.png
- test.png
nested_image:
image: test.png
caption: Some caption
responses:
'200':
description: returns a table
description: returns a table with array
content:
application/json:
schema:
Expand Down
13 changes: 12 additions & 1 deletion spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,23 @@
File.binwrite('test.png', png)
end
let(:image) { Rack::Test::UploadedFile.new('test.png', 'image/png') }

it 'returns a table' do
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { image: image }
expect(response.status).to eq(200)
end

it 'returns a table with nested' do
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: {
nested: { image: image, caption: 'Some caption' },
nested_image: { image: image, caption: 'Some caption' },
}
expect(response.status).to eq(200)
end

it 'returns a table with array' do
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { images: [image, image] }
expect(response.status).to eq(200)
end
end

describe '#destroy' do
Expand Down

0 comments on commit 7dfe7fc

Please sign in to comment.