-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Plug to Transform Parameters #149
Add Plug to Transform Parameters #149
Conversation
TODO
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work and could/will clean up a lot of custom plugs/transformations ppl may have used! So essentially we have incoming transformation and outgoing transformation. I think it might be good to make these obviously clear in the README. Overall a user has to understand, to underscore params, add a plug
. To underscore a response object, add a config option
.
0deefb1
to
28af691
Compare
28af691
to
13fb4d2
Compare
13fb4d2
to
e5c57a3
Compare
This asymmetry kind of stinks. Maybe we could optionally include the plug with the config option set? |
OK, current set of TODOs I'll try to wrap up over the next while:
|
"README.md", | ||
"LICENSE.md" | ||
], | ||
main: "readme" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope this is okay. There are so many goodies in this file, so we may as well include it.
b2a9bf7
to
a4b62a6
Compare
) | ||
|> ContentTypeNegotiation.call([]) | ||
|
||
assert conn.halted | ||
|
||
assert Plug.Conn.get_resp_header(conn, "content-type") == [ | ||
"application/vnd.api+json; charset=utf-8" | ||
"#{mime_type()}; charset=utf-8" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice cleanup 👍
README.md
Outdated
|
||
## Dasherizing Fields | ||
|
||
JSONAPI now recommends the use of dashes (`-`) in place of underscore (`_`) as a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note this is now camelCase But that will happen in a follow up PR.
end | ||
def dash(value) do | ||
value | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
following up on the previous conversation, I think the swap of method names could come in another PR or in #137. This looks great as is!
README.md
Outdated
JSONAPI now recommends the use of dashes (`-`) in place of underscore (`_`) as a | ||
word separator. Enabling this requires two quick steps: | ||
|
||
1. Dasherizing *outgoing* fields requires you to set the `underscore_to_dash` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
55a12ce
to
74e2a39
Compare
I'm pretty sure this is in a "done" state now. I'd like to recommend that a breaking 1.0 release project be opened as a result of this PR. Maybe we should sneak in a few other breaking changes whilst we're at it? |
@jherdman Nice work! Sry for asking, but what is the breaking change here? This just adds functionality, right? I most likely missed something. Also, I think there are a few items we could think about for 1.0.
|
README.md
Outdated
@@ -19,7 +19,7 @@ A project that will render your data models into [JSONAPI Documents](http://json | |||
|
|||
## Documentation | |||
|
|||
[Full docs here](https://hexdocs.pm/jsonapi/api-reference.html) | |||
[Full docs here](https://hexdocs.pm/jsonapi) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include a link to the actual JSONAPI spec here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
In this PR just that one MUST use this new plug to get underscored attrs.
This means QueryParser doesn't do this on it's own (I can revert this if
you'd like).
On master: the deprecations module was renamed (we should also mark this as
private if it isn't); we moved one plug, and I think renamed it.
On Fri., Dec. 28, 2018, 11:12 Scott Newcomer <notifications@github.com
wrote:
@jherdman <https://github.com/jherdman> Nice work! Sry for asking, but
what is the breaking change here? This just adds functionality, right? I
most likely missed something.
Also, I think there are a few items we *could* think about for 1.0.
1. profile links https://jsonapi.org/format/1.1/. (~January 2019)
albeit non breaking
2. camelCase member names (if we decide to switch over to). This is
totally up in the air and we could stick with conventions in the Elixir
community
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#149 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAM5PEMsJHGX-zwbSI_m5Df-dI_w2J5ks5u9kLfgaJpZM4Zgl0P>
.
On Dec. 28, 2018 11:12, "Scott Newcomer" <notifications@github.com> wrote:
@jherdman <https://github.com/jherdman> Nice work! Sry for asking, but what
is the breaking change here? This just adds functionality, right? I most
likely missed something.
Also, I think there are a few items we *could* think about for 1.0.
1. profile links https://jsonapi.org/format/1.1/. (~January 2019) albeit
non breaking
2. camelCase member names (if we decide to switch over to). This is
totally up in the air and we could stick with conventions in the Elixir
community
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#149 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAM5PEMsJHGX-zwbSI_m5Df-dI_w2J5ks5u9kLfgaJpZM4Zgl0P>
.
|
Totally down with that. I'll do so ASAP. It'd probably be prudent to note
which versions of the spec are supported.
…On Fri., Dec. 28, 2018, 11:21 Sean Callan ***@***.*** wrote:
***@***.**** commented on this pull request.
------------------------------
In README.md
<#149 (comment)>:
> @@ -19,7 +19,7 @@ A project that will render your data models into [JSONAPI Documents](http://json
## Documentation
-[Full docs here](https://hexdocs.pm/jsonapi/api-reference.html)
+[Full docs here](https://hexdocs.pm/jsonapi)
Should we include a link to the actual JSONAPI spec here?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#149 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAM5JleT4t6P0cZ7G7s_iR4ry9zBiHaks5u9kUMgaJpZM4Zgl0P>
.
|
74e2a39
to
4b09c4a
Compare
lib/jsonapi/plugs/query_parser.ex
Outdated
@@ -179,7 +186,7 @@ defmodule JSONAPI.QueryParser do | |||
if inc =~ ~r/\w+\.\w+/ do | |||
acc ++ handle_nested_include(inc, valid_includes, config) | |||
else | |||
inc = inc |> dash() |> String.to_existing_atom() | |||
inc = String.to_existing_atom(inc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snewcomer here's an example of a backwards incompat change. I could add it back in with a deprecation warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are doing the whole shebang, probably ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh... I should have checked the PR first. I can go back to the hard deprecation if you'd prefer.
lib/jsonapi/plugs/query_parser.ex
Outdated
@@ -179,7 +186,7 @@ defmodule JSONAPI.QueryParser do | |||
if inc =~ ~r/\w+\.\w+/ do | |||
acc ++ handle_nested_include(inc, valid_includes, config) | |||
else | |||
inc = inc |> dash() |> String.to_existing_atom() | |||
inc = String.to_existing_atom(inc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are doing the whole shebang, probably ok.
@@ -80,6 +84,30 @@ when Ecto gets more complex field selection support we will go further to only q | |||
|
|||
You will need to handle filtering yourself, the filter is just a map with key=value. | |||
|
|||
## Dasherized Fields | |||
|
|||
JSONAPI now recommends the use of dashes (`-`) in place of underscore (`_`) as a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be curious as to @doomspork or @jeregrine opinion what the library case
default should be and if we should take into consideration camelCase recommendation now. Or should we stick with general Elixir conventions for underscore and leave as it, providing configuration on top to convert to dash
or camelCase
based on what the consumer wants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As someone using this lib I'd prefer to opt into whatever I want, even on a request level. Imagine an API like Stripe's where you tell the API what version of it you're expecting. In such a case I'd want to either dasherize for old clients, or camelize for modern ones. FWIW as a default I'd love to see at least dashes given that this is the v1.0 recommendation.
4b09c4a
to
6aa4d6f
Compare
Reverted the hard deprecation on |
f603e42
to
d89b1a4
Compare
Initial testing has show that this also underscore values, e.g. if I
I'll get:
|
18be57a
to
89d8176
Compare
I've beefed up the tests to account for the above problems, and am successfully using this branch in a project. I'm reasonably sure this is good to go unless there's any more feedback. |
@jherdman can you please undo the change |
The reason for this change is to include it in the docs. ExDoc gets cranky
otherwise.
Apologies about this PR ballooning.
…On Sun., Dec. 30, 2018, 11:04 Sean Callan ***@***.*** wrote:
@jherdman <https://github.com/jherdman> can you please undo the change LICENSE
→ LICENSE.md, we do not need to add a .md extension. There's really no
reason to make these changes as part of this PR. Please *please* keep PRs
focused on the work you're doing and avoid unnecessary changes 👍
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#149 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAM5Arg5eVQPI-4BwTJKrclQuucIwIxks5u-OQHgaJpZM4Zgl0P>
.
|
This, optional, Plug can be used to transform incoming dasherized parameters to underscored ones that are more amenable to usage in your application. Resolves beam-community#145
Includes docs for the new `UnderscoreParamters` plug, and tweaks a few other things to make the narrative slightly better.
89d8176
to
5d3110e
Compare
@doomspork reverted! |
@doomspork anything else I can do? I'd love to put this PR to bed :) |
@snewcomer any final thoughts before I merge this? I believe we will need @jeregrine to cut a release once we have everything merged and ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice work @jherdman.
|
||
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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add ...dasherize incoming parameters as of version 1.0.0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do that in a separate PR so @jherdman can get this off his plate.
This, optional, Plug can be used to transform incoming dasherized
parameters to underscored ones that are more amenable to usage in your
application.
Resolves #145