Skip to content

Commit

Permalink
Merge pull request #46 from blocknotes/fix-nested-uploaded-file-examples
Browse files Browse the repository at this point in the history
Fix nested uploaded file examples
  • Loading branch information
k0kubun committed Mar 30, 2022
2 parents c6ef9dc + f4426c8 commit b812c58
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 b812c58

Please sign in to comment.