Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Plug to Transform Parameters #149

Merged

Conversation

jherdman
Copy link
Contributor

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

test/jsonapi/plugs/transform_parameters_test.exs Outdated Show resolved Hide resolved
lib/jsonapi/plugs/transform_parameters.ex Outdated Show resolved Hide resolved
lib/jsonapi/utils/underscore.ex Outdated Show resolved Hide resolved
@jherdman
Copy link
Contributor Author

TODO

  • Add documentation to README
  • Resolve all feedback

Copy link
Contributor

@snewcomer snewcomer left a 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.

lib/jsonapi/utils/underscore.ex Outdated Show resolved Hide resolved
lib/jsonapi/plugs/transform_parameters.ex Outdated Show resolved Hide resolved
lib/jsonapi/plugs/transform_parameters.ex Outdated Show resolved Hide resolved
@jherdman jherdman force-pushed the transform-incoming-parameters branch 2 times, most recently from 0deefb1 to 28af691 Compare December 26, 2018 16:08
@jherdman jherdman force-pushed the transform-incoming-parameters branch from 28af691 to 13fb4d2 Compare December 26, 2018 16:32
test/jsonapi/plugs/transform_parameters_test.exs Outdated Show resolved Hide resolved
lib/jsonapi/plugs/transform_parameters.ex Outdated Show resolved Hide resolved
@jherdman jherdman force-pushed the transform-incoming-parameters branch from 13fb4d2 to e5c57a3 Compare December 27, 2018 03:27
@jherdman
Copy link
Contributor Author

Overall a user has to understand, to underscore params, add a plug. To underscore a response object, add a config option...

This asymmetry kind of stinks. Maybe we could optionally include the plug with the config option set?

@jherdman
Copy link
Contributor Author

jherdman commented Dec 27, 2018

OK, current set of TODOs I'll try to wrap up over the next while:

  • Automatically dasherize incoming params via the config option?
  • Resolve duplication in transforming params in the QueryParser plug
  • Resolve Add Plug to Transform Parameters #149 (comment) — should we do this here, or wait for the other PR?

"README.md",
"LICENSE.md"
],
main: "readme"
Copy link
Contributor Author

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.

@jherdman jherdman force-pushed the transform-incoming-parameters branch from b2a9bf7 to a4b62a6 Compare December 27, 2018 15:40
)
|> 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"
Copy link
Contributor

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
Copy link
Contributor

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 :trollface: But that will happen in a follow up PR.

end
def dash(value) do
value
end
Copy link
Contributor

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`
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

README.md Outdated Show resolved Hide resolved
@jherdman jherdman force-pushed the transform-incoming-parameters branch 2 times, most recently from 55a12ce to 74e2a39 Compare December 28, 2018 15:33
@jherdman
Copy link
Contributor Author

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?

@snewcomer
Copy link
Contributor

@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

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)
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@jherdman
Copy link
Contributor Author

jherdman commented Dec 28, 2018 via email

@jherdman
Copy link
Contributor Author

jherdman commented Dec 28, 2018 via email

@jherdman jherdman force-pushed the transform-incoming-parameters branch from 74e2a39 to 4b09c4a Compare December 28, 2018 17:06
@@ -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)
Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

README.md Outdated Show resolved Hide resolved
@@ -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)
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@jherdman jherdman force-pushed the transform-incoming-parameters branch from 4b09c4a to 6aa4d6f Compare December 30, 2018 01:18
@jherdman
Copy link
Contributor Author

Reverted the hard deprecation on QueryParser dasherizing. This wasn't a lot of fun when I tried this branch out in my project.

@jherdman jherdman force-pushed the transform-incoming-parameters branch 2 times, most recently from f603e42 to d89b1a4 Compare December 30, 2018 01:32
@jherdman
Copy link
Contributor Author

Initial testing has show that this also underscore values, e.g. if I POST /sources with the payload like this:

{
  "data": {
    "attributes": {
      "math-problem": "1-1=2"

I'll get:

{
  "data": {
    "attributes": {
      "math-problem": "1_1=2"

@jherdman jherdman force-pushed the transform-incoming-parameters branch 3 times, most recently from 18be57a to 89d8176 Compare December 30, 2018 15:07
@jherdman
Copy link
Contributor Author

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.

@doomspork
Copy link
Member

@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 👍

@jherdman
Copy link
Contributor Author

jherdman commented Dec 30, 2018 via email

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.
@jherdman jherdman force-pushed the transform-incoming-parameters branch from 89d8176 to 5d3110e Compare December 31, 2018 01:05
@jherdman
Copy link
Contributor Author

@doomspork reverted!

@jherdman
Copy link
Contributor Author

jherdman commented Jan 4, 2019

@doomspork anything else I can do? I'd love to put this PR to bed :)

@doomspork
Copy link
Member

@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.

Copy link
Contributor

@snewcomer snewcomer left a 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",
Copy link
Contributor

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?

Copy link
Member

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.

@doomspork doomspork merged commit bc11890 into beam-community:master Jan 4, 2019
@jherdman jherdman mentioned this pull request Jan 7, 2019
3 tasks
@jherdman jherdman deleted the transform-incoming-parameters branch January 8, 2019 13:45
@jherdman jherdman mentioned this pull request Jan 12, 2019
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants