Skip to content

Commit c7eff87

Browse files
authored
Merge pull request #291 from myabc/fix/response_fields-nested-scopes-formatting
Fix formatting of nested response_field scopes
2 parents 8d0a315 + d56a820 commit c7eff87

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

features/html_documentation.feature

+12-7
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
@@ -71,12 +75,13 @@ Feature: Generate HTML documentation from test examples
7175
| scope[scoped] | This is a scoped variable |
7276
| scope[further][sub] | This is scoped |
7377

74-
Scenario: Examle HTML documentation should include the response fields
78+
Scenario: Example HTML documentation should include the response fields
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

+19-8
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ def filename
3232
def parameters
3333
super.each do |parameter|
3434
if parameter.has_key?(:scope)
35-
scope = Array(parameter[:scope]).each_with_index.map do |scope, index|
36-
if index == 0
37-
scope
38-
else
39-
"[#{scope}]"
40-
end
41-
end.join
42-
parameter[:scope] = scope
35+
parameter[:scope] = format_scope(parameter[:scope])
36+
end
37+
end
38+
end
39+
40+
def response_fields
41+
super.each do |response_field|
42+
if response_field.has_key?(:scope)
43+
response_field[:scope] = format_scope(response_field[:scope])
4344
end
4445
end
4546
end
@@ -72,6 +73,16 @@ def format_hash(hash = {})
7273
"#{k}: #{v}"
7374
end.join("\n")
7475
end
76+
77+
def format_scope(unformatted_scope)
78+
Array(unformatted_scope).each_with_index.map do |scope, index|
79+
if index == 0
80+
scope
81+
else
82+
"[#{scope}]"
83+
end
84+
end.join
85+
end
7586
end
7687
end
7788
end

0 commit comments

Comments
 (0)