Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting of nested response_field scopes #291

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions features/html_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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" } } }
"""
27 changes: 19 additions & 8 deletions lib/rspec_api_documentation/views/markup_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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