Skip to content

Commit

Permalink
Fixes aplication/vnd.api+json content not treated as JSON
Browse files Browse the repository at this point in the history
Connects to #235
  • Loading branch information
kurko committed Dec 14, 2016
1 parent 5eaf2f5 commit 2f894bb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
54 changes: 22 additions & 32 deletions features/api_blueprint_documentation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Generate API Blueprint documentation from test examples
class App < Sinatra::Base
get '/orders' do
content_type :json
content_type "application/vnd.api+json"
[200, {
:page => 1,
Expand Down Expand Up @@ -263,11 +263,6 @@ Feature: Generate API Blueprint documentation from test examples
Host: example.org
+ Body
Content-Type: text/html;charset=utf-8
Content-Length: 57
+ Response 200 (text/html;charset=utf-8)
+ Headers
Expand Down Expand Up @@ -296,8 +291,16 @@ Feature: Generate API Blueprint documentation from test examples
+ Body
Content-Type: application/json
Content-Length: 73
{
"data": {
"type": "order",
"attributes": {
"name": "Order 1",
"amount": 100.0,
"description": "A description"
}
}
}
+ Response 201 (application/json)
Expand All @@ -324,16 +327,11 @@ Feature: Generate API Blueprint documentation from test examples
Host: example.org
+ Body
Content-Type: application/json
Content-Length: 137
+ Response 200 (application/json)
+ Response 200 (application/vnd.api+json)
+ Headers
Content-Type: application/json
Content-Type: application/vnd.api+json
Content-Length: 137
+ Body
Expand Down Expand Up @@ -374,11 +372,6 @@ Feature: Generate API Blueprint documentation from test examples
Host: example.org
Content-Type: application/x-www-form-urlencoded
+ Body
Content-Type: text/html;charset=utf-8
Content-Length: 0
+ Response 200 (text/html;charset=utf-8)
+ Headers
Expand All @@ -394,11 +387,6 @@ Feature: Generate API Blueprint documentation from test examples
Host: example.org
+ Body
Content-Type: application/json
Content-Length: 73
+ Response 200 (application/json)
+ Headers
Expand All @@ -425,11 +413,6 @@ Feature: Generate API Blueprint documentation from test examples
Content-Type: application/json
Host: example.org
+ Body
Content-Type: application/json
Content-Length: 0
+ Response 400 (application/json)
+ Headers
Expand All @@ -446,8 +429,15 @@ Feature: Generate API Blueprint documentation from test examples
+ Body
Content-Type: application/json
Content-Length: 111
{
"data": {
"id": "1",
"type": "order",
"attributes": {
"name": "Order 1"
}
}
}
+ Response 200 (application/json)
Expand Down
25 changes: 19 additions & 6 deletions lib/rspec_api_documentation/views/api_blueprint_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ def parameters

def requests
super.map do |request|
if request[:request_content_type] =~ /application\/json/ && request[:request_body]
request[:request_body] = JSON.pretty_generate(JSON.parse(request[:request_body]))
end
request[:request_body] = body_to_json(request, :request)
request[:response_body] = body_to_json(request, :response)

request[:request_body] = indent(request[:request_body])
request[:request_body] = indent(request[:request_headers_text])
request[:request_body] = indent(request[:response_body])
request[:request_body] = indent(request[:response_headers_text])
request[:request_headers_text] = indent(request[:request_headers_text])
request[:response_body] = indent(request[:response_body])
request[:response_headers_text] = indent(request[:response_headers_text])
request
end
end
Expand All @@ -45,6 +44,20 @@ def indent(string)
end
end
end

# http_call: the hash that contains all information about the HTTP
# request and response.
# message_direction: either `request` or `response`.
def body_to_json(http_call, message_direction)
content_type = http_call["#{message_direction}_content_type".to_sym]
body = http_call["#{message_direction}_body".to_sym] # e.g request_body

if content_type =~ /application\/.*json/ && body
body = JSON.pretty_generate(JSON.parse(body))
end

body
end
end
end
end

0 comments on commit 2f894bb

Please sign in to comment.