Skip to content

Commit

Permalink
URI decode path params (#28)
Browse files Browse the repository at this point in the history
* URI decode path params

* Update changelog (will update day before merging)

Bump VERSION const to match actual version
  • Loading branch information
Blacksmoke16 committed Jan 8, 2021
1 parent a322cc0 commit 54af324
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2021-01-?? v0.4.4 - URI decode path params

Bugfix
* URI decodes path params before storing them in the result set's `#params` hash

2020-12-07 v0.4.3 - Crystal 1.0.0 compliance

Changes
* Updates crystal version to be installable in Crystal versions greater than 1.0.0
* Updates ameba to `0.13.x`

2020-04-27 v0.4.2 - Updates for Crystal 0.34.0

Changes
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: amber_router
version: 0.4.3
version: 0.4.4

authors:
- Robert L Carpenter <robert@robacarp.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ describe "glob parameters" do
"url" => "products/1",
})
end

it "URI decodes the parameters" do
router = build do
add "/get/categories/*categories/products", :categories_products
end

result = router.find "/get/categories/hats/scarfs%20&%20mittens/products"
result.params.should eq({"categories" => "hats/scarfs & mittens"})
end
end
9 changes: 9 additions & 0 deletions spec/amber_router/route_set/parameters/parameters_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ describe "parameters" do
result = router.find("/get/name/robert_paulson")
result.params.should eq({"name" => "robert_paulson"})
end

it "URI decodes the parameters" do
router = build do
add "/path/:value", :path_value
end

result = router.find "/path/foo%20bar"
result.params.should eq({"value" => "foo bar"})
end
end
7 changes: 5 additions & 2 deletions src/amber_router/route_set.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "uri"

module Amber::Router
# A tree which stores and navigates routes associated with a web application.
#
Expand Down Expand Up @@ -86,15 +88,15 @@ module Amber::Router

matched_routes = segment.route_set.select_routes(path, path_offset + 1)
matched_routes.each do |matched_route|
matched_route[segment.parameter] = path[path_offset] if segment.parametric?
matched_route[segment.parameter] = URI.decode path[path_offset] if segment.parametric?
matches << matched_route
end
when GlobSegment(T)
glob_matches = segment.route_set.reverse_select_routes(path)

glob_matches.each do |glob_match|
if segment.parametric?
glob_match.routed_result[segment.parameter] = path[path_offset..glob_match.match_position].join('/')
glob_match.routed_result[segment.parameter] = URI.decode path[path_offset..glob_match.match_position].join('/')
end

matches << glob_match.routed_result
Expand Down Expand Up @@ -123,6 +125,7 @@ module Amber::Router
glob_matches.each do |glob_match|
if segment.match? glob_match.current_segment
if segment.parametric?
# Defer decoding path paramter to `#select_routes` to avoid double decoding.
glob_match.routed_result[segment.parameter] = glob_match.current_segment
end

Expand Down
2 changes: 1 addition & 1 deletion src/amber_router/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Amber::Router
VERSION = "0.4.2"
VERSION = "0.4.4"
end

0 comments on commit 54af324

Please sign in to comment.