-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefers the built-in JSON library from Elixir 1.18.x when it's available #650
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,7 +181,7 @@ defmodule OpenApiSpex do | |
- ensures the schema is linked to the module by "x-struct" extension property | ||
- defines a struct with keys matching the schema properties | ||
- defines a @type `t` for the struct | ||
- derives a `Jason.Encoder` and/or `Poison.Encoder` for the struct | ||
- derives a `JSON.Encoder`, `Jason.Encoder` and/or `Poison.Encoder` for the struct | ||
|
||
See `OpenApiSpex.Schema` for additional examples and details. | ||
|
||
|
@@ -238,8 +238,8 @@ defmodule OpenApiSpex do | |
- `:struct?` (boolean) - When false, prevents the automatic generation | ||
of a struct definition for the schema module. | ||
- `:derive?` (boolean) When false, prevents the automatic generation | ||
of a `@derive` call for either `Poison.Encoder` | ||
or `Jason.Encoder`. Using this option can | ||
of a `@derive` call for either `JSON.Encoder` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comma after |
||
`Poison.Encoder` or `Jason.Encoder`. Using this option can | ||
prevent "... protocol has already been consolidated ..." | ||
compiler warnings. | ||
""" | ||
|
@@ -262,7 +262,7 @@ defmodule OpenApiSpex do | |
|
||
if Map.get(@schema, :"x-struct") == __MODULE__ do | ||
if Keyword.get(unquote(opts), :derive?, true) do | ||
@derive Enum.filter([Poison.Encoder, Jason.Encoder], &Code.ensure_loaded?/1) | ||
@derive Enum.filter([JSON.Encoder, Poison.Encoder, Jason.Encoder], &Code.ensure_loaded?/1) | ||
end | ||
|
||
if Keyword.get(unquote(opts), :struct?, true) do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ defmodule OpenApiSpex.OpenApi do | |
""" | ||
@callback spec() :: t | ||
|
||
@json_encoder Enum.find([Jason, Poison], &Code.ensure_loaded?/1) | ||
@json_encoder Enum.find([JSON, Jason, Poison], &Code.ensure_loaded?/1) | ||
@yaml_encoder nil | ||
@vendor_extensions ~w( | ||
x-struct | ||
|
@@ -80,7 +80,7 @@ defmodule OpenApiSpex.OpenApi do | |
|
||
def json_encoder, do: @json_encoder | ||
|
||
for encoder <- [Poison.Encoder, Jason.Encoder] do | ||
for encoder <- [JSON.Encoder, Poison.Encoder, Jason.Encoder] do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion would be to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be but I think it's even nicer to let the user actively choose the preferred encoder. Just like Phoenix does: https://hexdocs.pm/phoenix/Phoenix.html It could well be that another hex package in a developers project already include Jason or Poison. But that will require some more effort to implement 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd advise against application configuration for the JSON encoder in open_api_spex for the following reasons:
|
||
if Code.ensure_loaded?(encoder) do | ||
defimpl encoder do | ||
def encode(api_spec = %OpenApi{}, options) do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module used to encode should be the one derived in https://github.com/open-api-spex/open_api_spex/pull/650/files#diff-80ea122ee3ae0be84e69b60bc10913073a339ec09ff0654e4805e108190754bbR265
I don't see why we should prefer the builtin over a library the user has picked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I didn’t really think this through. Sorry. I’ll move this PR back to draft or close it and will think this a bit more through.