rspec-openapi icon indicating copy to clipboard operation
rspec-openapi copied to clipboard

Getting invalid byte sequence in UTF-8 when the response is a pdf

Open dgmora opened this issue 1 year ago • 2 comments

I was giving a try to rspec-openapi and after trying I'm getting an error when returning a pdf attachment like this:

      respond_to do |format|
        format.pdf do
          # params[:content] can be a plain text string like "foo"
          pdf = Princely::Pdf.new.pdf_from_string(params[:content]) 
          # pdf is a String that looks like
          # "%PDF-1.5\n%\xE2\xE3\xCF\xD3\n\n1 0 obj\n<</Type /Catalog\n/Pages 2 0 R\n/Outlines 5 0 R>>\nendobj\n\n6 0 obj\n<</Length 16 0 R\n/Filter /FlateDecode\n/Type /ObjStm\n/N 10\n/First 63>>\nstream\nx\x9C\x8D\x91OK\...
          send_data pdf,
            type: 'application/pdf',
            filename: filename,
            disposition: 'attachment'

I get this error:

An error occurred in an `after(:suite)` hook.
Failure/Error: elsif o =~ /\n(?!\Z)/  # match \n except blank line at the end of string

ArgumentError:
  invalid byte sequence in UTF-8
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:268:in `=~'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:268:in `visit_String'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:136:in `accept'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:175:in `block in visit_Struct'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:173:in `each'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:173:in `visit_Struct'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:136:in `accept'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/visitors/yaml_tree.rb:118:in `push'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych.rb:512:in `dump'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/3.2.0/psych/core_ext.rb:13:in `to_yaml'
# /Users/david/.local/share/rtx/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-openapi-0.8.1/lib/rspec/openapi/result_recorder.rb:22:in `block (4 levels) in record_results!'

How could I work around this? I was looking how to overwrite a part of this endpoints' specification but didn't manage.

dgmora avatar Aug 25 '23 15:08 dgmora

Could you create a minimum repro repo?

exoego avatar Aug 25 '23 21:08 exoego

Sure, here it is: https://github.com/dgmora/demo Getting the same error

dgmora avatar Aug 28 '23 09:08 dgmora

This issues can be reproduced with v0.8.0, but v0.9.0 and above is fine. Please use newer version

exoego avatar Apr 12 '24 14:04 exoego