Skip to content

Commit

Permalink
Moving params in to HTTP::Server::Context so it's available anywhere …
Browse files Browse the repository at this point in the history
…context is, and ensures we only parse once per request. Fixes #1473 (#1509)
  • Loading branch information
jwoertink authored May 29, 2021
1 parent d976793 commit b54bc3c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
13 changes: 13 additions & 0 deletions spec/lucky/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,17 @@ describe Lucky::Params do
params.should eq({"filter" => {"name" => "euphonium"}, "page" => "1", "per" => "50"})
end
end

describe "Setting route_params later" do
it "returns the correct values for get?" do
request = build_request body: "", content_type: ""
route_params = {"id" => "from_route"}
params = Lucky::Params.new(request)

params.get?(:id).should eq nil

params.route_params = route_params
params.get?(:id).should eq "from_route"
end
end
end
1 change: 1 addition & 0 deletions src/lucky/action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abstract class Lucky::Action
getter :context, :route_params

def initialize(@context : HTTP::Server::Context, @route_params : Hash(String, String))
context.params.route_params = @route_params
end

abstract def call
Expand Down
6 changes: 6 additions & 0 deletions src/lucky/context_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class HTTP::Server::Context
def flash : Lucky::FlashStore
@_flash ||= Lucky::FlashStore.from_session(session)
end

@_params : Lucky::Params?

def params : Lucky::Params
@_params ||= Lucky::Params.new(request)
end
end
4 changes: 2 additions & 2 deletions src/lucky/http_method_override_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Lucky::HttpMethodOverrideHandler
end

private def override_allowed?(context, http_method) : Bool
["POST"].includes?(context.request.method) && ["PATCH", "PUT", "DELETE"].includes?(http_method)
(context.request.method == "POST") && ["PATCH", "PUT", "DELETE"].includes?(http_method)
end

private def overridden_http_method(context) : String?
Lucky::Params.new(context.request).get?(:_method).try(&.upcase)
context.params.get?(:_method).try(&.upcase)
rescue Lucky::ParamParsingError
nil
end
Expand Down
6 changes: 2 additions & 4 deletions src/lucky/param_helpers.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Lucky::ParamHelpers
@_params : Lucky::Params?

def params : Lucky::Params
@_params ||= Lucky::Params.new(context.request, @route_params)
memoize def params : Lucky::Params
context.params
end
end
10 changes: 4 additions & 6 deletions src/lucky/params.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class Lucky::Params
@request : HTTP::Request
@route_params : Hash(String, String) = {} of String => String

# :nodoc:
private getter :request
private getter request : HTTP::Request
# :nodoc:
private getter :route_params
private getter route_params : Hash(String, String)
setter :route_params

# Create a new params object
#
Expand All @@ -20,7 +18,7 @@ class Lucky::Params
#
# Lucky::Params.new(request, route_params)
# ```
def initialize(@request, @route_params = {} of String => String)
def initialize(@request : HTTP::Request, @route_params : Hash(String, String) = empty_params)
end

# Parses the request body as `JSON::Any` or raises `Lucky::ParamParsingError` if JSON is invalid.
Expand Down

0 comments on commit b54bc3c

Please sign in to comment.