Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept tags for operations from parent contexts #29

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/rspec/rails/swagger/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ def path template, attributes = {}, &block
attributes.symbolize_keys!

raise ArgumentError, "Path must start with a /" unless template.starts_with?('/')

#TODO template might be a $ref
meta = {
swagger_object: :path_item,
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)
end
end
Expand All @@ -58,6 +61,12 @@ module PathItem

def operation method, attributes = {}, &block
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
validate_method! method
Expand Down Expand Up @@ -175,7 +184,8 @@ def produces *mime_types
end

def tags *tags
metadata[:swagger_operation][:tags] = tags
metadata[:swagger_operation][:tags] ||= []
metadata[:swagger_operation][:tags] += tags
end

def response status_code, attributes = {}, &block
Expand Down
2 changes: 2 additions & 0 deletions make_site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ fi
cd -

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
14 changes: 4 additions & 10 deletions spec/requests/request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
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
RSpec.describe "Sample Requests", type: :request, tags: [:context_tag] do
path '/posts', tags: ['path_tag'] do
operation "GET", summary: "fetch list" do
produces 'application/json'
tags 'operation_tag'

response(200, {description: "successful"})
end
Expand Down
23 changes: 23 additions & 0 deletions spec/rspec/rails/swagger/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def describe *args ; end

subject.path('/ping', swagger_document: 'hello_swagger.json')
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

RSpec.describe RSpec::Rails::Swagger::Helpers::PathItem do
Expand Down Expand Up @@ -81,6 +92,18 @@ def describe *args ; end
operationId: 'updatePetWithForm'
)
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

described_class::METHODS.each do |method|
Expand Down