Commit 0b9e6ea2 by andrew morton

Fix the formatter, make description parameters more consistent

parent 7c6501cd
...@@ -33,7 +33,7 @@ module RSpec ...@@ -33,7 +33,7 @@ module RSpec
config.extend Parameters, swagger_object: :path_item config.extend Parameters, swagger_object: :path_item
config.extend Operation, swagger_object: :operation config.extend Operation, swagger_object: :operation
config.extend Parameters, swagger_object: :operation config.extend Parameters, swagger_object: :operation
config.extend Response, swagger_object: :status_code config.extend Response, swagger_object: :response
config.include Resolver, :swagger_object config.include Resolver, :swagger_object
end end
...@@ -72,26 +72,13 @@ module RSpec ...@@ -72,26 +72,13 @@ module RSpec
def parameter name, attributes = {} def parameter name, attributes = {}
attributes.symbolize_keys! attributes.symbolize_keys!
raise ArgumentError, "Parameter is missing required 'in' value." unless attributes[:in] validate_location! attributes[:in]
locations = [:query, :header, :path, :formData, :body]
unless locations.include? attributes[:in]
raise ArgumentError, "Parameter has an invalid 'in' value. Try: #{locations}."
end
if attributes[:in] == :body if attributes[:in] == :body
unless attributes[:schema].present? unless attributes[:schema].present?
raise ArgumentError, "Parameter is missing required 'schema' value." raise ArgumentError, "Parameter is missing required 'schema' value."
end end
else else
unless attributes[:type].present? validate_type! attributes[:type]
raise ArgumentError, "Parameter is missing required 'type' value."
end
types = %i(string number integer boolean array file)
unless types.include?(attributes[:type])
raise ArgumentError, "Parameter has an invalid 'type' value. Try: #{types}."
end
end end
# Path attributes are always required # Path attributes are always required
...@@ -111,6 +98,30 @@ module RSpec ...@@ -111,6 +98,30 @@ module RSpec
param_key = "#{param[:in]}&#{param[:name]}" param_key = "#{param[:in]}&#{param[:name]}"
params[param_key] = param params[param_key] = param
end end
private
def validate_location! location
unless location.present?
raise ArgumentError, "Parameter is missing required 'in' value."
end
locations = %i(query header path formData body)
unless locations.include? location
raise ArgumentError, "Parameter has an invalid 'in' value. Try: #{locations}."
end
end
def validate_type! type
unless type.present?
raise ArgumentError, "Parameter is missing required 'type' value."
end
types = %i(string number integer boolean array file)
unless types.include?(type)
raise ArgumentError, "Parameter has an invalid 'type' value. Try: #{types}."
end
end
end end
module Operation module Operation
...@@ -122,13 +133,15 @@ module RSpec ...@@ -122,13 +133,15 @@ module RSpec
metadata[:swagger_operation][:produces] = mime_types metadata[:swagger_operation][:produces] = mime_types
end end
def response status_code, desc, params = {}, &block def response status_code, attributes = {}, params = {}, &block
unless status_code == :default || (100..599).cover?(status_code) attributes.symbolize_keys!
raise ArgumentError, "status_code must be an integer 100 to 599, or :default"
end validate_status_code! status_code
validate_description! attributes[:description]
meta = { meta = {
swagger_object: :status_code, swagger_object: :response,
swagger_response: {status_code: status_code, description: desc} swagger_response: attributes.merge(status_code: status_code)
} }
describe(status_code, meta) do describe(status_code, meta) do
self.module_exec(&block) if block_given? self.module_exec(&block) if block_given?
...@@ -152,11 +165,25 @@ module RSpec ...@@ -152,11 +165,25 @@ module RSpec
end end
end end
it("returns the correct status code", { swagger_object: :response }) do it("returns the correct status code") do
expect(response).to have_http_status(status_code) expect(response).to have_http_status(status_code)
end end
end end
end end
private
def validate_status_code! status_code
unless status_code == :default || (100..599).cover?(status_code)
raise ArgumentError, "status_code must be an integer 100 to 599, or :default"
end
end
def validate_description! description
unless description.present?
raise ArgumentError, "Response is missing required 'description' value."
end
end
end end
module Response module Response
......
require 'swagger_helper' require 'swagger_helper'
RSpec.describe "Requestsing", type: :request do RSpec.describe "Requestsing", type: :request do
# path "/ping" do
# operation :put do
# response(200, {description: 'OK'})
# puts "in here"
# end
# end
path '/posts' do path '/posts' do
operation "GET", summary:"fetch list" do operation "GET", summary:"fetch list" do
produces 'application/json' produces 'application/json'
# params response(200, {description: "successful"}, {})
response(200, "successful", {})
end end
operation "POST",summary: "create" do operation "POST",summary: "create" do
...@@ -17,7 +23,7 @@ RSpec.describe "Requestsing", type: :request do ...@@ -17,7 +23,7 @@ RSpec.describe "Requestsing", type: :request do
let(:body) { { post: { title: 'asdf', body: "blah" } } } let(:body) { { post: { title: 'asdf', body: "blah" } } }
# TODO: it should pull the body from the params # TODO: it should pull the body from the params
response(201, "successfully created", { post: { title: 'asdf', body: "blah" } }.to_json) do response(201, {description: "successfully created"}, { post: { title: 'asdf', body: "blah" } }.to_json) do
it "uses the body we passed in" do it "uses the body we passed in" do
post = JSON.parse(response.body) post = JSON.parse(response.body)
expect(post["title"]).to eq('asdf') expect(post["title"]).to eq('asdf')
...@@ -37,7 +43,7 @@ RSpec.describe "Requestsing", type: :request do ...@@ -37,7 +43,7 @@ RSpec.describe "Requestsing", type: :request do
before { Post.new.save } before { Post.new.save }
parameter "op-param", {in: :query, type: :string} parameter "op-param", {in: :query, type: :string}
response(200, "success", {}) do response(200, {description: "success"}, {}) do
capture_example capture_example
end end
end end
......
...@@ -27,13 +27,9 @@ RSpec.describe RSpec::Swagger::Formatter do ...@@ -27,13 +27,9 @@ RSpec.describe RSpec::Swagger::Formatter do
let(:metadata) do let(:metadata) do
{ {
swagger_object: :response, swagger_object: :response,
swagger_data: { swagger_path_item: {path: "/ping"},
path: "/ping", swagger_operation: {method: :put},
operation: :put, swagger_response: {status_code: 200, description: "OK"},
status_code: 200,
response_description: 'OK',
example: nil
}
} }
end end
...@@ -55,12 +51,10 @@ RSpec.describe RSpec::Swagger::Formatter do ...@@ -55,12 +51,10 @@ RSpec.describe RSpec::Swagger::Formatter do
let(:metadata) do let(:metadata) do
{ {
swagger_object: :response, swagger_object: :response,
swagger_data: { swagger_document: 'doc2.json',
document: 'doc2.json', swagger_path_item: {path: "/ping"},
path: "/ping", swagger_operation: {method: :put},
operation: :put, swagger_response: {status_code: 200, description: "OK"},
status_code: 200
}
} }
end end
...@@ -88,7 +82,9 @@ RSpec.describe RSpec::Swagger::Formatter do ...@@ -88,7 +82,9 @@ RSpec.describe RSpec::Swagger::Formatter do
let(:metadata) do let(:metadata) do
{ {
swagger_object: :response, swagger_object: :response,
swagger_data: {path: '/ping', operation: :get, status_code: 200, response_description: 'all good' } swagger_path_item: {path: "/ping"},
swagger_operation: {method: :get},
swagger_response: {status_code: 200, description: 'all good'},
} }
end end
......
...@@ -83,6 +83,7 @@ RSpec.describe RSpec::Swagger::Helpers::Parameters do ...@@ -83,6 +83,7 @@ RSpec.describe RSpec::Swagger::Helpers::Parameters do
Class.new do Class.new do
include RSpec::Swagger::Helpers::Parameters include RSpec::Swagger::Helpers::Parameters
attr_accessor :metadata attr_accessor :metadata
def describe *args ; end
end end
end end
subject { klass.new } subject { klass.new }
...@@ -142,6 +143,7 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do ...@@ -142,6 +143,7 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do
Class.new do Class.new do
include RSpec::Swagger::Helpers::Operation include RSpec::Swagger::Helpers::Operation
attr_accessor :metadata attr_accessor :metadata
def describe *args ; end
end end
end end
subject { klass.new } subject { klass.new }
...@@ -150,15 +152,19 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do ...@@ -150,15 +152,19 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do
before { subject.metadata = {swagger_object: :operation} } before { subject.metadata = {swagger_object: :operation} }
it "requires code be an integer 100...600 or :default" do it "requires code be an integer 100...600 or :default" do
expect{ subject.response 1, "description" }.to raise_exception(ArgumentError) expect{ subject.response 99, description: "too low" }.to raise_exception(ArgumentError)
expect{ subject.response 99, "description" }.to raise_exception(ArgumentError) expect{ subject.response 600, description: "too high" }.to raise_exception(ArgumentError)
expect{ subject.response 600, "description" }.to raise_exception(ArgumentError) expect{ subject.response '404', description: "string" }.to raise_exception(ArgumentError)
expect{ subject.response '404', "description" }.to raise_exception(ArgumentError) expect{ subject.response 'default', description: "string" }.to raise_exception(ArgumentError)
expect{ subject.response 'default', "description" }.to raise_exception(ArgumentError)
expect{ subject.response 100, description: "low" }.not_to raise_exception
# expect{ subject.response 100, "description" }.not_to raise_exception expect{ subject.response 599, description: "high" }.not_to raise_exception
# expect{ subject.response 599, "description" }.not_to raise_exception expect{ subject.response :default, description: "symbol" }.not_to raise_exception
# expect{ subject.response :default, "description" }.not_to raise_exception end
it "requires a description" do
expect{ subject.response 100 }.to raise_exception(ArgumentError)
expect{ subject.response 100, description: "low" }.not_to raise_exception
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment