Skip to content

Commit 3f41da9

Browse files
committed
Fixes aplication/vnd.api+json content not treated as JSON
Connects to #235
1 parent ca98bb0 commit 3f41da9

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

features/api_blueprint_documentation.feature

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: Generate API Blueprint documentation from test examples
77
88
class App < Sinatra::Base
99
get '/orders' do
10-
content_type :json
10+
content_type "application/vnd.api+json"
1111
1212
[200, {
1313
:page => 1,
@@ -263,11 +263,6 @@ Feature: Generate API Blueprint documentation from test examples
263263
264264
Host: example.org
265265
266-
+ Body
267-
268-
Content-Type: text/html;charset=utf-8
269-
Content-Length: 57
270-
271266
+ Response 200 (text/html;charset=utf-8)
272267
273268
+ Headers
@@ -296,8 +291,16 @@ Feature: Generate API Blueprint documentation from test examples
296291
297292
+ Body
298293
299-
Content-Type: application/json
300-
Content-Length: 73
294+
{
295+
"data": {
296+
"type": "order",
297+
"attributes": {
298+
"name": "Order 1",
299+
"amount": 100.0,
300+
"description": "A description"
301+
}
302+
}
303+
}
301304
302305
+ Response 201 (application/json)
303306
@@ -324,16 +327,11 @@ Feature: Generate API Blueprint documentation from test examples
324327
325328
Host: example.org
326329
327-
+ Body
328-
329-
Content-Type: application/json
330-
Content-Length: 137
331-
332-
+ Response 200 (application/json)
330+
+ Response 200 (application/vnd.api+json)
333331
334332
+ Headers
335333
336-
Content-Type: application/json
334+
Content-Type: application/vnd.api+json
337335
Content-Length: 137
338336
339337
+ Body
@@ -374,11 +372,6 @@ Feature: Generate API Blueprint documentation from test examples
374372
Host: example.org
375373
Content-Type: application/x-www-form-urlencoded
376374
377-
+ Body
378-
379-
Content-Type: text/html;charset=utf-8
380-
Content-Length: 0
381-
382375
+ Response 200 (text/html;charset=utf-8)
383376
384377
+ Headers
@@ -394,11 +387,6 @@ Feature: Generate API Blueprint documentation from test examples
394387
395388
Host: example.org
396389
397-
+ Body
398-
399-
Content-Type: application/json
400-
Content-Length: 73
401-
402390
+ Response 200 (application/json)
403391
404392
+ Headers
@@ -425,11 +413,6 @@ Feature: Generate API Blueprint documentation from test examples
425413
Content-Type: application/json
426414
Host: example.org
427415
428-
+ Body
429-
430-
Content-Type: application/json
431-
Content-Length: 0
432-
433416
+ Response 400 (application/json)
434417
435418
+ Headers
@@ -446,8 +429,15 @@ Feature: Generate API Blueprint documentation from test examples
446429
447430
+ Body
448431
449-
Content-Type: application/json
450-
Content-Length: 111
432+
{
433+
"data": {
434+
"id": "1",
435+
"type": "order",
436+
"attributes": {
437+
"name": "Order 1"
438+
}
439+
}
440+
}
451441
452442
+ Response 200 (application/json)
453443

lib/rspec_api_documentation/views/api_blueprint_example.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ def parameters
2020

2121
def requests
2222
super.map do |request|
23-
if request[:request_content_type] =~ /application\/json/ && request[:request_body]
24-
request[:request_body] = JSON.pretty_generate(JSON.parse(request[:request_body]))
25-
end
23+
request[:request_body] = body_to_json(request, :request)
24+
request[:response_body] = body_to_json(request, :response)
2625

2726
request[:request_body] = indent(request[:request_body])
28-
request[:request_body] = indent(request[:request_headers_text])
29-
request[:request_body] = indent(request[:response_body])
30-
request[:request_body] = indent(request[:response_headers_text])
27+
request[:request_headers_text] = indent(request[:request_headers_text])
28+
request[:response_body] = indent(request[:response_body])
29+
request[:response_headers_text] = indent(request[:response_headers_text])
3130
request
3231
end
3332
end
@@ -45,6 +44,20 @@ def indent(string)
4544
end
4645
end
4746
end
47+
48+
# http_call: the hash that contains all information about the HTTP
49+
# request and response.
50+
# message_direction: either `request` or `response`.
51+
def body_to_json(http_call, message_direction)
52+
content_type = http_call["#{message_direction}_content_type".to_sym]
53+
body = http_call["#{message_direction}_body".to_sym] # e.g request_body
54+
55+
if content_type =~ /application\/.*json/ && body
56+
body = JSON.pretty_generate(JSON.parse(body))
57+
end
58+
59+
body
60+
end
4861
end
4962
end
5063
end

0 commit comments

Comments
 (0)