Skip to content

Commit

Permalink
Merge pull request #126 from champierre/fix-arrayed-uploaded-file-exa…
Browse files Browse the repository at this point in the history
…mples

Fix arrayed uploaded file examples
  • Loading branch information
exoego committed Aug 15, 2023
2 parents ada8ce3 + 24beca9 commit 6a1fbf4
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 74 deletions.
9 changes: 8 additions & 1 deletion lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ def adjust_params(value)
adjust_params(v)
when Array
result = v.map do |item|
item.is_a?(ActionDispatch::Http::UploadedFile) ? item.original_filename : item
case item
when ActionDispatch::Http::UploadedFile
item.original_filename
when Hash
adjust_params(item)
else
item
end
end
value[key] = result
end
Expand Down
9 changes: 9 additions & 0 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ class ImageTest < ActionDispatch::IntegrationTest
post '/images/upload_multiple', params: { images: [image, image] }
assert_response 200
end

test 'returns a image payload with upload multiple 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_multiple_nested', params: { images: [{ image: image }, { image: image }] }
assert_response 200
end
end

class ExtraRoutesTest < ActionDispatch::IntegrationTest
Expand Down
4 changes: 4 additions & 0 deletions spec/rails/app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def upload_multiple
send_image
end

def upload_multiple_nested
send_image
end

private

def send_image
Expand Down
1 change: 1 addition & 0 deletions spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
post 'upload'
post 'upload_nested'
post 'upload_multiple'
post 'upload_multiple_nested'
end
end
resources :users, only: [:show, :create] do
Expand Down
146 changes: 103 additions & 43 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,9 @@
}
}
},
"/images/upload_multiple": {
"/images/upload_nested": {
"post": {
"summary": "upload_multiple",
"summary": "upload_nested",
"tags": [
"Image"
],
Expand All @@ -798,30 +798,81 @@
"schema": {
"type": "object",
"properties": {
"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": [
"images"
"nested_image"
]
},
"example": {
"images": [
"test.png",
"test.png"
"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 multiple",
"description": "returns a image payload with upload",
"content": {
"image/png": {
"schema": {
Expand All @@ -834,9 +885,9 @@
}
}
},
"/images/upload_nested": {
"/images/upload_multiple_nested": {
"post": {
"summary": "upload_nested",
"summary": "upload_multiple_nested",
"tags": [
"Image"
],
Expand All @@ -846,39 +897,42 @@
"schema": {
"type": "object",
"properties": {
"nested_image": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
"images": {
"type": "array",
"items": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
}
},
"caption": {
"type": "string"
}
},
"required": [
"image",
"caption"
]
"required": [
"image"
]
}
}
},
"required": [
"nested_image"
"images"
]
},
"example": {
"nested_image": {
"image": "test.png",
"caption": "Some caption"
}
"images": [
{
"image": "test.png"
},
{
"image": "test.png"
}
]
}
}
}
},
"responses": {
"200": {
"description": "returns a image payload with upload nested",
"description": "returns a image payload with upload multiple nested",
"content": {
"image/png": {
"schema": {
Expand All @@ -891,9 +945,9 @@
}
}
},
"/images/upload": {
"/images/upload_multiple": {
"post": {
"summary": "upload",
"summary": "upload_multiple",
"tags": [
"Image"
],
Expand All @@ -903,24 +957,30 @@
"schema": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
"images": {
"type": "array",
"items": {
"type": "string",
"format": "binary"
}
}
},
"required": [
"image"
"images"
]
},
"example": {
"image": "test.png"
"images": [
"test.png",
"test.png"
]
}
}
}
},
"responses": {
"200": {
"description": "returns a image payload with upload",
"description": "returns a image payload with upload multiple",
"content": {
"image/png": {
"schema": {
Expand Down
95 changes: 65 additions & 30 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -526,36 +526,6 @@ paths:
example:
- name: file.png
tags: []
"/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":
post:
summary: upload
Expand Down Expand Up @@ -618,3 +588,68 @@ paths:
schema:
type: string
format: binary
"/images/upload_multiple_nested":
post:
summary: upload_multiple_nested
tags:
- Image
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
images:
type: array
items:
type: object
properties:
image:
type: string
format: binary
required:
- image
required:
- images
example:
images:
- image: test.png
- image: test.png
responses:
'200':
description: returns a image payload with upload multiple nested
content:
image/png:
schema:
type: string
format: binary
"/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
Loading

0 comments on commit 6a1fbf4

Please sign in to comment.