diff --git a/features/html_documentation.feature b/features/html_documentation.feature index 18aa191a..ef2d0aad 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 @@ -71,12 +75,13 @@ 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: - | 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