rspec_api_documentation: Documentation no longer include JSON response body when using with Rack >= 2.1.0
Hello,
This issue is pretty much for reporting the incompatibility to the gem author, and hopefully it will help anyone who runs into this problem to understand what’s going on.
Basically, after we upgrade our dependencies to use Rack 2.1.1, we noticed that our generated documentation no longer show JSON response but instead showing [binary data]
instead.
Digging in further, we found out that in https://github.com/rack/rack/commit/8c62821f4a464858a6b6ca3c3966ec308d2bb53e, especially this change, MockResponse#body
now creates a buffer and use <<
to join the content together. However, on line 195, the author uses String.new
without specifying the encoding, resulting in Ruby creating a new String with ASCII-8BIT
encoding by default.
As it turns out, rspec_api_documentation relies on string encoding to determine if it should include the response body in the documentation or not:
Hence, the change in Rack broke this conditional.
I’ve reported this issue to Rack in https://github.com/rack/rack/pull/1486, and hopefully we can solve this soon.
The solution right now for us is to lock Rack to ~> 2.0.8
for now.
Thank you very much.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 38
- Comments: 16 (4 by maintainers)
Commits related to this issue
- fix https://github.com/zipmark/rspec_api_documentation/issues/456 — committed to jrg-team/rspec_api_documentation by FrankFang 4 years ago
- Update client_base.rb fixed issues https://github.com/zipmark/rspec_api_documentation/issues/456 — committed to FredaFei/rspec_api_documentation by FredaFei 4 years ago
- fix zipmark#456 — committed to yyzclyang/rspec_api_documentation by yyzclyang 4 years ago
I ran into the same issue recently. I don’t really like this solution, but I fixed it using mokey patching:
config/initializers/rspec_api_documentation.rb
Indeed, it seems to be caused by an encoding issue where utf-8 become ascii-8bit
There’s an additional issue with endpoints that use
send_data
:I’ve tweaked Tao’s fix, it ain’t perfect but it works for my projects:
It worked for me. Thank you so much
On my side, I fixed it by two mechanisms :
response_body_formatter
like it is intended to filter the different kind of output and make it OK forapitome
Resulting in this
spec/support/rspec_api_documentation.rb
file :I did a slight variation on this one so I could get the responses to show up as pretty printed:
@incubus Note the change in #458 does not, in itself alter the gem’s behavior. If you use the default
response_body_formatter
, then you will experience the same problem as before.To solve your problem, please:
master
branch (at least until there’s a new release).response_body_formatter
in your configuration, like so:Note that the above, compared with the new default (see below) introduced in #458 would stop filtering out potentially binary data.
https://github.com/zipmark/rspec_api_documentation/blob/d3892cc7388460a98476734963face5a7a2ac158/lib/rspec_api_documentation/configuration.rb#L123
As the test
response_body.encoding == Encoding::ASCII_8BIT
has become unreliable to filter binary data, it will be under your responsibility to figure out, in your context, what you can check to determine whether you want to display a given response or not.Here’s a more complex example:
Notes:
This is the old check, still unreliable, but because it happens after checking for JSON or PNG, then you would be able to display those two properly. You might lose the display of very plain text (the
else
part) if Rack returns everything asASCII_8BIT
(same problem as before).To alleviate the above, you might want to return a better string than just
[binary?]
if you aren’t sure your test is accurate. You could for example return something like this:This would look like this:
[binary?]
ˇÿˇ‡�JFIF���`�`��ˇ·�ÄExif��MM�*������������������J�������R(�������ái�������Z�������`������`����†�������†�����������ˇ· !http://ns.adobe.com/xap/1.0/�<?xpacket begin="Ôªø" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 5.4.0"> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about=""/> </rdf:RDF> </x:xmpmeta> <?xpacket end="w"?>�ˇÌ�8Photoshop 3.0�8BIM������8BIM%�����‘åŸè�≤ÈÄ òϯB~ˇ‚ËICC_PROFILE���ÿappl ��mntrRGB XYZ Ÿ�����acspAPPL����appl������������������ˆ÷�����”-appl�����������������������������������������������desc�����odscm��x��úcprt�����8wtpt��L���rXYZ��`���gXYZ��t���bXYZ��à���rTRC��ú���chad��¨���,bTRC��ú���gTRC��ú���desc�������Generic RGB Profile�����������Generic RGB Profile��������������������������������������������������mluc����������skSK���(��ÑdaDK���.��¨caES���$��⁄viVN���$��˛ptBR���&��"ukUA���*��HfrFU���(��rhuHU���(��özhTW�����¬nbNO���&��ÿcsCZ���"��˛heIL����� itIT���(��>roRO���$��fdeDE���,��äkoKR�����∂svSE���&��ÿzhCN�����ÃjaJP�����‚elGR���"��¸ptPO���&��nlNL���(��DesES���&��thTH���$��ltrTR���"��êfiFI���(��≤hrHR���(��⁄plPL���,��ruRU���"��.arEG���&��PenUS���&��v�Va�e�o�b�e�c�n�˝� �R�G�B� �p�r�o�f�i�l�G�e�n�e�r�e�l� �R�G�B�-�b�e�s�k�r�i�v�e�l�s�e�P�e�r�f�i�l� �R�G�B� �g�e�n�Ë�r�i�c�C•�u� �h�Ï�n�h� �R�G�B� �C�h�u�n�g�P�e�r�f�i�l� �R�G�B� �G�e�n�È�r�i�c�o030;L=89� ?@>D09;� �R�G�B�P�r�o�f�i�l� �g�È�n�È�r�i�q�u�e� �R�V�B�¡�l�t�a�l�·�n�o�s� �R�G�B� �p�r�o�f�i�lêu(� �R�G�B� Çr_icœè�G�e�n�e�r�i�s�k� �R�G�B�-�p�r�o�f�i�l�O�b�e�c�n�˝� �R�G�B� �p�r�o�f�i�l‰Ë’‰Ÿ‹� �R�G�B� €‹‹Ÿ�P�r�o�f�i�l�o� �R�G�B� �g�e�n�e�r�i�c�o�P�r�o�f�i�l� �R�G�B� �g�e�n�e�r�i�c�A�l�l�g�e�m�e�i�n�e�s� �R�G�B�-�P�r�o�f�i�l«|º� �R�G�B� ’∏\”«|fnê� �R�G�B� cœèeáNˆN�Ç,� �R�G�B� 0◊0Ì0’0°0§0ÎìµΩπ∫Ã� ¿¡ø∆ت� �R�G�B�P�e�r�f�i�l� �R�G�B� �g�e�n�È�r�i�c�o�A�l�g�e�m�e�e�n� �R�G�B�-�p�r�o�f�i�e�lB#D%L� �R�G�B� 1H'D�G�e�n�e�l� �R�G�B� �P�r�o�f�i�l�i�Y�l�e�i�n�e�n� �R�G�B�-�p�r�o�f�i�i�l�i�G�e�n�e�r�i �k�i� �R�G�B� �p�r�o�f�i�l�U�n�i�w�e�r�s�a�l�n�y� �p�r�o�f�i�l� �R�G�B1I89� ?@>D8;L� �R�G�BEDA� *91JA� �R�G�B� 'D9'E�G�e�n�e�r�i�c� �R�G�B� �P�r�o�f�i�l�etext����Copyright 2007 Apple Inc., all rights reserved.�XYZ ������ÛR����œXYZ ������tM��=Ó��–XYZ ������Zu��¨s��4XYZ ������(��ü��∏6curv�������Õ��sf32�����B��fiˇˇÛ&��í��˝ëˇˇ˚¢ˇˇ˝£��‹��¿lˇ¿���"�ˇƒ����������� ˇƒ�µ���}�!1AQa"q2Åë°#B±¡R—$3brÇ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzÉÑÖÜáàâäíìîïñóòôö¢£§•¶ß®©™≤≥¥µ∂∑∏π∫¬√ƒ≈∆«»… “”‘’÷◊ÿŸ⁄·‚„‰ÂÊÁËÈÍÒÚÛÙıˆ˜¯˘˙ˇƒ�������� ˇƒ�µ��w�!1AQaq"2ÅBë°±¡ #3Rbr— $4·%Ò&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzÇÉÑÖÜáàâäíìîïñóòôö¢£§•¶ß®©™≤≥¥µ∂∑∏π∫¬√ƒ≈∆«»… “”‘’÷◊ÿŸ⁄‚„‰ÂÊÁËÈÍÚÛÙıˆ˜¯˘˙ˇ€�C�00D000D\DDDD\t\\\\\tåttttttåååååååå®®®®®®ƒƒƒƒƒ‹‹‹‹‹‹‹‹‹‹ˇ€�C"$$848`44`ÊúÄúÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊˇ›��ˇ⁄���?�È(¢ä�ˇŸFor anyone following this, Ive merged #458 please let me know if this resolves the issue for you. cc @artofhuman
Just created a new rails 6 app and had this problem, Tao’s response above fixed it (https://github.com/zipmark/rspec_api_documentation/issues/456#issuecomment-597671587)