From 10dd37896f7a587960afbd7be8fa00fa74e84485 Mon Sep 17 00:00:00 2001 From: vinhbachsy Date: Tue, 18 Oct 2016 21:44:04 +0800 Subject: [PATCH] Support setting examples for response Add helper method `response_examples` to inject response examples to swagger --- rswag-specs/lib/rswag/specs/example_group_helpers.rb | 6 +++++- test-app/spec/integration/blogs_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/rswag-specs/lib/rswag/specs/example_group_helpers.rb b/rswag-specs/lib/rswag/specs/example_group_helpers.rb index 3024b15..6e5d243 100644 --- a/rswag-specs/lib/rswag/specs/example_group_helpers.rb +++ b/rswag-specs/lib/rswag/specs/example_group_helpers.rb @@ -25,7 +25,7 @@ module Rswag # functionality while also setting the appropriate metadata if applicable def description(value=nil) return super() if value.nil? - metadata[:operation][:description] = value + metadata[:operation][:description] = value end # These are array properties - note the splat operator @@ -61,6 +61,10 @@ module Rswag metadata[:response][:headers][name] = attributes end + def response_examples(example) + metadata[:response][:examples] = example + end + def run_test! # NOTE: rspec 2.x support if RSPEC_VERSION < 3 diff --git a/test-app/spec/integration/blogs_spec.rb b/test-app/spec/integration/blogs_spec.rb index 15fe8a9..588ddb8 100644 --- a/test-app/spec/integration/blogs_spec.rb +++ b/test-app/spec/integration/blogs_spec.rb @@ -52,6 +52,12 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do response '200', 'blog found' do schema '$ref' => '#/definitions/blog' + response_examples 'application/json' => { + id: 1, + title: 'Hello world!', + content: 'Hello world and hello universe. Thank you all very much!!!' + } + let(:blog) { Blog.create(title: 'foo', content: 'bar') } let(:id) { blog.id } run_test!