diff --git a/docs/generators/elixir.md b/docs/generators/elixir.md index 693e433b920..38d89cba8df 100644 --- a/docs/generators/elixir.md +++ b/docs/generators/elixir.md @@ -48,7 +48,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
  • AnyType
  • Atom
  • Boolean
  • -
  • DateTime
  • Decimal
  • Float
  • Integer
  • diff --git a/flake.nix b/flake.nix index ccc7357dbb6..dcafe3056c8 100644 --- a/flake.nix +++ b/flake.nix @@ -13,7 +13,7 @@ devShells.default = pkgs.mkShell { buildInputs = with pkgs;[ - jdk8 + jdk11 maven ]; }; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java index 4d8e7d54644..8bf83f7362c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElixirClientCodegen.java @@ -59,9 +59,10 @@ public class ElixirClientCodegen extends DefaultCodegen { String supportedElixirVersion = "1.10"; List extraApplications = Arrays.asList(":logger"); List 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}", + "{:dialyxir, \"~> 1.3\", only: [:dev, :test], runtime: false}" ); public ElixirClientCodegen() { @@ -194,7 +195,6 @@ public class ElixirClientCodegen extends DefaultCodegen { "AnyType", "Tuple", "PID", - "DateTime", "map()", // This is a workaround, since the DefaultCodeGen uses our elixir TypeSpec datetype to evaluate the primitive "any()" ) @@ -210,7 +210,7 @@ public class ElixirClientCodegen extends DefaultCodegen { typeMapping.put("string", "String"); typeMapping.put("byte", "Integer"); typeMapping.put("boolean", "Boolean"); - typeMapping.put("Date", "DateTime"); + typeMapping.put("Date", "Date"); typeMapping.put("DateTime", "DateTime"); typeMapping.put("file", "String"); typeMapping.put("map", "Map"); @@ -575,7 +575,12 @@ public class ElixirClientCodegen extends DefaultCodegen { } else if (ModelUtils.isBooleanSchema(p)) { return "boolean()"; } else if (!StringUtils.isEmpty(p.get$ref())) { - return this.moduleName + ".Model." + super.getTypeDeclaration(p) + ".t"; + switch (super.getTypeDeclaration(p)) { + case "String": + return "String.t"; + default: + return this.moduleName + ".Model." + super.getTypeDeclaration(p) + ".t"; + } } else if (ModelUtils.isFileSchema(p)) { return "String.t"; } else if (ModelUtils.isStringSchema(p)) { @@ -662,28 +667,23 @@ public class ElixirClientCodegen extends DefaultCodegen { } 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 "%{}"; } + // Primitive return type, don't even try to decode if (baseType == null || (containerType == null && primitiveType)) { return "false"; } else if (isArray && languageSpecificPrimitives().contains(baseType)) { return "[]"; } + StringBuilder sb = new StringBuilder(); - if (isArray) { - sb.append("["); - } - sb.append("%"); sb.append(moduleName); sb.append(".Model."); sb.append(baseType); - sb.append("{}"); - if (isArray) { - sb.append("]"); - } + return sb.toString(); } @@ -768,6 +768,24 @@ public class ElixirClientCodegen extends DefaultCodegen { this.replacedPathName = replacedPathName; } + private void translateBaseType(StringBuilder returnEntry, String baseType) { + switch (baseType) { + case "AnyType": + returnEntry.append("any()"); + break; + case "Boolean": + returnEntry.append("boolean()"); + break; + case "Float": + returnEntry.append("float()"); + break; + default: + returnEntry.append(baseType); + returnEntry.append(".t"); + break; + } + } + public String typespec() { StringBuilder sb = new StringBuilder("@spec "); sb.append(underscore(operationId)); @@ -793,12 +811,7 @@ public class ElixirClientCodegen extends DefaultCodegen { returnEntry.append(".Model."); } - if (exResponse.baseType.equals("AnyType")) { - returnEntry.append("any()"); - }else { - returnEntry.append(exResponse.baseType); - returnEntry.append(".t"); - } + translateBaseType(returnEntry, exResponse.baseType); } else { if (exResponse.containerType.equals("array") || exResponse.containerType.equals("set")) { @@ -808,12 +821,8 @@ public class ElixirClientCodegen extends DefaultCodegen { returnEntry.append(".Model."); } - if (exResponse.baseType.equals("AnyType")) { - returnEntry.append("any())"); - }else { - returnEntry.append(exResponse.baseType); - returnEntry.append(".t)"); - } + translateBaseType(returnEntry, exResponse.baseType); + returnEntry.append(")"); } else if (exResponse.containerType.equals("map")) { returnEntry.append("map()"); } diff --git a/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache b/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache index 19f863fbe15..f51e2137e28 100644 --- a/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache +++ b/modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache @@ -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( diff --git a/modules/openapi-generator/src/main/resources/elixir/deserializer.ex.mustache b/modules/openapi-generator/src/main/resources/elixir/deserializer.ex.mustache index a5c4da88658..2ea317b4dca 100644 --- a/modules/openapi-generator/src/main/resources/elixir/deserializer.ex.mustache +++ b/modules/openapi-generator/src/main/resources/elixir/deserializer.ex.mustache @@ -4,37 +4,81 @@ defmodule {{moduleName}}.Deserializer do Helper functions for deserializing responses into models """ + @jason_decode_opts [keys: :strings] + + def jason_decode(json) do + Jason.decode(json, @jason_decode_opts) + end + + def jason_decode(json, module) do + json + |> jason_decode() + |> case do + {:ok, decoded} -> {:ok, to_struct(decoded, module)} + {:error, _} = error -> error + end + end + @doc """ Update the provided model with a deserialization of a nested value """ - @spec deserialize(struct(), :atom, :atom, struct(), keyword()) :: struct() - def deserialize(model, field, :list, mod, options) do + @spec deserialize(struct(), atom(), :date | :datetime | :list | :map | :struct, module()) :: + struct() + def deserialize(model, field, :list, module) do model - |> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: [struct(mod)]])))) + |> Map.update!(field, fn + nil -> + nil + + list -> + Enum.map(list, &to_struct(&1, module)) + end) end - def deserialize(model, field, :struct, mod, options) do + def deserialize(model, field, :struct, module) do model - |> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: struct(mod)])))) + |> Map.update!(field, fn + nil -> + nil + + value -> + to_struct(value, module) + end) end - def deserialize(model, field, :map, mod, options) do + def deserialize(model, field, :map, module) do maybe_transform_map = fn nil -> nil existing_value -> Map.new(existing_value, fn - {key, val} -> - {key, Poison.Decode.decode(val, Keyword.merge(options, as: struct(mod)))} + {key, value} -> + {key, to_struct(value, module)} end) end Map.update!(model, field, maybe_transform_map) end - def deserialize(model, field, :date, _, _options) do + def deserialize(model, field, :date, _) do value = Map.get(model, field) + + case is_binary(value) do + true -> + case Date.from_iso8601(value) do + {:ok, date} -> Map.put(model, field, date) + _ -> model + end + + false -> + model + end + end + + def deserialize(model, field, :datetime, _) do + value = Map.get(model, field) + case is_binary(value) do true -> case DateTime.from_iso8601(value) do @@ -46,4 +90,23 @@ defmodule {{moduleName}}.Deserializer do model end end + + defp to_struct(map_or_list, module) + defp to_struct(nil, _), do: nil + + defp to_struct(list, module) when is_list(list) and is_atom(module) do + Enum.map(list, &to_struct(&1, module)) + end + + defp to_struct(map, module) when is_map(map) and is_atom(module) do + model = struct(module) + + model + |> Map.keys() + |> List.delete(:__struct__) + |> Enum.reduce(model, fn field, acc -> + Map.replace(acc, field, Map.get(map, Atom.to_string(field))) + end) + |> module.decode() + end end diff --git a/modules/openapi-generator/src/main/resources/elixir/mix.exs.mustache b/modules/openapi-generator/src/main/resources/elixir/mix.exs.mustache index 71b8143b9c3..712c2fff3ed 100644 --- a/modules/openapi-generator/src/main/resources/elixir/mix.exs.mustache +++ b/modules/openapi-generator/src/main/resources/elixir/mix.exs.mustache @@ -9,7 +9,9 @@ defmodule {{moduleName}}.Mixfile do build_embedded: Mix.env() == :prod, start_permanent: Mix.env() == :prod, package: package(), - description: "{{appDescription}}", + description: """ + {{appDescription}} + """, deps: deps() ] end diff --git a/modules/openapi-generator/src/main/resources/elixir/model.mustache b/modules/openapi-generator/src/main/resources/elixir/model.mustache index 0f9cc57a893..e2dc0facf91 100644 --- a/modules/openapi-generator/src/main/resources/elixir/model.mustache +++ b/modules/openapi-generator/src/main/resources/elixir/model.mustache @@ -4,7 +4,7 @@ {{&description}} """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ {{#vars}}{{#atom}}{{&baseName}}{{/atom}}{{^-last}}, {{/-last}}{{/vars}} @@ -14,22 +14,21 @@ {{#vars}}{{#atom}}{{&baseName}}{{/atom}} => {{{datatype}}}{{#isNullable}} | nil{{/isNullable}}{{^isNullable}}{{^required}} | nil{{/required}}{{/isNullable}}{{^-last}}, {{/-last}}{{/vars}} } -end -defimpl Poison.Decoder, for: {{&moduleName}}.Model.{{&classname}} do {{#hasComplexVars}} - import {{&moduleName}}.Deserializer - def decode(value, options) do + alias {{&moduleName}}.Deserializer + + def decode(value) do value {{#vars}} {{^isPrimitiveType}} - {{#baseType}}|> deserialize({{#atom}}{{&baseName}}{{/atom}}, {{#isArray}}:list, {{&moduleName}}.Model.{{{items.baseType}}}{{/isArray}}{{#isMap}}:map, {{&moduleName}}.Model.{{{items.baseType}}}{{/isMap}}{{#isDate}}:date, nil{{/isDate}}{{#isDateTime}}:date, nil{{/isDateTime}}{{^isDate}}{{^isDateTime}}{{^isMap}}{{^isArray}}:struct, {{moduleName}}.Model.{{baseType}}{{/isArray}}{{/isMap}}{{/isDateTime}}{{/isDate}}, options) + {{#baseType}} |> Deserializer.deserialize({{#atom}}{{&baseName}}{{/atom}}, {{#isArray}}:list, {{&moduleName}}.Model.{{{items.baseType}}}{{/isArray}}{{#isMap}}:map, {{&moduleName}}.Model.{{{items.baseType}}}{{/isMap}}{{#isDate}}:date, nil{{/isDate}}{{#isDateTime}}:datetime, nil{{/isDateTime}}{{^isDate}}{{^isDateTime}}{{^isMap}}{{^isArray}}:struct, {{moduleName}}.Model.{{baseType}}{{/isArray}}{{/isMap}}{{/isDateTime}}{{/isDate}}) {{/baseType}} {{/isPrimitiveType}} {{/vars}} {{/hasComplexVars}} {{^hasComplexVars}} - def decode(value, _options) do + def decode(value) do value {{/hasComplexVars}} end diff --git a/modules/openapi-generator/src/main/resources/elixir/request_builder.ex.mustache b/modules/openapi-generator/src/main/resources/elixir/request_builder.ex.mustache index 6da43f8eddc..9adcc14e005 100644 --- a/modules/openapi-generator/src/main/resources/elixir/request_builder.ex.mustache +++ b/modules/openapi-generator/src/main/resources/elixir/request_builder.ex.mustache @@ -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) @@ -146,8 +146,8 @@ defmodule {{moduleName}}.RequestBuilder do Map.put_new(request, :body, "") end - @type status_code :: 100..599 - @type response_mapping :: [{status_code, struct() | false}] + @type status_code :: :default | 100..599 + @type response_mapping :: [{status_code, false | %{} | module()}] @doc """ Evaluate the response from a Tesla request. @@ -185,5 +185,11 @@ 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}, %{}) do + {{moduleName}}.Deserializer.jason_decode(body) + end + + defp decode(%Tesla.Env{body: body}, module) do + {{moduleName}}.Deserializer.jason_decode(body, module) + end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/api/another_fake.ex b/samples/client/petstore/elixir/lib/openapi_petstore/api/another_fake.ex index 75f9d2df76f..8a4b1ab9836 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/api/another_fake.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/api/another_fake.ex @@ -36,7 +36,7 @@ defmodule OpenapiPetstore.Api.AnotherFake do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.Client{}} + {200, OpenapiPetstore.Model.Client} ]) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/api/default.ex b/samples/client/petstore/elixir/lib/openapi_petstore/api/default.ex index 4aaaf57307b..e0cfe5c1ecc 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/api/default.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/api/default.ex @@ -32,7 +32,7 @@ defmodule OpenapiPetstore.Api.Default do connection |> Connection.request(request) |> evaluate_response([ - {:default, %OpenapiPetstore.Model.FooGetDefaultResponse{}} + {:default, OpenapiPetstore.Model.FooGetDefaultResponse} ]) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/api/fake.ex b/samples/client/petstore/elixir/lib/openapi_petstore/api/fake.ex index fc5c094fcd4..d4048e64db7 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/api/fake.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/api/fake.ex @@ -33,7 +33,7 @@ defmodule OpenapiPetstore.Api.Fake do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.FakeBigDecimalMap200Response{}} + {200, OpenapiPetstore.Model.FakeBigDecimalMap200Response} ]) end @@ -61,7 +61,7 @@ defmodule OpenapiPetstore.Api.Fake do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.HealthCheckResult{}} + {200, OpenapiPetstore.Model.HealthCheckResult} ]) end @@ -117,7 +117,7 @@ defmodule OpenapiPetstore.Api.Fake do - `{:ok, boolean()}` on success - `{:error, Tesla.Env.t}` on failure """ - @spec fake_outer_boolean_serialize(Tesla.Env.client, keyword()) :: {:ok, Boolean.t} | {:error, Tesla.Env.t} + @spec fake_outer_boolean_serialize(Tesla.Env.client, keyword()) :: {:ok, boolean()} | {:error, Tesla.Env.t} def fake_outer_boolean_serialize(connection, opts \\ []) do optional_params = %{ :body => :body @@ -169,7 +169,7 @@ defmodule OpenapiPetstore.Api.Fake do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.OuterComposite{}} + {200, OpenapiPetstore.Model.OuterComposite} ]) end @@ -187,7 +187,7 @@ defmodule OpenapiPetstore.Api.Fake do - `{:ok, float()}` on success - `{:error, Tesla.Env.t}` on failure """ - @spec fake_outer_number_serialize(Tesla.Env.client, keyword()) :: {:ok, Float.t} | {:error, Tesla.Env.t} + @spec fake_outer_number_serialize(Tesla.Env.client, keyword()) :: {:ok, float()} | {:error, Tesla.Env.t} def fake_outer_number_serialize(connection, opts \\ []) do optional_params = %{ :body => :body @@ -269,7 +269,7 @@ defmodule OpenapiPetstore.Api.Fake do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.OuterObjectWithEnumProperty{}} + {200, OpenapiPetstore.Model.OuterObjectWithEnumProperty} ]) end @@ -391,7 +391,7 @@ defmodule OpenapiPetstore.Api.Fake do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.Client{}} + {200, OpenapiPetstore.Model.Client} ]) end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/api/fake_classname_tags123.ex b/samples/client/petstore/elixir/lib/openapi_petstore/api/fake_classname_tags123.ex index 7306d63b5a4..23287818019 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/api/fake_classname_tags123.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/api/fake_classname_tags123.ex @@ -36,7 +36,7 @@ defmodule OpenapiPetstore.Api.FakeClassnameTags123 do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.Client{}} + {200, OpenapiPetstore.Model.Client} ]) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/api/pet.ex b/samples/client/petstore/elixir/lib/openapi_petstore/api/pet.ex index 73a04e6da5f..4b2c878d184 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/api/pet.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/api/pet.ex @@ -105,7 +105,7 @@ defmodule OpenapiPetstore.Api.Pet do connection |> Connection.request(request) |> evaluate_response([ - {200, [%OpenapiPetstore.Model.Pet{}]}, + {200, OpenapiPetstore.Model.Pet}, {400, false} ]) end @@ -137,7 +137,7 @@ defmodule OpenapiPetstore.Api.Pet do connection |> Connection.request(request) |> evaluate_response([ - {200, [%OpenapiPetstore.Model.Pet{}]}, + {200, OpenapiPetstore.Model.Pet}, {400, false} ]) end @@ -168,7 +168,7 @@ defmodule OpenapiPetstore.Api.Pet do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.Pet{}}, + {200, OpenapiPetstore.Model.Pet}, {400, false}, {404, false} ]) @@ -283,7 +283,7 @@ defmodule OpenapiPetstore.Api.Pet do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.ApiResponse{}} + {200, OpenapiPetstore.Model.ApiResponse} ]) end @@ -321,7 +321,7 @@ defmodule OpenapiPetstore.Api.Pet do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.ApiResponse{}} + {200, OpenapiPetstore.Model.ApiResponse} ]) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/api/store.ex b/samples/client/petstore/elixir/lib/openapi_petstore/api/store.ex index a9f67151ead..bbabd1b9ad1 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/api/store.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/api/store.ex @@ -95,7 +95,7 @@ defmodule OpenapiPetstore.Api.Store do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.Order{}}, + {200, OpenapiPetstore.Model.Order}, {400, false}, {404, false} ]) @@ -128,7 +128,7 @@ defmodule OpenapiPetstore.Api.Store do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.Order{}}, + {200, OpenapiPetstore.Model.Order}, {400, false} ]) end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/api/user.ex b/samples/client/petstore/elixir/lib/openapi_petstore/api/user.ex index 46a20a5e39e..effe8f434bb 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/api/user.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/api/user.ex @@ -159,7 +159,7 @@ defmodule OpenapiPetstore.Api.User do connection |> Connection.request(request) |> evaluate_response([ - {200, %OpenapiPetstore.Model.User{}}, + {200, OpenapiPetstore.Model.User}, {400, false}, {404, false} ]) diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex b/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex index 7f5a92db98f..6b5ee2549ce 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/connection.ex @@ -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( diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/deserializer.ex b/samples/client/petstore/elixir/lib/openapi_petstore/deserializer.ex index c96c04796bd..7ad37e79c64 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/deserializer.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/deserializer.ex @@ -6,37 +6,81 @@ defmodule OpenapiPetstore.Deserializer do Helper functions for deserializing responses into models """ + @jason_decode_opts [keys: :strings] + + def jason_decode(json) do + Jason.decode(json, @jason_decode_opts) + end + + def jason_decode(json, module) do + json + |> jason_decode() + |> case do + {:ok, decoded} -> {:ok, to_struct(decoded, module)} + {:error, _} = error -> error + end + end + @doc """ Update the provided model with a deserialization of a nested value """ - @spec deserialize(struct(), :atom, :atom, struct(), keyword()) :: struct() - def deserialize(model, field, :list, mod, options) do + @spec deserialize(struct(), atom(), :date | :datetime | :list | :map | :struct, module()) :: + struct() + def deserialize(model, field, :list, module) do model - |> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: [struct(mod)]])))) + |> Map.update!(field, fn + nil -> + nil + + list -> + Enum.map(list, &to_struct(&1, module)) + end) end - def deserialize(model, field, :struct, mod, options) do + def deserialize(model, field, :struct, module) do model - |> Map.update!(field, &(Poison.Decode.decode(&1, Keyword.merge(options, [as: struct(mod)])))) + |> Map.update!(field, fn + nil -> + nil + + value -> + to_struct(value, module) + end) end - def deserialize(model, field, :map, mod, options) do + def deserialize(model, field, :map, module) do maybe_transform_map = fn nil -> nil existing_value -> Map.new(existing_value, fn - {key, val} -> - {key, Poison.Decode.decode(val, Keyword.merge(options, as: struct(mod)))} + {key, value} -> + {key, to_struct(value, module)} end) end Map.update!(model, field, maybe_transform_map) end - def deserialize(model, field, :date, _, _options) do + def deserialize(model, field, :date, _) do value = Map.get(model, field) + + case is_binary(value) do + true -> + case Date.from_iso8601(value) do + {:ok, date} -> Map.put(model, field, date) + _ -> model + end + + false -> + model + end + end + + def deserialize(model, field, :datetime, _) do + value = Map.get(model, field) + case is_binary(value) do true -> case DateTime.from_iso8601(value) do @@ -48,4 +92,23 @@ defmodule OpenapiPetstore.Deserializer do model end end + + defp to_struct(map_or_list, module) + defp to_struct(nil, _), do: nil + + defp to_struct(list, module) when is_list(list) and is_atom(module) do + Enum.map(list, &to_struct(&1, module)) + end + + defp to_struct(map, module) when is_map(map) and is_atom(module) do + model = struct(module) + + model + |> Map.keys() + |> List.delete(:__struct__) + |> Enum.reduce(model, fn field, acc -> + Map.replace(acc, field, Map.get(map, Atom.to_string(field))) + end) + |> module.decode() + end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/_foo_get_default_response.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/_foo_get_default_response.ex index 3175497128b..115443207f6 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/_foo_get_default_response.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/_foo_get_default_response.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.FooGetDefaultResponse do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :string ] @@ -14,13 +14,12 @@ defmodule OpenapiPetstore.Model.FooGetDefaultResponse do @type t :: %__MODULE__{ :string => OpenapiPetstore.Model.Foo.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.FooGetDefaultResponse do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:string, :struct, OpenapiPetstore.Model.Foo, options) + |> Deserializer.deserialize(:string, :struct, OpenapiPetstore.Model.Foo) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/_special_model_name_.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/_special_model_name_.ex index 2aa6e95a31a..b4a013243a9 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/_special_model_name_.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/_special_model_name_.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.SpecialModelName do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :"$special[property.name]" ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.SpecialModelName do @type t :: %__MODULE__{ :"$special[property.name]" => integer() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.SpecialModelName do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/additional_properties_class.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/additional_properties_class.ex index 6c67590c4e5..0c73fb1f163 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/additional_properties_class.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/additional_properties_class.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.AdditionalPropertiesClass do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :map_property, :map_of_map_property @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.AdditionalPropertiesClass do :map_property => %{optional(String.t) => String.t} | nil, :map_of_map_property => %{optional(String.t) => %{optional(String.t) => String.t}} | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.AdditionalPropertiesClass do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/all_of_with_single_ref.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/all_of_with_single_ref.ex index 0d99c2eea02..90936b5dbb0 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/all_of_with_single_ref.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/all_of_with_single_ref.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.AllOfWithSingleRef do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :username, :SingleRefType @@ -16,13 +16,12 @@ defmodule OpenapiPetstore.Model.AllOfWithSingleRef do :username => String.t | nil, :SingleRefType => OpenapiPetstore.Model.SingleRefType.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.AllOfWithSingleRef do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:SingleRefType, :struct, OpenapiPetstore.Model.SingleRefType, options) + |> Deserializer.deserialize(:SingleRefType, :struct, OpenapiPetstore.Model.SingleRefType) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/animal.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/animal.ex index 8ee084d8432..8a820bc569e 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/animal.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/animal.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Animal do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :className, :color @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.Animal do :className => String.t, :color => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Animal do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/api_response.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/api_response.ex index 198a3be2735..23610c786bb 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/api_response.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/api_response.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ApiResponse do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :code, :type, @@ -18,10 +18,8 @@ defmodule OpenapiPetstore.Model.ApiResponse do :type => String.t | nil, :message => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.ApiResponse do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_array_of_number_only.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_array_of_number_only.ex index 6f6d4118882..5c80aa224b5 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_array_of_number_only.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_array_of_number_only.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ArrayOfArrayOfNumberOnly do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :ArrayArrayNumber ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.ArrayOfArrayOfNumberOnly do @type t :: %__MODULE__{ :ArrayArrayNumber => [[float()]] | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.ArrayOfArrayOfNumberOnly do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_number_only.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_number_only.ex index cd02092082d..f739b8c1d94 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_number_only.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/array_of_number_only.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ArrayOfNumberOnly do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :ArrayNumber ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.ArrayOfNumberOnly do @type t :: %__MODULE__{ :ArrayNumber => [float()] | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.ArrayOfNumberOnly do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/array_test.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/array_test.ex index bedaef3fcf0..c4d8fbacc16 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/array_test.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/array_test.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ArrayTest do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :array_of_string, :array_array_of_integer, @@ -18,10 +18,8 @@ defmodule OpenapiPetstore.Model.ArrayTest do :array_array_of_integer => [[integer()]] | nil, :array_array_of_model => [[OpenapiPetstore.Model.ReadOnlyFirst.t]] | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.ArrayTest do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/capitalization.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/capitalization.ex index 20e514b7b4a..fb1ee017bed 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/capitalization.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/capitalization.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Capitalization do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :smallCamel, :CapitalCamel, @@ -24,10 +24,8 @@ defmodule OpenapiPetstore.Model.Capitalization do :SCA_ETH_Flow_Points => String.t | nil, :ATT_NAME => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Capitalization do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/cat.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/cat.ex index 8994709724d..798b2dfc381 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/cat.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/cat.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Cat do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :className, :color, @@ -18,10 +18,8 @@ defmodule OpenapiPetstore.Model.Cat do :color => String.t | nil, :declawed => boolean() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Cat do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/category.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/category.ex index 52c8d80034d..87a24b77f27 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/category.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/category.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Category do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :id, :name @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.Category do :id => integer() | nil, :name => String.t } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Category do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/class_model.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/class_model.ex index f3fe3f77596..5a2db9744a6 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/class_model.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/class_model.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ClassModel do Model for testing model with \"_class\" property """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :_class ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.ClassModel do @type t :: %__MODULE__{ :_class => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.ClassModel do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/client.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/client.ex index 0721d1ad360..0b9204a7e79 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/client.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/client.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Client do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :client ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.Client do @type t :: %__MODULE__{ :client => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Client do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/deprecated_object.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/deprecated_object.ex index a1b3458e385..efbb7eacea8 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/deprecated_object.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/deprecated_object.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.DeprecatedObject do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :name ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.DeprecatedObject do @type t :: %__MODULE__{ :name => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.DeprecatedObject do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/dog.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/dog.ex index c2e250ef449..a9c06cc6fe0 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/dog.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/dog.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Dog do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :className, :color, @@ -18,10 +18,8 @@ defmodule OpenapiPetstore.Model.Dog do :color => String.t | nil, :breed => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Dog do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_arrays.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_arrays.ex index 91d2b467884..2ad3cd5338d 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_arrays.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_arrays.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.EnumArrays do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :just_symbol, :array_enum @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.EnumArrays do :just_symbol => String.t | nil, :array_enum => [String.t] | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.EnumArrays do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_class.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_class.ex index 7bd5b6a1c61..d93a31cbefc 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_class.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_class.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.EnumClass do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.EnumClass do @type t :: %__MODULE__{ } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.EnumClass do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_test.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_test.ex index 8d8ea27dcfb..f7788231bc9 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_test.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/enum_test.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.EnumTest do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :enum_string, :enum_string_required, @@ -28,16 +28,15 @@ defmodule OpenapiPetstore.Model.EnumTest do :outerEnumDefaultValue => OpenapiPetstore.Model.OuterEnumDefaultValue.t | nil, :outerEnumIntegerDefaultValue => OpenapiPetstore.Model.OuterEnumIntegerDefaultValue.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.EnumTest do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:outerEnum, :struct, OpenapiPetstore.Model.OuterEnum, options) - |> deserialize(:outerEnumInteger, :struct, OpenapiPetstore.Model.OuterEnumInteger, options) - |> deserialize(:outerEnumDefaultValue, :struct, OpenapiPetstore.Model.OuterEnumDefaultValue, options) - |> deserialize(:outerEnumIntegerDefaultValue, :struct, OpenapiPetstore.Model.OuterEnumIntegerDefaultValue, options) + |> Deserializer.deserialize(:outerEnum, :struct, OpenapiPetstore.Model.OuterEnum) + |> Deserializer.deserialize(:outerEnumInteger, :struct, OpenapiPetstore.Model.OuterEnumInteger) + |> Deserializer.deserialize(:outerEnumDefaultValue, :struct, OpenapiPetstore.Model.OuterEnumDefaultValue) + |> Deserializer.deserialize(:outerEnumIntegerDefaultValue, :struct, OpenapiPetstore.Model.OuterEnumIntegerDefaultValue) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/fake_big_decimal_map_200_response.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/fake_big_decimal_map_200_response.ex index f18eb2f53bf..53e7e65713e 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/fake_big_decimal_map_200_response.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/fake_big_decimal_map_200_response.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.FakeBigDecimalMap200Response do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :someId, :someMap @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.FakeBigDecimalMap200Response do :someId => float() | nil, :someMap => %{optional(String.t) => float()} | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.FakeBigDecimalMap200Response do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/file.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/file.ex index 44dc48abeb1..b24b508dbcd 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/file.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/file.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.File do Must be named `File` for test. """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :sourceURI ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.File do @type t :: %__MODULE__{ :sourceURI => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.File do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/file_schema_test_class.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/file_schema_test_class.ex index 509cfb2686e..a7320262ce9 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/file_schema_test_class.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/file_schema_test_class.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.FileSchemaTestClass do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :file, :files @@ -16,14 +16,13 @@ defmodule OpenapiPetstore.Model.FileSchemaTestClass do :file => OpenapiPetstore.Model.File.t | nil, :files => [OpenapiPetstore.Model.File.t] | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.FileSchemaTestClass do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:file, :struct, OpenapiPetstore.Model.File, options) - |> deserialize(:files, :list, OpenapiPetstore.Model.File, options) + |> Deserializer.deserialize(:file, :struct, OpenapiPetstore.Model.File) + |> Deserializer.deserialize(:files, :list, OpenapiPetstore.Model.File) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/foo.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/foo.ex index b089c097362..47dda5b3d67 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/foo.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/foo.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Foo do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :bar ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.Foo do @type t :: %__MODULE__{ :bar => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Foo do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/format_test.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/format_test.ex index 637a231cce5..2e1688d4685 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/format_test.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/format_test.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.FormatTest do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :integer, :int32, @@ -44,13 +44,13 @@ defmodule OpenapiPetstore.Model.FormatTest do :pattern_with_digits => String.t | nil, :pattern_with_digits_and_delimiter => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.FormatTest do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:date, :date, nil, options) + |> Deserializer.deserialize(:date, :date, nil) + |> Deserializer.deserialize(:dateTime, :datetime, nil) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/has_only_read_only.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/has_only_read_only.ex index 1a4c39d7015..b4104ef6e71 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/has_only_read_only.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/has_only_read_only.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.HasOnlyReadOnly do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :bar, :foo @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.HasOnlyReadOnly do :bar => String.t | nil, :foo => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.HasOnlyReadOnly do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/health_check_result.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/health_check_result.ex index 649a2fb5662..4998592f64f 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/health_check_result.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/health_check_result.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.HealthCheckResult do Just a string to inform instance is up and running. Make it nullable in hope to get it as pointer in generated model. """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :NullableMessage ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.HealthCheckResult do @type t :: %__MODULE__{ :NullableMessage => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.HealthCheckResult do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/list.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/list.ex index c9ccbe8d08f..8bb520df198 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/list.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/list.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.List do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :"123-list" ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.List do @type t :: %__MODULE__{ :"123-list" => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.List do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/map_test.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/map_test.ex index 5b3b1d9ac46..6f2ce9ae83c 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/map_test.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/map_test.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.MapTest do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :map_map_of_string, :map_of_enum_string, @@ -20,10 +20,8 @@ defmodule OpenapiPetstore.Model.MapTest do :direct_map => %{optional(String.t) => boolean()} | nil, :indirect_map => %{optional(String.t) => boolean()} | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.MapTest do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/mixed_properties_and_additional_properties_class.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/mixed_properties_and_additional_properties_class.ex index bd5426612bc..6e7f935374b 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/mixed_properties_and_additional_properties_class.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/mixed_properties_and_additional_properties_class.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.MixedPropertiesAndAdditionalPropertiesClass do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :uuid, :dateTime, @@ -18,13 +18,13 @@ defmodule OpenapiPetstore.Model.MixedPropertiesAndAdditionalPropertiesClass do :dateTime => DateTime.t | nil, :map => %{optional(String.t) => OpenapiPetstore.Model.Animal.t} | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.MixedPropertiesAndAdditionalPropertiesClass do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:map, :map, OpenapiPetstore.Model.Animal, options) + |> Deserializer.deserialize(:dateTime, :datetime, nil) + |> Deserializer.deserialize(:map, :map, OpenapiPetstore.Model.Animal) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/model_200_response.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/model_200_response.ex index 6a473f552f8..a7cc1003c8a 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/model_200_response.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/model_200_response.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Model200Response do Model for testing model name starting with number """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :name, :class @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.Model200Response do :name => integer() | nil, :class => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Model200Response do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/name.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/name.ex index 0a8d36757ce..6a1b9b5366a 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/name.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/name.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Name do Model for testing model name same as property name """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :name, :snake_case, @@ -20,10 +20,8 @@ defmodule OpenapiPetstore.Model.Name do :property => String.t | nil, :"123Number" => integer() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Name do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/nullable_class.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/nullable_class.ex index c282a203fa1..92fe3512449 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/nullable_class.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/nullable_class.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.NullableClass do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :integer_prop, :number_prop, @@ -36,13 +36,13 @@ defmodule OpenapiPetstore.Model.NullableClass do :object_and_items_nullable_prop => %{optional(String.t) => map()} | nil, :object_items_nullable => %{optional(String.t) => map()} | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.NullableClass do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:date_prop, :date, nil, options) + |> Deserializer.deserialize(:date_prop, :date, nil) + |> Deserializer.deserialize(:datetime_prop, :datetime, nil) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/number_only.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/number_only.ex index 2d48d9ae609..8071ca0afb4 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/number_only.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/number_only.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.NumberOnly do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :JustNumber ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.NumberOnly do @type t :: %__MODULE__{ :JustNumber => float() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.NumberOnly do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/object_with_deprecated_fields.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/object_with_deprecated_fields.ex index 44e0dd7962b..67b403e60af 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/object_with_deprecated_fields.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/object_with_deprecated_fields.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ObjectWithDeprecatedFields do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :uuid, :id, @@ -18,15 +18,14 @@ defmodule OpenapiPetstore.Model.ObjectWithDeprecatedFields do :uuid => String.t | nil, :id => float() | nil, :deprecatedRef => OpenapiPetstore.Model.DeprecatedObject.t | nil, - :bars => [OpenapiPetstore.Model.String.t] | nil + :bars => [String.t] | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.ObjectWithDeprecatedFields do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:deprecatedRef, :struct, OpenapiPetstore.Model.DeprecatedObject, options) + |> Deserializer.deserialize(:deprecatedRef, :struct, OpenapiPetstore.Model.DeprecatedObject) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/order.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/order.ex index 4f4ea7b3a39..4450a7f3184 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/order.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/order.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Order do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :id, :petId, @@ -24,11 +24,12 @@ defmodule OpenapiPetstore.Model.Order do :status => String.t | nil, :complete => boolean() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Order do - def decode(value, _options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value + |> Deserializer.deserialize(:shipDate, :datetime, nil) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_composite.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_composite.ex index 0e09aecba90..911be7c9d97 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_composite.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_composite.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.OuterComposite do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :my_number, :my_string, @@ -18,10 +18,8 @@ defmodule OpenapiPetstore.Model.OuterComposite do :my_string => String.t | nil, :my_boolean => boolean() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.OuterComposite do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum.ex index 4bb536a0342..dbc04c6532e 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.OuterEnum do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.OuterEnum do @type t :: %__MODULE__{ } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.OuterEnum do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_default_value.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_default_value.ex index 7368598630a..5af5bc6a8b2 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_default_value.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_default_value.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.OuterEnumDefaultValue do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.OuterEnumDefaultValue do @type t :: %__MODULE__{ } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.OuterEnumDefaultValue do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer.ex index 3b6285d01b6..5eba27d330f 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.OuterEnumInteger do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.OuterEnumInteger do @type t :: %__MODULE__{ } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.OuterEnumInteger do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer_default_value.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer_default_value.ex index 3debd284a56..2b5c62158d7 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer_default_value.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_enum_integer_default_value.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.OuterEnumIntegerDefaultValue do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.OuterEnumIntegerDefaultValue do @type t :: %__MODULE__{ } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.OuterEnumIntegerDefaultValue do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_object_with_enum_property.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_object_with_enum_property.ex index 8ec0302d3a3..8f48be9c154 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_object_with_enum_property.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/outer_object_with_enum_property.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.OuterObjectWithEnumProperty do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :value ] @@ -14,13 +14,12 @@ defmodule OpenapiPetstore.Model.OuterObjectWithEnumProperty do @type t :: %__MODULE__{ :value => OpenapiPetstore.Model.OuterEnumInteger.t } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.OuterObjectWithEnumProperty do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:value, :struct, OpenapiPetstore.Model.OuterEnumInteger, options) + |> Deserializer.deserialize(:value, :struct, OpenapiPetstore.Model.OuterEnumInteger) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/pet.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/pet.ex index 34b9f199b0f..d236516ae83 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/pet.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/pet.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Pet do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :id, :category, @@ -24,14 +24,13 @@ defmodule OpenapiPetstore.Model.Pet do :tags => [OpenapiPetstore.Model.Tag.t] | nil, :status => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Pet do - import OpenapiPetstore.Deserializer - def decode(value, options) do + alias OpenapiPetstore.Deserializer + + def decode(value) do value - |> deserialize(:category, :struct, OpenapiPetstore.Model.Category, options) - |> deserialize(:tags, :list, OpenapiPetstore.Model.Tag, options) + |> Deserializer.deserialize(:category, :struct, OpenapiPetstore.Model.Category) + |> Deserializer.deserialize(:tags, :list, OpenapiPetstore.Model.Tag) end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/read_only_first.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/read_only_first.ex index bc9c1b5a17d..38c33f6fa88 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/read_only_first.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/read_only_first.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.ReadOnlyFirst do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :bar, :baz @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.ReadOnlyFirst do :bar => String.t | nil, :baz => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.ReadOnlyFirst do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/return.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/return.ex index e5a026d8de8..a955541b63e 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/return.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/return.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Return do Model for testing reserved words """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :return ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.Return do @type t :: %__MODULE__{ :return => integer() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Return do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/single_ref_type.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/single_ref_type.ex index 28b77566b79..8feb8814092 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/single_ref_type.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/single_ref_type.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.SingleRefType do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ ] @@ -14,10 +14,8 @@ defmodule OpenapiPetstore.Model.SingleRefType do @type t :: %__MODULE__{ } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.SingleRefType do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/tag.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/tag.ex index 83e6e6856dd..a900c01e987 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/tag.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/tag.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.Tag do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :id, :name @@ -16,10 +16,8 @@ defmodule OpenapiPetstore.Model.Tag do :id => integer() | nil, :name => String.t | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.Tag do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/model/user.ex b/samples/client/petstore/elixir/lib/openapi_petstore/model/user.ex index 92134273764..754874a7c95 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/model/user.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/model/user.ex @@ -6,7 +6,7 @@ defmodule OpenapiPetstore.Model.User do """ - @derive [Poison.Encoder] + @derive Jason.Encoder defstruct [ :id, :username, @@ -28,10 +28,8 @@ defmodule OpenapiPetstore.Model.User do :phone => String.t | nil, :userStatus => integer() | nil } -end -defimpl Poison.Decoder, for: OpenapiPetstore.Model.User do - def decode(value, _options) do + def decode(value) do value end end diff --git a/samples/client/petstore/elixir/lib/openapi_petstore/request_builder.ex b/samples/client/petstore/elixir/lib/openapi_petstore/request_builder.ex index 561966a4106..d12885f32a3 100644 --- a/samples/client/petstore/elixir/lib/openapi_petstore/request_builder.ex +++ b/samples/client/petstore/elixir/lib/openapi_petstore/request_builder.ex @@ -96,7 +96,7 @@ defmodule OpenapiPetstore.RequestBuilder do Tesla.Multipart.add_field( multipart, key, - Poison.encode!(value), + Jason.encode!(value), headers: [{:"Content-Type", "application/json"}] ) end) @@ -148,8 +148,8 @@ defmodule OpenapiPetstore.RequestBuilder do Map.put_new(request, :body, "") end - @type status_code :: 100..599 - @type response_mapping :: [{status_code, struct() | false}] + @type status_code :: :default | 100..599 + @type response_mapping :: [{status_code, false | %{} | module()}] @doc """ Evaluate the response from a Tesla request. @@ -187,5 +187,11 @@ defmodule OpenapiPetstore.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}, %{}) do + OpenapiPetstore.Deserializer.jason_decode(body) + end + + defp decode(%Tesla.Env{body: body}, module) do + OpenapiPetstore.Deserializer.jason_decode(body, module) + end end diff --git a/samples/client/petstore/elixir/mix.exs b/samples/client/petstore/elixir/mix.exs index d7f7fae6880..7ccbcd9ffb3 100644 --- a/samples/client/petstore/elixir/mix.exs +++ b/samples/client/petstore/elixir/mix.exs @@ -9,7 +9,9 @@ defmodule OpenapiPetstore.Mixfile do build_embedded: Mix.env() == :prod, start_permanent: Mix.env() == :prod, package: package(), - description: "This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\", + description: """ + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + """, deps: deps() ] end @@ -33,9 +35,10 @@ defmodule OpenapiPetstore.Mixfile do # Type "mix help deps" for more examples and options defp deps do [ - {: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}, + {:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false} ] end diff --git a/samples/client/petstore/elixir/test/deserializer_test.exs b/samples/client/petstore/elixir/test/deserializer_test.exs new file mode 100644 index 00000000000..31b2b2cf45f --- /dev/null +++ b/samples/client/petstore/elixir/test/deserializer_test.exs @@ -0,0 +1,49 @@ +defmodule DeserializerTest do + use ExUnit.Case, async: true + alias OpenapiPetstore.Deserializer + alias OpenapiPetstore.Model.{Category, Pet, Tag} + + @valid_json """ + { + "id": 14, + "category": { + "id": 75, + "name": "sea dragons" + }, + "name": "Nagga", + "photoUrls": [ + "https://example.com/nagga1.jpg", + "https://example.com/nagga2.jpg" + ], + "tags": [ + { + "id": 99, + "name": "dragon" + }, + { + "id": 23, + "name": "sea" + } + ], + "status": "foo" + } + """ + + test "jason_decode/2 with valid JSON" do + assert Deserializer.jason_decode(@valid_json, Pet) == + {:ok, + %Pet{ + id: 14, + category: %Category{id: 75, name: "sea dragons"}, + name: "Nagga", + photoUrls: ["https://example.com/nagga1.jpg", "https://example.com/nagga2.jpg"], + tags: [%Tag{id: 99, name: "dragon"}, %Tag{id: 23, name: "sea"}], + status: "foo" + }} + end + + test "jason_decode/2 with invalid JSON" do + assert Deserializer.jason_decode(~s/{: 1}/, Pet) == + {:error, %Jason.DecodeError{data: "{: 1}", position: 1, token: nil}} + end +end diff --git a/samples/client/petstore/elixir/test/format_test.exs b/samples/client/petstore/elixir/test/format_test.exs new file mode 100644 index 00000000000..22e545db6b7 --- /dev/null +++ b/samples/client/petstore/elixir/test/format_test.exs @@ -0,0 +1,84 @@ +defmodule FormatTest do + use ExUnit.Case, async: true + alias OpenapiPetstore.Model.FormatTest + + test "decode all properties (not nil)" do + assert %FormatTest{ + integer: 1, + int32: 2, + int64: 3, + number: 4.1, + float: 5.2, + double: 6.3, + decimal: "7.4", + string: "Hello world!", + byte: "U3dhZ2dlciByb2Nrcw==", + binary: <<1, 2, 3>>, + date: "2013-10-20", + dateTime: "2013-10-20T19:20:30+01:00", + uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6", + password: "green?horse", + pattern_with_digits: "1234567890", + pattern_with_digits_and_delimiter: "Image_01" + } + |> FormatTest.decode() == + %FormatTest{ + integer: 1, + int32: 2, + int64: 3, + number: 4.1, + float: 5.2, + double: 6.3, + decimal: "7.4", + string: "Hello world!", + byte: "U3dhZ2dlciByb2Nrcw==", + binary: <<1, 2, 3>>, + date: ~D[2013-10-20], + dateTime: ~U[2013-10-20T18:20:30Z], + uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6", + password: "green?horse", + pattern_with_digits: "1234567890", + pattern_with_digits_and_delimiter: "Image_01" + } + end + + test "decode all properties (some are nil)" do + assert %FormatTest{ + integer: nil, + int32: nil, + int64: nil, + number: 4.1, + float: nil, + double: nil, + decimal: nil, + string: nil, + byte: "U3dhZ2dlciByb2Nrcw==", + binary: nil, + date: "2013-10-20", + dateTime: nil, + uuid: nil, + password: "green?horse", + pattern_with_digits: nil, + pattern_with_digits_and_delimiter: nil + } + |> FormatTest.decode() == + %FormatTest{ + integer: nil, + int32: nil, + int64: nil, + number: 4.1, + float: nil, + double: nil, + decimal: nil, + string: nil, + byte: "U3dhZ2dlciByb2Nrcw==", + binary: nil, + date: ~D[2013-10-20], + dateTime: nil, + uuid: nil, + password: "green?horse", + pattern_with_digits: nil, + pattern_with_digits_and_delimiter: nil + } + end +end diff --git a/samples/client/petstore/elixir/test/mixed_properties_and_additional_properties_class_test.exs b/samples/client/petstore/elixir/test/mixed_properties_and_additional_properties_class_test.exs new file mode 100644 index 00000000000..94e18ee9345 --- /dev/null +++ b/samples/client/petstore/elixir/test/mixed_properties_and_additional_properties_class_test.exs @@ -0,0 +1,46 @@ +defmodule MixedPropertiesAndAdditionalPropertiesClass do + use ExUnit.Case, async: true + alias OpenapiPetstore.Model.MixedPropertiesAndAdditionalPropertiesClass, as: Model + alias OpenapiPetstore.Model.Animal + + test "decode all properties (not nil)" do + assert %Model{ + uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6", + dateTime: "2013-10-20T19:20:30+01:00", + map: %{ + "doggie" => %{"className" => "DOG", "color" => "yellow", "breed" => "Shiba Inu"}, + "meow" => %{"className" => "CAT", "color" => "white", "declawed" => false} + } + } + |> Model.decode() == + %Model{ + uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6", + dateTime: ~U[2013-10-20T18:20:30Z], + map: %{ + # TODO values should be Dog and Cat structs instead of an Animal + "doggie" => %Animal{ + className: "DOG", + color: "yellow" + }, + "meow" => %Animal{ + className: "CAT", + color: "white" + } + } + } + end + + test "decode all properties (nil)" do + assert %Model{ + uuid: nil, + dateTime: nil, + map: nil + } + |> Model.decode() == + %Model{ + uuid: nil, + dateTime: nil, + map: nil + } + end +end diff --git a/samples/client/petstore/elixir/test/pet_test.exs b/samples/client/petstore/elixir/test/pet_test.exs index 6011a08ccf5..0c8743062db 100644 --- a/samples/client/petstore/elixir/test/pet_test.exs +++ b/samples/client/petstore/elixir/test/pet_test.exs @@ -1,60 +1,66 @@ defmodule PetTest do - use ExUnit.Case - alias OpenapiPetstore.Connection - alias OpenapiPetstore.Api.Pet, as: PetApi - alias OpenapiPetstore.Model.Pet - alias OpenapiPetstore.Model.Category - alias OpenapiPetstore.Model.Tag + use ExUnit.Case, async: true + alias OpenapiPetstore.Connection + alias OpenapiPetstore.Api.Pet, as: PetApi + alias OpenapiPetstore.Model.Pet + alias OpenapiPetstore.Model.Category + alias OpenapiPetstore.Model.Tag - test "add and delete a pet" do - petId = 10007 - pet = %Pet{ - :id => petId, - :name => "elixir client test", - :photoUrls => ["http://test_elixir_unit_test.com"], - :category => %Category{:id => petId, :name=> "test elixir category"}, - :tags => [%Tag{:id => petId, :name => "test elixir tag"}], - } - {:ok, response} = PetApi.add_pet(Connection.new, pet) - assert response.status == 200 + setup do + %{connection: Connection.new()} + end - {:ok, pet} = PetApi.get_pet_by_id(Connection.new, petId) - assert pet.id == petId - assert pet.name == "elixir client test" - assert List.first(pet.photoUrls) == "http://test_elixir_unit_test.com" - assert pet.category.id == petId - assert pet.category.name == "test elixir category" - assert List.first(pet.tags) == %Tag{:id => petId, :name => "test elixir tag"} + test "add and delete a pet", %{connection: connection} do + petId = 10007 - {:ok, response} = PetApi.delete_pet(Connection.new, petId) - assert response.status == 200 - {:ok, response} = PetApi.get_pet_by_id(Connection.new, petId) - assert response.status == 404 - end + pet = %Pet{ + :id => petId, + :name => "elixir client test", + :photoUrls => ["http://test_elixir_unit_test.com"], + :category => %Category{:id => petId, :name => "test elixir category"}, + :tags => [%Tag{:id => petId, :name => "test elixir tag"}] + } - test "update a pet" do - petId = 10007 - pet = %Pet{ - :id => petId, - :name => "elixir client updatePet", - :status => "pending", - :photoUrls => ["http://test_elixir_unit_test.com"] - } - {:ok, response} = PetApi.update_pet(Connection.new, pet) - assert response.status == 200 + {:ok, %Tesla.Env{} = response} = PetApi.add_pet(connection, pet) + assert response.status == 200 - {:ok, pet} = PetApi.get_pet_by_id(Connection.new, petId) - assert pet.id == petId - assert pet.name == "elixir client updatePet" - assert pet.status == "pending" - end + {:ok, pet} = PetApi.get_pet_by_id(connection, petId) + assert pet.id == petId + assert pet.name == "elixir client test" + assert pet.photoUrls == ["http://test_elixir_unit_test.com"] + assert pet.category == %Category{id: petId, name: "test elixir category"} + assert pet.tags == [%Tag{:id => petId, :name => "test elixir tag"}] - test "find pet by status" do - {:ok, listPets} = PetApi.find_pets_by_status(Connection.new, "available") - assert List.first(listPets) != nil + {:ok, response} = PetApi.delete_pet(connection, petId) + assert response.status == 200 + {:ok, response} = PetApi.get_pet_by_id(connection, petId) + assert response.status == 404 + end - {:ok, listPets} = PetApi.find_pets_by_status(Connection.new, "unknown_and_incorrect_status") - assert List.first(listPets) == nil - end + test "update a pet", %{connection: connection} do + petId = 10007 + pet = %Pet{ + :id => petId, + :name => "elixir client updatePet", + :status => "pending", + :photoUrls => ["http://test_elixir_unit_test.com"] + } + + {:ok, response} = PetApi.update_pet(connection, pet) + assert response.status == 200 + + {:ok, pet} = PetApi.get_pet_by_id(connection, petId) + assert pet.id == petId + assert pet.name == "elixir client updatePet" + assert pet.status == "pending" + end + + test "find pet by status", %{connection: connection} do + {:ok, listPets} = PetApi.find_pets_by_status(connection, "available") + assert List.first(listPets) != nil + + {:ok, listPets} = PetApi.find_pets_by_status(connection, "unknown_and_incorrect_status") + assert List.first(listPets) == nil + end end diff --git a/samples/client/petstore/elixir/test/store_test.exs b/samples/client/petstore/elixir/test/store_test.exs new file mode 100644 index 00000000000..46e53fab3ea --- /dev/null +++ b/samples/client/petstore/elixir/test/store_test.exs @@ -0,0 +1,17 @@ +defmodule StoreTest do + use ExUnit.Case, async: true + alias OpenapiPetstore.Connection + alias OpenapiPetstore.Api.Store, as: StoreApi + + setup do + %{connection: Connection.new()} + end + + test "fetch inventory", %{connection: connection} do + {:ok, inventory} = StoreApi.get_inventory(connection) + + assert is_map(inventory) + assert Enum.all?(Map.keys(inventory), &is_binary/1) + assert Enum.all?(Map.values(inventory), &is_integer/1) + end +end