Skip to content

Commit

Permalink
Fix nested uploaded file examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattia Roccoberton committed Mar 28, 2022
1 parent c6ef9dc commit f4426c8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
6 changes: 6 additions & 0 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,15 @@ def try_cast(value)
def build_example(value)
return nil if value.nil?
value = value.dup
adjust_params(value)
end

def adjust_params(value)
value.each do |key, v|
if v.is_a?(ActionDispatch::Http::UploadedFile)
value[key] = v.original_filename
elsif v.is_a?(Hash)
adjust_params(v)
end
end
end
Expand Down
19 changes: 15 additions & 4 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,25 @@
"schema": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
"nested": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary"
},
"caption": {
"type": "string"
}
}
}
}
},
"example": {
"image": "test.png"
"nested": {
"image": "#<ActionDispatch::Http::UploadedFile:0x00007fa723dec4a8>",
"caption": "Some caption"
}
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,18 @@ paths:
schema:
type: object
properties:
image:
type: string
format: binary
nested:
type: object
properties:
image:
type: string
format: binary
caption:
type: string
example:
image: test.png
nested:
image: test.png
caption: Some caption
responses:
'200':
description: returns a table
Expand Down
4 changes: 3 additions & 1 deletion spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@
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 }
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: {
nested: { image: image, caption: 'Some caption' }
}
expect(response.status).to eq(200)
end
end
Expand Down

0 comments on commit f4426c8

Please sign in to comment.