mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Merge branch 'add-formData-support' of https://github.com/thg303/rswag into thg303-add-formData-support
This commit is contained in:
commit
b16198377b
@ -138,15 +138,21 @@ module Rswag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_form_payload(parameters, example)
|
||||||
|
#See http://seejohncode.com/2012/04/29/quick-tip-testing-multipart-uploads-with-rspec/
|
||||||
|
# Rather that serializing with the appropriate encoding (e.g. multipart/form-data),
|
||||||
|
# Rails test infrastructure allows us to send the values directly as a hash
|
||||||
|
# PROS: simple to implement, CONS: serialization/deserialization is bypassed in test
|
||||||
|
tuples = parameters
|
||||||
|
.select { |p| p[:in] == :formData }
|
||||||
|
.map { |p| [ p[:name], example.send(p[:name]) ] }
|
||||||
|
Hash[ tuples ]
|
||||||
|
end
|
||||||
|
|
||||||
def build_json_payload(parameters, example)
|
def build_json_payload(parameters, example)
|
||||||
body_param = parameters.select { |p| p[:in] == :body }.first
|
body_param = parameters.select { |p| p[:in] == :body }.first
|
||||||
body_param ? example.send(body_param[:name]).to_json : nil
|
body_param ? example.send(body_param[:name]).to_json : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_form_payload(parameters, example)
|
|
||||||
nil
|
|
||||||
# TODO:
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -158,6 +158,25 @@ module Rswag
|
|||||||
expect(request[:payload]).to eq("{\"text\":\"Some comment\"}")
|
expect(request[:payload]).to eq("{\"text\":\"Some comment\"}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'form payload' do
|
||||||
|
before do
|
||||||
|
metadata[:operation][:consumes] = [ 'multipart/form-data' ]
|
||||||
|
metadata[:operation][:parameters] = [
|
||||||
|
{ name: 'f1', in: :formData, type: :string },
|
||||||
|
{ name: 'f2', in: :formData, type: :string }
|
||||||
|
]
|
||||||
|
allow(example).to receive(:f1).and_return('foo blah')
|
||||||
|
allow(example).to receive(:f2).and_return('bar blah')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets payload to hash of names and example values' do
|
||||||
|
expect(request[:payload]).to eq(
|
||||||
|
'f1' => 'foo blah',
|
||||||
|
'f2' => 'bar blah'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'produces content' do
|
context 'produces content' do
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
require 'fileutils'
|
||||||
|
|
||||||
class BlogsController < ApplicationController
|
class BlogsController < ApplicationController
|
||||||
|
|
||||||
# POST /blogs
|
# POST /blogs
|
||||||
@ -6,6 +8,14 @@ class BlogsController < ApplicationController
|
|||||||
respond_with @blog
|
respond_with @blog
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Put /blogs/1
|
||||||
|
def upload
|
||||||
|
@blog = Blog.find_by_id(params[:id])
|
||||||
|
return head :not_found if @blog.nil?
|
||||||
|
@blog.thumbnail = save_uploaded_file params[:file]
|
||||||
|
head @blog.save ? :ok : :unprocsessible_entity
|
||||||
|
end
|
||||||
|
|
||||||
# GET /blogs
|
# GET /blogs
|
||||||
def index
|
def index
|
||||||
@blogs = Blog.all
|
@blogs = Blog.all
|
||||||
@ -22,4 +32,13 @@ class BlogsController < ApplicationController
|
|||||||
respond_with @blog, status: :not_found and return unless @blog
|
respond_with @blog, status: :not_found and return unless @blog
|
||||||
respond_with @blog
|
respond_with @blog
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def save_uploaded_file(field)
|
||||||
|
return if field.nil?
|
||||||
|
file = File.join('tmp', field.original_filename)
|
||||||
|
FileUtils.cp field.tempfile.path, file
|
||||||
|
field.original_filename
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,8 @@ class Blog < ActiveRecord::Base
|
|||||||
{
|
{
|
||||||
id: id,
|
id: id,
|
||||||
title: title,
|
title: title,
|
||||||
content: nil
|
content: nil,
|
||||||
|
thumbnail: thumbnail
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
TestApp::Application.routes.draw do
|
TestApp::Application.routes.draw do
|
||||||
resources :blogs
|
resources :blogs
|
||||||
|
put '/blogs/:id/upload', to: 'blogs#upload'
|
||||||
|
|
||||||
post 'auth-tests/basic', to: 'auth_tests#basic'
|
post 'auth-tests/basic', to: 'auth_tests#basic'
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ class CreateBlogs < migration_class
|
|||||||
create_table :blogs do |t|
|
create_table :blogs do |t|
|
||||||
t.string :title
|
t.string :title
|
||||||
t.text :content
|
t.text :content
|
||||||
|
t.string :thumbnail
|
||||||
|
|
||||||
t.timestamps
|
t.timestamps
|
||||||
end
|
end
|
||||||
|
|||||||
@ -15,6 +15,7 @@ ActiveRecord::Schema.define(version: 20160218212104) do
|
|||||||
create_table "blogs", force: :cascade do |t|
|
create_table "blogs", force: :cascade do |t|
|
||||||
t.string "title"
|
t.string "title"
|
||||||
t.text "content"
|
t.text "content"
|
||||||
|
t.string "thumbnail"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
end
|
end
|
||||||
|
|||||||
BIN
test-app/spec/fixtures/thumbnail.png
vendored
Normal file
BIN
test-app/spec/fixtures/thumbnail.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
@ -10,7 +10,7 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
operationId 'createBlog'
|
operationId 'createBlog'
|
||||||
consumes 'application/json'
|
consumes 'application/json'
|
||||||
produces 'application/json'
|
produces 'application/json'
|
||||||
parameter name: :blog, :in => :body, schema: { '$ref' => '#/definitions/blog' }
|
parameter name: :blog, in: :body, schema: { '$ref' => '#/definitions/blog' }
|
||||||
|
|
||||||
let(:blog) { { title: 'foo', content: 'bar' } }
|
let(:blog) { { title: 'foo', content: 'bar' } }
|
||||||
|
|
||||||
@ -24,11 +24,6 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
let(:blog) { { title: 'foo' } }
|
let(:blog) { { title: 'foo' } }
|
||||||
run_test!
|
run_test!
|
||||||
end
|
end
|
||||||
|
|
||||||
response '406', 'unsupported accept header' do
|
|
||||||
let(:'Accept') { 'application/foo' }
|
|
||||||
run_test!
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
get 'Searches blogs' do
|
get 'Searches blogs' do
|
||||||
@ -53,10 +48,10 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
path '/blogs/{id}' do
|
path '/blogs/{id}' do
|
||||||
parameter name: :id, :in => :path, :type => :string
|
parameter name: :id, in: :path, type: :string
|
||||||
|
|
||||||
let(:id) { blog.id }
|
let(:id) { blog.id }
|
||||||
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
|
let(:blog) { Blog.create(title: 'foo', content: 'bar', thumbnail: 'thumbnail.png') }
|
||||||
|
|
||||||
get 'Retrieves a blog' do
|
get 'Retrieves a blog' do
|
||||||
tags 'Blogs'
|
tags 'Blogs'
|
||||||
@ -74,9 +69,11 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
examples 'application/json' => {
|
examples 'application/json' => {
|
||||||
id: 1,
|
id: 1,
|
||||||
title: 'Hello world!',
|
title: 'Hello world!',
|
||||||
content: 'Hello world and hello universe. Thank you all very much!!!'
|
content: 'Hello world and hello universe. Thank you all very much!!!',
|
||||||
|
thumbnail: "thumbnail.png"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let(:id) { blog.id }
|
||||||
run_test!
|
run_test!
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -84,9 +81,24 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
let(:id) { 'invalid' }
|
let(:id) { 'invalid' }
|
||||||
run_test!
|
run_test!
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
response '406', 'unsupported accept header' do
|
path '/blogs/{id}/upload' do
|
||||||
let(:'Accept') { 'application/foo' }
|
parameter name: :id, in: :path, type: :string
|
||||||
|
|
||||||
|
let(:id) { blog.id }
|
||||||
|
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
|
||||||
|
|
||||||
|
put 'upload a blog thumbnail' do
|
||||||
|
tags 'Blogs'
|
||||||
|
description 'Upload a thumbnail for specific blog by id'
|
||||||
|
operationId 'uploadThumbnailBlog'
|
||||||
|
consumes 'multipart/form-data'
|
||||||
|
parameter name: :file, :in => :formData, :type => :file, required: true
|
||||||
|
|
||||||
|
response '200', 'blog updated' do
|
||||||
|
let(:file) { Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/thumbnail.png")) }
|
||||||
run_test!
|
run_test!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -47,6 +47,10 @@ RSpec.configure do |config|
|
|||||||
# triggering implicit auto-inclusion in groups with matching metadata.
|
# triggering implicit auto-inclusion in groups with matching metadata.
|
||||||
config.shared_context_metadata_behavior = :apply_to_host_groups
|
config.shared_context_metadata_behavior = :apply_to_host_groups
|
||||||
|
|
||||||
|
config.after(:suite) do
|
||||||
|
File.delete("#{Rails.root}/tmp/thumbnail.png") if File.file?("#{Rails.root}/tmp/thumbnail.png")
|
||||||
|
end
|
||||||
|
|
||||||
# The settings below are suggested to provide a good initial experience
|
# The settings below are suggested to provide a good initial experience
|
||||||
# with RSpec, but feel free to customize to your heart's content.
|
# with RSpec, but feel free to customize to your heart's content.
|
||||||
=begin
|
=begin
|
||||||
|
|||||||
@ -39,9 +39,10 @@ RSpec.configure do |config|
|
|||||||
properties: {
|
properties: {
|
||||||
id: { type: 'integer' },
|
id: { type: 'integer' },
|
||||||
title: { type: 'string' },
|
title: { type: 'string' },
|
||||||
content: { type: 'string', 'x-nullable': true }
|
content: { type: 'string', 'x-nullable': true },
|
||||||
|
thumbnail: { type: 'string'}
|
||||||
},
|
},
|
||||||
required: [ 'id', 'title', 'content' ]
|
required: [ 'id', 'title', 'content', 'thumbnail' ]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
securityDefinitions: {
|
securityDefinitions: {
|
||||||
|
|||||||
@ -61,9 +61,6 @@
|
|||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/errors_object"
|
"$ref": "#/definitions/errors_object"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"406": {
|
|
||||||
"description": "unsupported accept header"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -140,15 +137,47 @@
|
|||||||
"application/json": {
|
"application/json": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"title": "Hello world!",
|
"title": "Hello world!",
|
||||||
"content": "Hello world and hello universe. Thank you all very much!!!"
|
"content": "Hello world and hello universe. Thank you all very much!!!",
|
||||||
|
"thumbnail": "thumbnail.png"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"404": {
|
"404": {
|
||||||
"description": "blog not found"
|
"description": "blog not found"
|
||||||
},
|
}
|
||||||
"406": {
|
}
|
||||||
"description": "unsupported accept header"
|
}
|
||||||
|
},
|
||||||
|
"/blogs/{id}/upload": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"type": "string",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"put": {
|
||||||
|
"summary": "upload a blog thumbnail",
|
||||||
|
"tags": [
|
||||||
|
"Blogs"
|
||||||
|
],
|
||||||
|
"description": "Upload a thumbnail for specific blog by id",
|
||||||
|
"operationId": "uploadThumbnailBlog",
|
||||||
|
"consumes": [
|
||||||
|
"multipart/form-data"
|
||||||
|
],
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "file",
|
||||||
|
"in": "formData",
|
||||||
|
"type": "file",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "blog updated"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,12 +213,16 @@
|
|||||||
"content": {
|
"content": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"x-nullable": true
|
"x-nullable": true
|
||||||
|
},
|
||||||
|
"thumbnail": {
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
"id",
|
"id",
|
||||||
"title",
|
"title",
|
||||||
"content"
|
"content",
|
||||||
|
"thumbnail"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user