mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
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:
parent
5ea97a4278
commit
0b0acfe4c7
@ -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: '...'
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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!!!'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user