Commit bf153373 by andrew morton Committed by GitHub

Merge pull request #29 from drewish/tags

Accept tags for operations from parent contexts
parents fe947519 88083617
...@@ -42,13 +42,16 @@ module RSpec ...@@ -42,13 +42,16 @@ module RSpec
attributes.symbolize_keys! attributes.symbolize_keys!
raise ArgumentError, "Path must start with a /" unless template.starts_with?('/') raise ArgumentError, "Path must start with a /" unless template.starts_with?('/')
#TODO template might be a $ref #TODO template might be a $ref
meta = { meta = {
swagger_object: :path_item, swagger_object: :path_item,
swagger_document: attributes[:swagger_document] || RSpec.configuration.swagger_docs.keys.first, swagger_document: attributes[:swagger_document] || RSpec.configuration.swagger_docs.keys.first,
swagger_path_item: {path: template} swagger_path_item: {path: template},
} }
# Merge tags passed into the path with those from parent contexts.
if attributes[:tags]
meta[:tags] = (metadata.try(:[], :tags) || []) + attributes[:tags]
end
describe(template, meta, &block) describe(template, meta, &block)
end end
end end
...@@ -58,6 +61,12 @@ module RSpec ...@@ -58,6 +61,12 @@ module RSpec
def operation method, attributes = {}, &block def operation method, attributes = {}, &block
attributes.symbolize_keys! attributes.symbolize_keys!
# Include tags from parent contexts so you can tag all the paths
# in a controller at once.
if metadata.try(:[], :tags).present?
attributes[:tags] ||= []
attributes[:tags] += metadata[:tags]
end
method = method.to_s.downcase method = method.to_s.downcase
validate_method! method validate_method! method
...@@ -175,7 +184,8 @@ module RSpec ...@@ -175,7 +184,8 @@ module RSpec
end end
def tags *tags def tags *tags
metadata[:swagger_operation][:tags] = tags metadata[:swagger_operation][:tags] ||= []
metadata[:swagger_operation][:tags] += tags
end end
def response status_code, attributes = {}, &block def response status_code, attributes = {}, &block
......
...@@ -28,3 +28,5 @@ fi ...@@ -28,3 +28,5 @@ fi
cd - cd -
bundle exec rspec bundle exec rspec
# Duplicating the body of the rake task. Need to figure out how to call it directly.
bundle exec rspec -f RSpec::Rails::Swagger::Formatter --order defined -t swagger_object
require 'swagger_helper' require 'swagger_helper'
RSpec.describe "Requestsing", type: :request do RSpec.describe "Sample Requests", type: :request, tags: [:context_tag] do
# path "/ping" do path '/posts', tags: ['path_tag'] do
# operation :put do operation "GET", summary: "fetch list" do
# response(200, {description: 'OK'})
# puts "in here"
# end
# end
path '/posts' do
operation "GET", summary:"fetch list" do
produces 'application/json' produces 'application/json'
tags 'operation_tag'
response(200, {description: "successful"}) response(200, {description: "successful"})
end end
......
...@@ -34,6 +34,17 @@ RSpec.describe RSpec::Rails::Swagger::Helpers::Paths do ...@@ -34,6 +34,17 @@ RSpec.describe RSpec::Rails::Swagger::Helpers::Paths do
subject.path('/ping', swagger_document: 'hello_swagger.json') subject.path('/ping', swagger_document: 'hello_swagger.json')
end end
it "passes tags through to the metadata" do
expect(subject).to receive(:describe).with("/ping", {
swagger_object: :path_item,
swagger_document: RSpec.configuration.swagger_docs.keys.first,
swagger_path_item: {path: '/ping'},
tags: ['tag1']
})
subject.path('/ping', tags: ['tag1'])
end
end end
RSpec.describe RSpec::Rails::Swagger::Helpers::PathItem do RSpec.describe RSpec::Rails::Swagger::Helpers::PathItem do
...@@ -81,6 +92,18 @@ RSpec.describe RSpec::Rails::Swagger::Helpers::PathItem do ...@@ -81,6 +92,18 @@ RSpec.describe RSpec::Rails::Swagger::Helpers::PathItem do
operationId: 'updatePetWithForm' operationId: 'updatePetWithForm'
) )
end end
it 'includes tags from metadata of parent contexts' do
subject.metadata = { tags: ['baz'] }
expect(subject).to receive(:describe).with('get', {
swagger_object: :operation,
swagger_operation: {
tags: ['foo', 'baz'], method: :get
}
})
subject.operation(:get, tags: ['foo'])
end
end end
described_class::METHODS.each do |method| described_class::METHODS.each do |method|
......
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