From d16670e4347c5047ac8a96a41d9452cf31ca8307 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Fri, 25 Jan 2019 16:23:40 -0500 Subject: [PATCH 1/4] Remove `underscore_to_dash` Configuration Option --- README.md | 4 ---- lib/jsonapi/deprecation.ex | 7 ------- lib/jsonapi/utils/string.ex | 15 +-------------- test/utils/string_test.exs | 32 -------------------------------- 4 files changed, 1 insertion(+), 57 deletions(-) diff --git a/README.md b/README.md index fb390fb9..963397d9 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,6 @@ Under-the-hood `JSONAPI.EnsureSpec` relies on three individual plugs: config :jsonapi, host: "www.someotherhost.com", scheme: "https", - underscore_to_dash: true, # DEPRECATED field_transformation: :underscore, remove_links: false, json_library: Jason @@ -151,9 +150,6 @@ config :jsonapi, `"favorite-color": blue`). If your API uses dashed fields, set this value to `:dasherize`. If your API uses underscores (e.g. `"favorite_color": "red"`) set to `:underscore`. -- **underscore_to_dash**. This is a deprecated option that previously defaulted - to `false`. Please set the appropriate value on the `field_transformation` - configuration option. ## Other diff --git a/lib/jsonapi/deprecation.ex b/lib/jsonapi/deprecation.ex index ed769803..4d8b6d28 100644 --- a/lib/jsonapi/deprecation.ex +++ b/lib/jsonapi/deprecation.ex @@ -27,11 +27,4 @@ defmodule JSONAPI.Deprecation do Macro.Env.stacktrace(__ENV__) ) end - - def warn(:underscore_to_dash) do - IO.warn( - "`:underscore_to_dash` is deprecated. If you want underscored fields, set `:field_transformation` to :underscore. If you want your fields to be dashed, set to :dasherized", - Macro.Env.stacktrace(__ENV__) - ) - end end diff --git a/lib/jsonapi/utils/string.ex b/lib/jsonapi/utils/string.ex index 55ade066..0a1ec247 100644 --- a/lib/jsonapi/utils/string.ex +++ b/lib/jsonapi/utils/string.ex @@ -179,18 +179,6 @@ defmodule JSONAPI.Utils.String do fun.(value) end - defp normalized_underscore_to_dash_config(value) when is_boolean(value) do - Deprecation.warn(:underscore_to_dash) - - if value do - :dasherize - else - :underscore - end - end - - defp normalized_underscore_to_dash_config(value) when is_nil(value), do: value - @doc """ The configured transformation for the API's fields. JSON:API v1.1 recommends using camlized fields (e.g. "goodDog", versus "good_dog"). However, we don't hold a strong @@ -219,8 +207,7 @@ defmodule JSONAPI.Utils.String do ``` """ def field_transformation do - normalized_underscore_to_dash_config(Application.get_env(:jsonapi, :underscore_to_dash)) || - field_transformation(Application.get_env(:jsonapi, :field_transformation)) + field_transformation(Application.get_env(:jsonapi, :field_transformation)) end @doc false diff --git a/test/utils/string_test.exs b/test/utils/string_test.exs index 512f5fea..3f913a9a 100644 --- a/test/utils/string_test.exs +++ b/test/utils/string_test.exs @@ -6,36 +6,4 @@ defmodule JSONAPI.Utils.StringTest do import JSONAPI.Utils.String doctest JSONAPI.Utils.String - - describe "legacy configuration to dasherize fields" do - setup do - Application.put_env(:jsonapi, :underscore_to_dash, true) - - on_exit(fn -> - Application.delete_env(:jsonapi, :underscore_to_dash) - end) - - {:ok, []} - end - - test "#field_transformation/0 returns :dasherize" do - assert field_transformation() == :dasherize - end - end - - describe "legacy configuration to underscore fields" do - setup do - Application.put_env(:jsonapi, :underscore_to_dash, false) - - on_exit(fn -> - Application.delete_env(:jsonapi, :underscore_to_dash) - end) - - {:ok, []} - end - - test "#field_transformation/0 returns :underscore" do - assert field_transformation() == :underscore - end - end end From 8d6e044a769ffc01974eabf2a82f123342aeca1f Mon Sep 17 00:00:00 2001 From: James Herdman Date: Fri, 25 Jan 2019 16:25:39 -0500 Subject: [PATCH 2/4] Remove `View.hidden/0` --- lib/jsonapi/deprecation.ex | 7 ------- lib/jsonapi/view.ex | 9 +-------- test/jsonapi/view_test.exs | 2 +- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/lib/jsonapi/deprecation.ex b/lib/jsonapi/deprecation.ex index 4d8b6d28..ebdd7621 100644 --- a/lib/jsonapi/deprecation.ex +++ b/lib/jsonapi/deprecation.ex @@ -14,13 +14,6 @@ defmodule JSONAPI.Deprecation do ) end - def warn(:hidden) do - IO.warn( - "`JSONAPI.View.hidden/0` is deprecated; use `JSONAPI.View.hidden/1` instead.", - Macro.Env.stacktrace(__ENV__) - ) - end - def warn(:query_parser_dash) do IO.warn( "`JSONAPI.QueryParser` will no longer automatically dasherize incoming parameters. Please include `JSONAPI.UnderscoreParameters` in your pipeline. See https://github.com/jeregrine/jsonapi/pull/149", diff --git a/lib/jsonapi/view.ex b/lib/jsonapi/view.ex index a8cf76ce..af6e5fce 100644 --- a/lib/jsonapi/view.ex +++ b/lib/jsonapi/view.ex @@ -105,14 +105,7 @@ defmodule JSONAPI.View do end def attributes(data, conn) do - hidden = - if Enum.member?(__MODULE__.__info__(:functions), {:hidden, 0}) do - JSONAPI.Deprecation.warn(:hidden) - this = __MODULE__ - this.hidden() - else - hidden(data) - end + hidden = hidden(data) visible_fields = fields() -- hidden diff --git a/test/jsonapi/view_test.exs b/test/jsonapi/view_test.exs index e6842910..87fa90f5 100644 --- a/test/jsonapi/view_test.exs +++ b/test/jsonapi/view_test.exs @@ -36,7 +36,7 @@ defmodule JSONAPI.ViewTest do "#{user.first_name} #{user.last_name}" end - def hidden do + def hidden(_data) do [:password] end end From 3cfc84892746e51ae79ea763ca9d28c139797a12 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Fri, 25 Jan 2019 17:14:00 -0500 Subject: [PATCH 3/4] Remove Deprecated `includes` Pathway --- lib/jsonapi/config.ex | 1 - lib/jsonapi/deprecation.ex | 23 ----------------------- lib/jsonapi/plugs/query_parser.ex | 8 ++------ lib/jsonapi/serializer.ex | 3 ++- test/jsonapi/plugs/query_parser_test.exs | 23 +++++++---------------- test/serializer_test.exs | 19 ++++++++++--------- 6 files changed, 21 insertions(+), 56 deletions(-) delete mode 100644 lib/jsonapi/deprecation.ex diff --git a/lib/jsonapi/config.ex b/lib/jsonapi/config.ex index 4339dc60..bfd6c1c8 100644 --- a/lib/jsonapi/config.ex +++ b/lib/jsonapi/config.ex @@ -7,7 +7,6 @@ defmodule JSONAPI.Config do fields: %{}, filter: [], include: [], - includes: [], opts: nil, required_fields: nil, sort: nil, diff --git a/lib/jsonapi/deprecation.ex b/lib/jsonapi/deprecation.ex deleted file mode 100644 index ebdd7621..00000000 --- a/lib/jsonapi/deprecation.ex +++ /dev/null @@ -1,23 +0,0 @@ -defmodule JSONAPI.Deprecation do - @moduledoc """ - Generate warnings in places where we want to deprecate functions or struct parameters - """ - - @doc """ - Generates a deprecation warning for `includes` in the JSONAPI.Config struct. - """ - - def warn(:includes) do - IO.warn( - "`%JSONAPI.Config{}.includes` is deprecated; call `%JSONAPI.Config{}.include` instead.", - Macro.Env.stacktrace(__ENV__) - ) - end - - def warn(:query_parser_dash) do - IO.warn( - "`JSONAPI.QueryParser` will no longer automatically dasherize incoming parameters. Please include `JSONAPI.UnderscoreParameters` in your pipeline. See https://github.com/jeregrine/jsonapi/pull/149", - Macro.Env.stacktrace(__ENV__) - ) - end -end diff --git a/lib/jsonapi/plugs/query_parser.ex b/lib/jsonapi/plugs/query_parser.ex index a9153028..3b710af3 100644 --- a/lib/jsonapi/plugs/query_parser.ex +++ b/lib/jsonapi/plugs/query_parser.ex @@ -172,11 +172,7 @@ defmodule JSONAPI.QueryParser do def parse_include(%Config{} = config, include_str) do includes = handle_include(include_str, config) - Deprecation.warn(:includes) - - config - |> Map.put(:includes, includes) - |> Map.put(:include, includes) + Map.put(config, :include, includes) end def handle_include(str, config) when is_binary(str) do @@ -232,7 +228,7 @@ defmodule JSONAPI.QueryParser do defp build_config(opts) do view = Keyword.fetch!(opts, :view) - struct(JSONAPI.Config, opts: opts, view: view) + struct(Config, opts: opts, view: view) end defp struct_from_map(params, struct) do diff --git a/lib/jsonapi/serializer.ex b/lib/jsonapi/serializer.ex index 4c92d1da..1df93356 100644 --- a/lib/jsonapi/serializer.ex +++ b/lib/jsonapi/serializer.ex @@ -6,6 +6,7 @@ defmodule JSONAPI.Serializer do import JSONAPI.Ecto, only: [assoc_loaded?: 1] alias JSONAPI.Utils.String, as: JString + alias JSONAPI.Config require Logger @@ -19,7 +20,7 @@ defmodule JSONAPI.Serializer do def serialize(view, data, conn \\ nil, meta \\ nil) do query_includes = case conn do - %Plug.Conn{assigns: %{jsonapi_query: %{includes: includes}}} -> includes + %Plug.Conn{assigns: %{jsonapi_query: %Config{include: include}}} -> include _ -> [] end diff --git a/test/jsonapi/plugs/query_parser_test.exs b/test/jsonapi/plugs/query_parser_test.exs index a32c6491..94b135cf 100644 --- a/test/jsonapi/plugs/query_parser_test.exs +++ b/test/jsonapi/plugs/query_parser_test.exs @@ -77,11 +77,11 @@ defmodule JSONAPI.QueryParserTest do test "parse_include/2 turns an include string into a keyword list" do config = struct(Config, view: MyView) - assert parse_include(config, "author,comments.user").includes == [:author, comments: :user] - assert parse_include(config, "author").includes == [:author] - assert parse_include(config, "comments,author").includes == [:comments, :author] - assert parse_include(config, "comments.user").includes == [comments: :user] - assert parse_include(config, "best_friends").includes == [:best_friends] + assert parse_include(config, "author,comments.user").include == [:author, comments: :user] + assert parse_include(config, "author").include == [:author] + assert parse_include(config, "comments,author").include == [:comments, :author] + assert parse_include(config, "comments.user").include == [comments: :user] + assert parse_include(config, "best_friends").include == [:best_friends] assert_raise ArgumentError, "argument error", fn -> assert parse_include(config, "author.top-posts") @@ -101,20 +101,11 @@ defmodule JSONAPI.QueryParserTest do test "parse_include/2 turns an include string into a keyword list" do config = struct(Config, view: MyView) - assert parse_include(config, "author.top-posts").includes == [author: :top_posts] - assert parse_include(config, "best-friends").includes == [:best_friends] + assert parse_include(config, "author.top-posts").include == [author: :top_posts] + assert parse_include(config, "best-friends").include == [:best_friends] end end - test "parse_include/2 returns a map with duplicate values for include and includes for compatibility" do - include_list = - Config - |> struct(view: MyView) - |> parse_include("comments.user") - - assert include_list.includes == include_list.include - end - test "parse_include/2 errors with invalid includes" do config = struct(Config, view: MyView) diff --git a/test/serializer_test.exs b/test/serializer_test.exs index 165e5fe6..c6afba76 100644 --- a/test/serializer_test.exs +++ b/test/serializer_test.exs @@ -1,6 +1,7 @@ defmodule JSONAPISerializerTest do use ExUnit.Case, async: false - alias JSONAPI.Serializer + + alias JSONAPI.{Config, Serializer} import ExUnit.CaptureLog @@ -268,8 +269,8 @@ defmodule JSONAPISerializerTest do conn = %Plug.Conn{ assigns: %{ - jsonapi_query: %{ - includes: [best_comments: :user] + jsonapi_query: %Config{ + include: [best_comments: :user] } } } @@ -293,8 +294,8 @@ defmodule JSONAPISerializerTest do conn = %Plug.Conn{ assigns: %{ - jsonapi_query: %{ - includes: [:company] + jsonapi_query: %Config{ + include: [:company] } } } @@ -318,8 +319,8 @@ defmodule JSONAPISerializerTest do conn = %Plug.Conn{ assigns: %{ - jsonapi_query: %{ - includes: [company: :industry] + jsonapi_query: %Config{ + include: [company: :industry] } } } @@ -354,8 +355,8 @@ defmodule JSONAPISerializerTest do conn = %Plug.Conn{ assigns: %{ - jsonapi_query: %{ - includes: [company: [industry: :tags]] + jsonapi_query: %Config{ + include: [company: [industry: :tags]] } } } From 0e996bec8e591534df6e23d53619d5bdefb2bdbd Mon Sep 17 00:00:00 2001 From: James Herdman Date: Fri, 25 Jan 2019 17:22:21 -0500 Subject: [PATCH 4/4] Remove Query Parser Normalizing Deprecation --- lib/jsonapi/plugs/query_parser.ex | 16 ++-------------- lib/jsonapi/utils/string.ex | 2 -- test/jsonapi/plugs/query_parser_test.exs | 18 ------------------ 3 files changed, 2 insertions(+), 34 deletions(-) diff --git a/lib/jsonapi/plugs/query_parser.ex b/lib/jsonapi/plugs/query_parser.ex index 3b710af3..1b3af2e6 100644 --- a/lib/jsonapi/plugs/query_parser.ex +++ b/lib/jsonapi/plugs/query_parser.ex @@ -1,8 +1,7 @@ defmodule JSONAPI.QueryParser do @behaviour Plug - alias JSONAPI.{Config, Deprecation, Page} + alias JSONAPI.{Config, Page} alias JSONAPI.Exceptions.InvalidQuery - alias JSONAPI.Utils.String, as: JString alias Plug.Conn import JSONAPI.Utils.IncludeTree @@ -183,7 +182,7 @@ defmodule JSONAPI.QueryParser do if inc =~ ~r/\w+\.\w+/ do acc ++ handle_nested_include(inc, valid_includes, config) else - inc = inc |> normalize_fields() |> String.to_existing_atom() + inc = inc |> String.to_existing_atom() if Enum.any?(valid_includes, fn {key, _val} -> key == inc end) do acc ++ [inc] @@ -198,7 +197,6 @@ defmodule JSONAPI.QueryParser do keys = key |> String.split(".") - |> Enum.map(&normalize_fields/1) |> Enum.map(&String.to_existing_atom/1) last = List.last(keys) @@ -242,14 +240,4 @@ defmodule JSONAPI.QueryParser do struct(struct, processed_map) end - - defp normalize_fields(fields) do - Deprecation.warn(:query_parser_dash) - - if JString.field_transformation() == :underscore do - fields - else - JString.expand_fields(fields, &JString.underscore/1) - end - end end diff --git a/lib/jsonapi/utils/string.ex b/lib/jsonapi/utils/string.ex index 0a1ec247..01dd92f5 100644 --- a/lib/jsonapi/utils/string.ex +++ b/lib/jsonapi/utils/string.ex @@ -3,8 +3,6 @@ defmodule JSONAPI.Utils.String do String manipulation helpers. """ - alias JSONAPI.Deprecation - @allowed_transformations [:camelize, :dasherize, :underscore] @doc """ diff --git a/test/jsonapi/plugs/query_parser_test.exs b/test/jsonapi/plugs/query_parser_test.exs index 94b135cf..a56a9b73 100644 --- a/test/jsonapi/plugs/query_parser_test.exs +++ b/test/jsonapi/plugs/query_parser_test.exs @@ -88,24 +88,6 @@ defmodule JSONAPI.QueryParserTest do end end - describe "when API configured for dashed fields" do - setup do - Application.put_env(:jsonapi, :field_transformation, :dasherize) - - on_exit(fn -> - Application.delete_env(:jsonapi, :field_transformation) - end) - - {:ok, []} - end - - test "parse_include/2 turns an include string into a keyword list" do - config = struct(Config, view: MyView) - assert parse_include(config, "author.top-posts").include == [author: :top_posts] - assert parse_include(config, "best-friends").include == [:best_friends] - end - end - test "parse_include/2 errors with invalid includes" do config = struct(Config, view: MyView)