sheharyarn / better_params Goto Github PK
View Code? Open in Web Editor NEWCleaner request parameters in Elixir web applications ๐
License: MIT License
Cleaner request parameters in Elixir web applications ๐
License: MIT License
When the web request contains a %Plug.Upload{}
struct, it crashes the process with a Protocol.UndefinedError
:
[error] #PID<0.737.0> running MyApp.Endpoint terminated
Server: localhost:5000 (http)
Request: POST /some/server/path
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Enumerable not implemented for %Plug.Upload{content_type: "image/jpeg", filename: "123456789.jpg", path: "/var/folders/rr/1l6bz6rd6tq_h70m1scxy51m0000gn/T//plug-1502/multipart-837302-850377-2"}
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:116: Enumerable.reduce/3
(elixir) lib/enum.ex:1767: Enum.reduce/3
lib/better_params.ex:79: anonymous fn/2 in BetterParams.symbolize_keys/1
(stdlib) lists.erl:1263: :lists.foldl/3
lib/better_params.ex:68: BetterParams.symbolize_merge/1
lib/better_params.ex:36: BetterParams.call/2
...
Hey there! I really love the idea of this module, preferring the feel of atom keys by a mile, but it's creating some trouble with Ecto. In cases where I just want to dump a whole mess of params into a cast
call, Ecto throws an error on encountering mixed keys:
** (Ecto.CastError) expected params to be a map with atoms or string keys, got a map with mixed keys: %{:author_email => "[email protected]", :author_name => "Daniel P. Shannon", :body => "Foo bar baz quux.", :title => "Foo Bar Baz Quux", "author_email" => "[email protected]", "author_name" => "Daniel P. Shannon", "body" => "Foo bar baz quux.", "title" => "Foo Bar Baz Quux"}
(ecto) lib/ecto/changeset.ex:542: anonymous fn/3 in Ecto.Changeset.convert_params/1
(ecto) lib/ecto/changeset.ex:537: Ecto.Changeset.convert_params/1
(ecto) lib/ecto/changeset.ex:442: Ecto.Changeset.do_cast/7
(poppins_schema) lib/poppins_schema/crm/query.ex:16: PoppinsSchema.CRM.Query.submit_changeset/2
(poppins_schema) lib/poppins_schema/crm/crm.ex:7: PoppinsSchema.CRM.submit_query/1
(poppins_web) lib/poppins_web/controllers/crm/query_controller.ex:9: PoppinsWeb.CRM.QueryController.create/2
(poppins_web) lib/poppins_web/controllers/crm/query_controller.ex:1: PoppinsWeb.CRM.QueryController.action/2
Frankly this strikes me as a little silly of Ecto, but I was curious whether there was a way to either drop the duplicate fallback strings or get around the cast issue without explicitly rebuilding the params map for every endpoint.
(Sorry for a bit of a StackOverflow-y question, but I guess it's part feature request since I'd love to just omit the strings. ๐ฌ )
I tried your library, thank you for developing it, looks interesting.
One issue i found is that sometimes String.to_existing_atom
will fail and we would loose some keys, i think we need to handle this case in rescue
block, for example as String.to_atom
A quick look at your code, seems like using your ExUtils to convert any request parameter into atoms is a bad idea (unless you used something like String.to_existing_atom), otherwise it would be really easy to create something that generates random parameter keys and quickly fills your atom table as they are never freed.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.