graphql-docs
graphql-docs copied to clipboard
Exception when description uses unicode character
When my GraphQL schema contains unicode characters, or their HTML entity equivalents, I'm unable to render documentation. I encountered the same error for —
, ”
and “
I get the following error:
Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
(erb):34:in `block in render'
/Users/fried/.rvm/gems/ruby-2.6.3@developer-4me-com/gems/graphql-docs-2.0.0/lib/graphql-docs/renderer.rb:48:in `render'
/Users/fried/.rvm/gems/ruby-2.6.3@developer-4me-com/gems/graphql-docs-2.0.0/lib/graphql-docs/generator.rb:225:in `write_file'
/Users/fried/.rvm/gems/ruby-2.6.3@developer-4me-com/gems/graphql-docs-2.0.0/lib/graphql-docs/generator.rb:126:in `block in create_graphql_object_pages'
/Users/fried/.rvm/gems/ruby-2.6.3@developer-4me-com/gems/graphql-docs-2.0.0/lib/graphql-docs/generator.rb:122:in `each'
/Users/fried/.rvm/gems/ruby-2.6.3@developer-4me-com/gems/graphql-docs-2.0.0/lib/graphql-docs/generator.rb:122:in `create_graphql_object_pages'
/Users/fried/.rvm/gems/ruby-2.6.3@developer-4me-com/gems/graphql-docs-2.0.0/lib/graphql-docs/generator.rb:54:in `generate'
/Users/fried/.rvm/gems/ruby-2.6.3@developer-4me-com/gems/graphql-docs-2.0.0/lib/graphql-docs.rb:52:in `build'
(Error remains the same after upgrading to 2.0.1)
Turns out the problem was the default_external encoding.
I don't have a LANG
environment variable causing Encoding.default_external
to be US-ASCII
. That did not work well with unicode characters in the GraphQL schema.
Setting Encoding.default_external = 'UTF-8'
before generating the documentation fixed the issue.
I don't believe that should that be needed, if my schema uses HTML entities only.
@fhoeben it looks like Ruby 3+ (could be earlier vers too) defaults to whatever the operating system is for encoding:
The default external encoding is initialized by the -E option. If -E isn't set, it is initialized to UTF-8 on Windows and the locale on other operating systems.
On my machine, MacOS running Ruby 3.1.1, the Encoding.default_external
is UTF-8. So it seems like this behavior is configurable on an OS-level instead. If yourself or others think that the gem should handle overriding this value, let me know and I can adjust it. But I'm going to close it for now.