Commit 0b9e6ea2 by andrew morton

Fix the formatter, make description parameters more consistent

parent 7c6501cd
......@@ -33,7 +33,7 @@ module RSpec
config.extend Parameters, swagger_object: :path_item
config.extend Operation, 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
end
......@@ -72,26 +72,13 @@ module RSpec
def parameter name, attributes = {}
attributes.symbolize_keys!
raise ArgumentError, "Parameter is missing required 'in' value." unless attributes[:in]
locations = [:query, :header, :path, :formData, :body]
unless locations.include? attributes[:in]
raise ArgumentError, "Parameter has an invalid 'in' value. Try: #{locations}."
end
validate_location! attributes[:in]
if attributes[:in] == :body
unless attributes[:schema].present?
raise ArgumentError, "Parameter is missing required 'schema' value."
end
else
unless attributes[:type].present?
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
validate_type! attributes[:type]
end
# Path attributes are always required
......@@ -111,6 +98,30 @@ module RSpec
param_key = "#{param[:in]}&#{param[:name]}"
params[param_key] = param
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
module Operation
......@@ -122,13 +133,15 @@ module RSpec
metadata[:swagger_operation][:produces] = mime_types
end
def response status_code, desc, params = {}, &block
unless status_code == :default || (100..599).cover?(status_code)
raise ArgumentError, "status_code must be an integer 100 to 599, or :default"
end
def response status_code, attributes = {}, params = {}, &block
attributes.symbolize_keys!
validate_status_code! status_code
validate_description! attributes[:description]
meta = {
swagger_object: :status_code,
swagger_response: {status_code: status_code, description: desc}
swagger_object: :response,
swagger_response: attributes.merge(status_code: status_code)
}
describe(status_code, meta) do
self.module_exec(&block) if block_given?
......@@ -152,11 +165,25 @@ module RSpec
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)
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
module Response
......
require 'swagger_helper'
RSpec.describe "Requestsing", type: :request do
# path "/ping" do
# operation :put do
# response(200, {description: 'OK'})
# puts "in here"
# end
# end
path '/posts' do
operation "GET", summary:"fetch list" do
produces 'application/json'
# params
response(200, "successful", {})
response(200, {description: "successful"}, {})
end
operation "POST",summary: "create" do
......@@ -17,7 +23,7 @@ RSpec.describe "Requestsing", type: :request do
let(:body) { { post: { title: 'asdf', body: "blah" } } }
# 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
post = JSON.parse(response.body)
expect(post["title"]).to eq('asdf')
......@@ -37,7 +43,7 @@ RSpec.describe "Requestsing", type: :request do
before { Post.new.save }
parameter "op-param", {in: :query, type: :string}
response(200, "success", {}) do
response(200, {description: "success"}, {}) do
capture_example
end
end
......
......@@ -27,13 +27,9 @@ RSpec.describe RSpec::Swagger::Formatter do
let(:metadata) do
{
swagger_object: :response,
swagger_data: {
path: "/ping",
operation: :put,
status_code: 200,
response_description: 'OK',
example: nil
}
swagger_path_item: {path: "/ping"},
swagger_operation: {method: :put},
swagger_response: {status_code: 200, description: "OK"},
}
end
......@@ -55,12 +51,10 @@ RSpec.describe RSpec::Swagger::Formatter do
let(:metadata) do
{
swagger_object: :response,
swagger_data: {
document: 'doc2.json',
path: "/ping",
operation: :put,
status_code: 200
}
swagger_document: 'doc2.json',
swagger_path_item: {path: "/ping"},
swagger_operation: {method: :put},
swagger_response: {status_code: 200, description: "OK"},
}
end
......@@ -88,7 +82,9 @@ RSpec.describe RSpec::Swagger::Formatter do
let(:metadata) do
{
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
......
......@@ -83,6 +83,7 @@ RSpec.describe RSpec::Swagger::Helpers::Parameters do
Class.new do
include RSpec::Swagger::Helpers::Parameters
attr_accessor :metadata
def describe *args ; end
end
end
subject { klass.new }
......@@ -142,6 +143,7 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do
Class.new do
include RSpec::Swagger::Helpers::Operation
attr_accessor :metadata
def describe *args ; end
end
end
subject { klass.new }
......@@ -150,15 +152,19 @@ RSpec.describe RSpec::Swagger::Helpers::Operation do
before { subject.metadata = {swagger_object: :operation} }
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" }.to raise_exception(ArgumentError)
expect{ subject.response 600, "description" }.to raise_exception(ArgumentError)
expect{ subject.response '404', "description" }.to raise_exception(ArgumentError)
expect{ subject.response 'default', "description" }.to raise_exception(ArgumentError)
# expect{ subject.response 100, "description" }.not_to raise_exception
# expect{ subject.response 599, "description" }.not_to raise_exception
# expect{ subject.response :default, "description" }.not_to raise_exception
expect{ subject.response 99, description: "too low" }.to raise_exception(ArgumentError)
expect{ subject.response 600, description: "too high" }.to raise_exception(ArgumentError)
expect{ subject.response '404', description: "string" }.to raise_exception(ArgumentError)
expect{ subject.response 'default', description: "string" }.to raise_exception(ArgumentError)
expect{ subject.response 100, description: "low" }.not_to raise_exception
expect{ subject.response 599, description: "high" }.not_to raise_exception
expect{ subject.response :default, description: "symbol" }.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
......
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