Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested uploaded file examples #46

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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