Skip to content

Commit 0ac72ed

Browse files
committed
Fix formatting of nested response_field scopes
Fixes #256 Signed-off-by: Alex Coles <alex@alexbcoles.com>
1 parent d8570ef commit 0ac72ed

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

features/html_documentation.feature

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ Feature: Generate HTML documentation from test examples
88
request = Rack::Request.new(env)
99
response = Rack::Response.new
1010
response["Content-Type"] = "application/json"
11-
response.write({ "hello" => request.params["target"] }.to_json)
11+
response.write({
12+
"hello" => request.params["target"],
13+
"more_greetings" => { "bonjour" => { "message" => "le monde" } }
14+
}.to_json)
1215
response.finish
1316
end
1417
end
@@ -31,14 +34,15 @@ Feature: Generate HTML documentation from test examples
3134
parameter :scoped, "This is a scoped variable", :scope => :scope
3235
parameter :sub, "This is scoped", :scope => [:scope, :further]
3336
34-
response_field :hello, "The greeted thing"
37+
response_field :hello, "The greeted thing"
38+
response_field :message, "Translated greeting", scope: [:more_greetings, :bonjour]
3539
3640
example "Greeting your favorite gem" do
3741
do_request :target => "rspec_api_documentation"
3842
3943
expect(response_headers["Content-Type"]).to eq("application/json")
4044
expect(status).to eq(200)
41-
expect(response_body).to eq('{"hello":"rspec_api_documentation"}')
45+
expect(response_body).to eq('{"hello":"rspec_api_documentation","more_greetings":{"bonjour":{"message":"le monde"}}}')
4246
end
4347
end
4448
end
@@ -75,8 +79,9 @@ Feature: Generate HTML documentation from test examples
7579
When I open the index
7680
And I navigate to "Greeting your favorite gem"
7781
Then I should see the following response fields:
78-
| name | description |
79-
| hello | The greeted thing |
82+
| name | description |
83+
| hello | The greeted thing |
84+
| more_greetings[bonjour][message] | Translated greeting |
8085

8186
Scenario: Example HTML documentation includes the request information
8287
When I open the index
@@ -99,5 +104,5 @@ Feature: Generate HTML documentation from test examples
99104
| Content-Length | 35 |
100105
And I should see the following response body:
101106
"""
102-
{ "hello": "rspec_api_documentation" }
107+
{ "hello": "rspec_api_documentation", "more_greetings": { "bonjour": { "message": "le monde" } } }
103108
"""

lib/rspec_api_documentation/views/markup_example.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ def filename
3131
def parameters
3232
super.each do |parameter|
3333
if parameter.has_key?(:scope)
34-
scope = Array(parameter[:scope]).each_with_index.map do |scope, index|
35-
if index == 0
36-
scope
37-
else
38-
"[#{scope}]"
39-
end
40-
end.join
41-
parameter[:scope] = scope
34+
parameter[:scope] = format_scope(parameter[:scope])
35+
end
36+
end
37+
end
38+
39+
def response_fields
40+
super.each do |response_field|
41+
if response_field.has_key?(:scope)
42+
response_field[:scope] = format_scope(response_field[:scope])
4243
end
4344
end
4445
end
@@ -71,6 +72,16 @@ def format_hash(hash = {})
7172
"#{k}: #{v}"
7273
end.join("\n")
7374
end
75+
76+
def format_scope(unformatted_scope)
77+
Array(unformatted_scope).each_with_index.map do |scope, index|
78+
if index == 0
79+
scope
80+
else
81+
"[#{scope}]"
82+
end
83+
end.join
84+
end
7485
end
7586
end
7687
end

0 commit comments

Comments
 (0)