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 c876cac
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 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
6 changes: 2 additions & 4 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@ class TablesCreateTest < ActionDispatch::IntegrationTest
class TablesUpdateTest < ActionDispatch::IntegrationTest
openapi!

test 'returns a table' do
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: {
nested: { image: image, caption: 'Some caption' },
}
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { images: [image, image] }
assert_response 200
end
end
Expand Down
40 changes: 38 additions & 2 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -474,24 +474,60 @@
"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"
"images"
]
},
"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: 28 additions & 2 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,41 @@ paths:
required:
- 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
- images
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
7 changes: 3 additions & 4 deletions spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@
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: {
nested: { image: image, caption: 'Some caption' },
}

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
Expand Down

0 comments on commit c876cac

Please sign in to comment.