From 0ac72edf079a00b7e32eb960c0f32d1ad98c3cfe Mon Sep 17 00:00:00 2001 From: Alex Coles Date: Sat, 2 Jul 2016 11:16:24 +0100 Subject: [PATCH 1/2] Fix formatting of nested response_field scopes Fixes #256 Signed-off-by: Alex Coles --- features/html_documentation.feature | 17 +++++++----- .../views/markup_example.rb | 27 +++++++++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/features/html_documentation.feature b/features/html_documentation.feature index 18aa191a..c213b1ac 100644 --- a/features/html_documentation.feature +++ b/features/html_documentation.feature @@ -8,7 +8,10 @@ Feature: Generate HTML documentation from test examples request = Rack::Request.new(env) response = Rack::Response.new response["Content-Type"] = "application/json" - response.write({ "hello" => request.params["target"] }.to_json) + response.write({ + "hello" => request.params["target"], + "more_greetings" => { "bonjour" => { "message" => "le monde" } } + }.to_json) response.finish end end @@ -31,14 +34,15 @@ Feature: Generate HTML documentation from test examples parameter :scoped, "This is a scoped variable", :scope => :scope parameter :sub, "This is scoped", :scope => [:scope, :further] - response_field :hello, "The greeted thing" + response_field :hello, "The greeted thing" + response_field :message, "Translated greeting", scope: [:more_greetings, :bonjour] example "Greeting your favorite gem" do do_request :target => "rspec_api_documentation" expect(response_headers["Content-Type"]).to eq("application/json") expect(status).to eq(200) - expect(response_body).to eq('{"hello":"rspec_api_documentation"}') + expect(response_body).to eq('{"hello":"rspec_api_documentation","more_greetings":{"bonjour":{"message":"le monde"}}}') end end end @@ -75,8 +79,9 @@ Feature: Generate HTML documentation from test examples When I open the index And I navigate to "Greeting your favorite gem" Then I should see the following response fields: - | name | description | - | hello | The greeted thing | + | name | description | + | hello | The greeted thing | + | more_greetings[bonjour][message] | Translated greeting | Scenario: Example HTML documentation includes the request information When I open the index @@ -99,5 +104,5 @@ Feature: Generate HTML documentation from test examples | Content-Length | 35 | And I should see the following response body: """ - { "hello": "rspec_api_documentation" } + { "hello": "rspec_api_documentation", "more_greetings": { "bonjour": { "message": "le monde" } } } """ diff --git a/lib/rspec_api_documentation/views/markup_example.rb b/lib/rspec_api_documentation/views/markup_example.rb index 7863e844..ab8b5ffe 100644 --- a/lib/rspec_api_documentation/views/markup_example.rb +++ b/lib/rspec_api_documentation/views/markup_example.rb @@ -31,14 +31,15 @@ def filename def parameters super.each do |parameter| if parameter.has_key?(:scope) - scope = Array(parameter[:scope]).each_with_index.map do |scope, index| - if index == 0 - scope - else - "[#{scope}]" - end - end.join - parameter[:scope] = scope + parameter[:scope] = format_scope(parameter[:scope]) + end + end + end + + def response_fields + super.each do |response_field| + if response_field.has_key?(:scope) + response_field[:scope] = format_scope(response_field[:scope]) end end end @@ -71,6 +72,16 @@ def format_hash(hash = {}) "#{k}: #{v}" end.join("\n") end + + def format_scope(unformatted_scope) + Array(unformatted_scope).each_with_index.map do |scope, index| + if index == 0 + scope + else + "[#{scope}]" + end + end.join + end end end end From d56a8204e9510d2a974439cefc833f8b20ed6d56 Mon Sep 17 00:00:00 2001 From: Alex Coles Date: Sat, 2 Jul 2016 11:48:04 +0100 Subject: [PATCH 2/2] Fix typo in HTML Documentation feature Signed-off-by: Alex Coles --- features/html_documentation.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/html_documentation.feature b/features/html_documentation.feature index c213b1ac..ef2d0aad 100644 --- a/features/html_documentation.feature +++ b/features/html_documentation.feature @@ -75,7 +75,7 @@ Feature: Generate HTML documentation from test examples | scope[scoped] | This is a scoped variable | | scope[further][sub] | This is scoped | - Scenario: Examle HTML documentation should include the response fields + Scenario: Example HTML documentation should include the response fields When I open the index And I navigate to "Greeting your favorite gem" Then I should see the following response fields: