Support Elixir 1.18 JSON module as json_codec#310
Open
donleandro wants to merge 1 commit intoabsinthe-graphql:mainfrom
Open
Support Elixir 1.18 JSON module as json_codec#310donleandro wants to merge 1 commit intoabsinthe-graphql:mainfrom
donleandro wants to merge 1 commit intoabsinthe-graphql:mainfrom
Conversation
When users set `json_codec: JSON`, calls to `JSON.encode!/2` fail because the built-in JSON module expects an encoder function as its second argument, not a keyword list of options like Jason does. This introduces `safe_encode!/3` which calls `encode!/1` when opts is empty (compatible with both Jason and JSON) and only passes opts when non-empty (for codecs like Jason that support keyword options). For GraphiQL's pretty-printing (`encode!(value, pretty: true)`), a `pretty_encode!/2` helper detects whether the codec supports keyword options and falls back to plain encoding for codecs like JSON. Closes absinthe-graphql#299
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #299.
When configuring
json_codec: JSONto use Elixir 1.18's built-in JSON module, calls toJSON.encode!/2fail because the built-in module expects an encoder function as the second argument, while this library passes a keyword list of options (the convention used by Jason).This came up while upgrading to Elixir 1.18 on a project I work on (Shiko, a veterinary platform). The fix is minimal and fully backward compatible:
safe_encode!/3inAbsinthe.Plug: whenoptsis[](which is the default whenjson_codecis configured as a bare module), it callsencode!/1instead ofencode!/2. Both Jason and JSON support arity-1. When opts is non-empty, it passes them through as before, so{Jason, [pretty: true]}etc. still works.pretty_encode!/2inAbsinthe.Plug.GraphiQL: the GraphiQL interface callsencode!(value, pretty: true)for display purposes. Since the built-in JSON module does not support this keyword option, the helper checks whether the codec supports keyword-style opts and falls back to plain encoding when it does not.No changes to the public API. Jason and Poison keep working exactly as before.
Changes
lib/absinthe/plug.ex-encode/4andencode_json!/2now go throughsafe_encode!/3lib/absinthe/plug/graphiql.ex- allencode!(value, pretty: true)calls go throughpretty_encode!/2safe_encode!/3with both Jason and JSON, plus integration tests usingjson_codec: JSONfor full request/response cycles (guarded withCode.ensure_loaded?(JSON)so they only run on Elixir >= 1.18)Test plan
json_codec: JSONon Elixir 1.18.4