Skip to content
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

Elixir: Switch Poison to Jason #16061

Merged
merged 11 commits into from
Jul 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public class ElixirClientCodegen extends DefaultCodegen {
String supportedElixirVersion = "1.10";
List<String> extraApplications = Arrays.asList(":logger");
List<String> deps = Arrays.asList(
"{:tesla, \"~> 1.4\"}",
"{:poison, \"~> 3.0\"}",
"{:ex_doc, \"~> 0.28\", only: :dev, runtime: false}"
"{:tesla, \"~> 1.7\"}",
"{:jason, \"~> 1.4\"}",
"{:ex_doc, \"~> 0.30\", only: :dev, runtime: false}"
);

public ElixirClientCodegen() {
Expand Down Expand Up @@ -662,7 +662,7 @@ public String codeMappingKey() {
}

public String decodedStruct() {
// Let Poison decode the entire response into a generic blob
// Let Jason decode the entire response into a generic blob
if (isMap) {
return "%{}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ defmodule {{moduleName}}.Connection do

tesla_options = Application.get_env(:tesla, __MODULE__, [])
middleware = Keyword.get(tesla_options, :middleware, [])
json_engine = Keyword.get(tesla_options, :json, Poison)
json_engine = Keyword.get(tesla_options, :json, Jason)

user_agent =
Keyword.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ defmodule {{moduleName}}.Deserializer do
@spec deserialize(struct(), :atom, :atom, struct(), keyword()) :: struct()
def deserialize(model, field, :list, mod, options) do
model
|> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: [struct(mod)]]))))
|> Map.update!(field, &(Jason.Decode.decode(&1, Keyword.merge(options, [as: [struct(mod)]]))))
end

def deserialize(model, field, :struct, mod, options) do
model
|> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: struct(mod)]))))
|> Map.update!(field, &(Jason.Decode.decode(&1, Keyword.merge(options, [as: struct(mod)]))))
end

def deserialize(model, field, :map, mod, options) do
Expand All @@ -26,7 +26,7 @@ defmodule {{moduleName}}.Deserializer do
existing_value ->
Map.new(existing_value, fn
{key, val} ->
{key, Poison.Decode.decode(val, Keyword.merge(options, as: struct(mod)))}
{key, Jason.Decode.decode(val, Keyword.merge(options, as: struct(mod)))}
end)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{&description}}
"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
{{#vars}}{{#atom}}{{&baseName}}{{/atom}}{{^-last}},
{{/-last}}{{/vars}}
Expand All @@ -16,7 +16,7 @@
}
end

defimpl Poison.Decoder, for: {{&moduleName}}.Model.{{&classname}} do
defimpl Jason.Decoder, for: {{&moduleName}}.Model.{{&classname}} do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in the following error:

== Compilation error in file lib/openapi_petstore/model/_special_model_name_.ex ==
** (ArgumentError) Jason.Decoder is not a protocol
    (elixir 1.14.4) lib/protocol.ex:330: Protocol.assert_protocol!/2
    lib/openapi_petstore/model/_special_model_name_.ex:19: (file)
    (elixir 1.14.4) lib/kernel/parallel_compiler.ex:340: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{#hasComplexVars}}
import {{&moduleName}}.Deserializer
def decode(value, options) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule {{moduleName}}.RequestBuilder do
Tesla.Multipart.add_field(
multipart,
key,
Poison.encode!(value),
Jason.encode!(value),
headers: [{:"Content-Type", "application/json"}]
)
end)
Expand Down Expand Up @@ -185,5 +185,5 @@ defmodule {{moduleName}}.RequestBuilder do

defp decode(%Tesla.Env{} = env, false), do: {:ok, env}

defp decode(%Tesla.Env{body: body}, struct), do: Poison.decode(body, as: struct)
defp decode(%Tesla.Env{body: body}, struct), do: Jason.decode(body, as: struct)
end
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ defmodule OpenapiPetstore.Connection do

tesla_options = Application.get_env(:tesla, __MODULE__, [])
middleware = Keyword.get(tesla_options, :middleware, [])
json_engine = Keyword.get(tesla_options, :json, Poison)
json_engine = Keyword.get(tesla_options, :json, Jason)

user_agent =
Keyword.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ defmodule OpenapiPetstore.Deserializer do
@spec deserialize(struct(), :atom, :atom, struct(), keyword()) :: struct()
def deserialize(model, field, :list, mod, options) do
model
|> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: [struct(mod)]]))))
|> Map.update!(field, &(Jason.Decode.decode(&1, Keyword.merge(options, [as: [struct(mod)]]))))
end

