Skip to content

Commit

Permalink
Merge pull request #17 from fruitriin/fix-invalid-openapi-from-multip…
Browse files Browse the repository at this point in the history
…art-data

fix Multipart/form-data example object to filename
  • Loading branch information
k0kubun authored Jan 27, 2021
2 parents 1442444 + 937945b commit 4697904
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
15 changes: 14 additions & 1 deletion lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def build_request_body(record)
content: {
normalize_content_type(record.request_content_type) => {
schema: build_property(record.request_params),
example: (record.request_params if example_enabled?),
example: (build_example(record.request_params) if example_enabled?),
}.compact
}
}
Expand Down Expand Up @@ -126,6 +126,19 @@ def try_cast(value)
end
end

def build_example(value)
return nil if value.nil?
examples = {}
value.each do |key, v|
if v.is_a? ActionDispatch::Http::UploadedFile
examples[key] = v.original_filename
else
examples[key] = v
end
end
examples
end

def normalize_path(path)
path.gsub(%r|/:([^:/]+)|, '/{\1}')
end
Expand Down
13 changes: 1 addition & 12 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,7 @@ paths:
type: string
format: binary
example:
image: !ruby/object:ActionDispatch::Http::UploadedFile
tempfile: !ruby/object:Tempfile
unlinked: true
mode: 194
tmpfile: &1 !ruby/object:File {}
opts:
:perm: 384
delegate_dc_obj: *1
original_filename: test.png
content_type: image/png
headers: "Content-Disposition: form-data; name=\"image\"; filename=\"test.png\"\r\nContent-Type:
image/png\r\nContent-Length: 71\r\n"
image: test.png
responses:
'200':
description: returns a table
Expand Down

0 comments on commit 4697904

Please sign in to comment.