Skip to content

Commit

Permalink
Merge pull request #163 from jherdman/remove-all-deprecations
Browse files Browse the repository at this point in the history
Remove All Deprecated Code
  • Loading branch information
jherdman authored Jan 27, 2019
2 parents 201ae1c + 0e996be commit 323eab3
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 161 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/jsonapi/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule JSONAPI.Config do
fields: %{},
filter: [],
include: [],
includes: [],
opts: nil,
required_fields: nil,
sort: nil,
Expand Down
37 changes: 0 additions & 37 deletions lib/jsonapi/deprecation.ex

This file was deleted.

24 changes: 4 additions & 20 deletions lib/jsonapi/plugs/query_parser.ex
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -172,11 +171,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
Expand All @@ -187,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]
Expand All @@ -202,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)
Expand Down Expand Up @@ -232,7 +226,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
Expand All @@ -246,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
3 changes: 2 additions & 1 deletion lib/jsonapi/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
17 changes: 1 addition & 16 deletions lib/jsonapi/utils/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule JSONAPI.Utils.String do
String manipulation helpers.
"""

alias JSONAPI.Deprecation

@allowed_transformations [:camelize, :dasherize, :underscore]

@doc """
Expand Down Expand Up @@ -179,18 +177,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
Expand Down Expand Up @@ -219,8 +205,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
Expand Down
9 changes: 1 addition & 8 deletions lib/jsonapi/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 5 additions & 32 deletions test/jsonapi/plugs/query_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,44 +77,17 @@ 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")
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").includes == [author: :top_posts]
assert parse_include(config, "best-friends").includes == [: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)

Expand Down
2 changes: 1 addition & 1 deletion test/jsonapi/view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions test/serializer_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule JSONAPISerializerTest do
use ExUnit.Case, async: false
alias JSONAPI.Serializer

alias JSONAPI.{Config, Serializer}

import ExUnit.CaptureLog

Expand Down Expand Up @@ -268,8 +269,8 @@ defmodule JSONAPISerializerTest do

conn = %Plug.Conn{
assigns: %{
jsonapi_query: %{
includes: [best_comments: :user]
jsonapi_query: %Config{
include: [best_comments: :user]
}
}
}
Expand All @@ -293,8 +294,8 @@ defmodule JSONAPISerializerTest do

conn = %Plug.Conn{
assigns: %{
jsonapi_query: %{
includes: [:company]
jsonapi_query: %Config{
include: [:company]
}
}
}
Expand All @@ -318,8 +319,8 @@ defmodule JSONAPISerializerTest do

conn = %Plug.Conn{
assigns: %{
jsonapi_query: %{
includes: [company: :industry]
jsonapi_query: %Config{
include: [company: :industry]
}
}
}
Expand Down Expand Up @@ -354,8 +355,8 @@ defmodule JSONAPISerializerTest do

conn = %Plug.Conn{
assigns: %{
jsonapi_query: %{
includes: [company: [industry: :tags]]
jsonapi_query: %Config{
include: [company: [industry: :tags]]
}
}
}
Expand Down
32 changes: 0 additions & 32 deletions test/utils/string_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 323eab3

Please sign in to comment.