def deserialize(model, field, :struct, mod, options) do
model
|> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: struct(mod)]))))
|> Map.update!(field, &(Jason.Decode.decode(&1, Keyword.merge(options, [as: struct(mod)]))))
end

def deserialize(model, field, :map, mod, options) do
Expand All @@ -28,7 +28,7 @@ defmodule OpenapiPetstore.Deserializer do
existing_value ->
Map.new(existing_value, fn
{key, val} ->
{key, Poison.Decode.decode(val, Keyword.merge(options, as: struct(mod)))}
{key, Jason.Decode.decode(val, Keyword.merge(options, as: struct(mod)))}
end)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.FooGetDefaultResponse do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:string
]
Expand All @@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.FooGetDefaultResponse do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.FooGetDefaultResponse do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.FooGetDefaultResponse do
import OpenapiPetstore.Deserializer
def decode(value, options) do
value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.SpecialModelName do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:"$special[property.name]"
]
Expand All @@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.SpecialModelName do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.SpecialModelName do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.SpecialModelName do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.AdditionalPropertiesClass do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:map_property,
:map_of_map_property
Expand All @@ -18,7 +18,7 @@ defmodule OpenapiPetstore.Model.AdditionalPropertiesClass do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.AdditionalPropertiesClass do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.AdditionalPropertiesClass do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.AllOfWithSingleRef do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:username,
:SingleRefType
Expand All @@ -18,7 +18,7 @@ defmodule OpenapiPetstore.Model.AllOfWithSingleRef do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.AllOfWithSingleRef do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.AllOfWithSingleRef do
import OpenapiPetstore.Deserializer
def decode(value, options) do
value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Animal do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:className,
:color
Expand All @@ -18,7 +18,7 @@ defmodule OpenapiPetstore.Model.Animal do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.Animal do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.Animal do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ApiResponse do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:code,
:type,
Expand All @@ -20,7 +20,7 @@ defmodule OpenapiPetstore.Model.ApiResponse do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.ApiResponse do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.ApiResponse do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ArrayOfArrayOfNumberOnly do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:ArrayArrayNumber
]
Expand All @@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.ArrayOfArrayOfNumberOnly do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.ArrayOfArrayOfNumberOnly do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.ArrayOfArrayOfNumberOnly do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ArrayOfNumberOnly do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:ArrayNumber
]
Expand All @@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.ArrayOfNumberOnly do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.ArrayOfNumberOnly do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.ArrayOfNumberOnly do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ArrayTest do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:array_of_string,
:array_array_of_integer,
Expand All @@ -20,7 +20,7 @@ defmodule OpenapiPetstore.Model.ArrayTest do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.ArrayTest do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.ArrayTest do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Capitalization do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:smallCamel,
:CapitalCamel,
Expand All @@ -26,7 +26,7 @@ defmodule OpenapiPetstore.Model.Capitalization do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.Capitalization do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.Capitalization do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Cat do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:className,
:color,
Expand All @@ -20,7 +20,7 @@ defmodule OpenapiPetstore.Model.Cat do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.Cat do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.Cat do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Category do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:id,
:name
Expand All @@ -18,7 +18,7 @@ defmodule OpenapiPetstore.Model.Category do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.Category do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.Category do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ClassModel do
Model for testing model with \"_class\" property
"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:_class
]
Expand All @@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.ClassModel do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.ClassModel do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.ClassModel do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Client do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:client
]
Expand All @@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.Client do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.Client do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.Client do
def decode(value, _options) do
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.DeprecatedObject do

"""

@derive [Poison.Encoder]
@derive [Jason.Encoder]
defstruct [
:name
]
Expand All @@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.DeprecatedObject do
}
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.DeprecatedObject do
defimpl Jason.Decoder, for: OpenapiPetstore.Model.DeprecatedObject do
def decode(value, _options) do
value
end
Expand Down
Loading
Loading