Rename response_examples to examples for consistent DSL

Special handling `examples` invocation with no parameters to avoid
overriding the `examples` method of rspec-core ExampleGroup
This commit is contained in:
vinhbachsy 2016-10-19 03:04:03 +08:00
parent 5ea97a4278
commit 0b0acfe4c7
4 changed files with 28 additions and 5 deletions

View File

@ -264,7 +264,7 @@ end
### Response examples ### ### Response examples ###
You can provide custom response examples to the generated swagger file by calling the method `response_examples` inside the response block: You can provide custom response examples to the generated swagger file by calling the method `examples` inside the response block:
```ruby ```ruby
# spec/integration/blogs_spec.rb # spec/integration/blogs_spec.rb
@ -275,7 +275,7 @@ describe 'Blogs API' do
get 'Retrieves a blog' do get 'Retrieves a blog' do
response 200, 'blog found' do response 200, 'blog found' do
response_examples 'application/json' => { examples 'application/json' => {
id: 1, id: 1,
title: 'Hello world!', title: 'Hello world!',
content: '...' content: '...'

View File

@ -61,7 +61,11 @@ module Rswag
metadata[:response][:headers][name] = attributes metadata[:response][:headers][name] = attributes
end end
def response_examples(example) # NOTE: Similar to 'description', 'examples' need to handle the case when
# being invoked with no params to avoid overriding 'examples' method of
# rspec-core ExampleGroup
def examples(example = nil)
return super() if example.nil?
metadata[:response][:examples] = example metadata[:response][:examples] = example
end end

View File

@ -113,7 +113,7 @@ module Rswag
context "'path' parameter" do context "'path' parameter" do
before { subject.parameter(name: :id, in: :path) } before { subject.parameter(name: :id, in: :path) }
let(:api_metadata) { { operation: {} } } let(:api_metadata) { { operation: {} } }
it "automatically sets the 'required' flag" do it "automatically sets the 'required' flag" do
expect(api_metadata[:operation][:parameters]).to match( expect(api_metadata[:operation][:parameters]).to match(
[ name: :id, in: :path, required: true ] [ name: :id, in: :path, required: true ]
@ -151,6 +151,25 @@ module Rswag
) )
end end
end end
describe '#examples(example)' do
let(:json_example) do
{
'application/json' => {
foo: 'bar'
}
}
end
let(:api_metadata) { { response: {} } }
before do
subject.examples(json_example)
end
it "adds to the 'response examples' metadata" do
expect(api_metadata[:response][:examples]).to eq(json_example)
end
end
end end
end end
end end

View File

@ -56,7 +56,7 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
schema '$ref' => '#/definitions/blog' schema '$ref' => '#/definitions/blog'
response_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!!!'