-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: #310 The framework should be able to parse JSON request body appropriately by returning a `JSON::Any` object. Currently, there is a bug when trying to parse JSON parameters from the body of a request where nested JSON payload is not being parsed correctly ```crystal { "test": "test", "address": { "city": "New York" } params["address"].as(Hash)["city"] ``` Params is now a custom Hash of the following signature `Hash(String | Symbol, Amber::Router::ParamsType)` or `Amber::Router::ParamsHash.new` To give more context all parameters where being parsed using the **HTTP::Params.parse** method which returns an object of type **Hash(String, Array(String))** this caused issues when parsing JSON params from the body, It turns out that nested JSON keys where being parsed as a string. Basically, all `params` values are currently returning as a string. With the changes presented in this PR users can now define model objects with JSON mapping that should be evaluated correctly based on the mapping defined. So for instance: ```crystal params["user_id"].as(Int64) => 123 params["address"].as(Hash).each ... params["user"].as(User) => User.name User.from_json(params["user"]) => User.name ``` **Controller** The controller now generates the app using params.validation similar to Rails strong params. It adds a method to the Controller and overloads the Granite initializer and set_attributes methods. ```crystal private def animal_params params.validation do required("name") { |v| v.str? & !v.empty? } end end animal = Animal.new(animal_params.validate!) ``` Model Models generate for the Granite::ORM now includes the following line include Amber::Models::Granite this will allow models to be populated with the controller params using the params.validations. The reason this is added here and not to the Granite::ORM repo is to keep Granite loosely coupled from any framework since the params.validation is specific to amber. ```crystal class Animal < Granite::ORM include Amber::Models::Granite end ```
- Loading branch information
Showing
16 changed files
with
136 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.