From 5796df494e66dc8a17c60d6495e948112d485f2d Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Sat, 11 Apr 2020 20:39:35 -0400 Subject: [PATCH 1/5] Change default-options-handler to default-options-resource To make the options resource more customizable. --- modules/reitit-ring/src/reitit/ring.cljc | 27 ++++++++++++++---------- test/cljc/reitit/ring_test.cljc | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 81c72b94..2411be75 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -28,11 +28,11 @@ (update acc method expand opts) acc)) data http-methods)]) -(defn compile-result [[path data] {::keys [default-options-handler] :as opts}] +(defn compile-result [[path data] {::keys [default-options-resource] :as opts}] (let [[top childs] (group-keys data) childs (cond-> childs - (and (not (:options childs)) (not (:handler top)) default-options-handler) - (assoc :options {:no-doc true, :handler default-options-handler})) + (and (not (:options childs)) (not (:handler top)) default-options-resource) + (assoc :options default-options-resource)) ->endpoint (fn [p d m s] (-> (middleware/compile-result [p d] opts s) (map->Endpoint) @@ -65,6 +65,10 @@ ([request respond _] (respond (handle request)))))) +(def default-options-resource + {:no-doc true + :handler default-options-handler}) + ;; ;; public api ;; @@ -74,11 +78,11 @@ support for http-methods and Middleware. See documentation on [[reitit.core/router]] for available options. In addition, the following options are available: - | key | description - | ---------------------------------------|------------- - | `:reitit.middleware/transform` | Function or vector of functions of type `[Middleware] => [Middleware]` to transform the expanded Middleware (default: identity) - | `:reitit.middleware/registry` | Map of `keyword => IntoMiddleware` to replace keyword references into Middleware - | `:reitit.ring/default-options-handler` | Default handler for `:options` method in endpoints (default: default-options-handler) + | key | description + | ----------------------------------------|------------- + | `:reitit.middleware/transform` | Function or vector of functions of type `[Middleware] => [Middleware]` to transform the expanded Middleware (default: identity) + | `:reitit.middleware/registry` | Map of `keyword => IntoMiddleware` to replace keyword references into Middleware + | `:reitit.ring/default-options-resource` | Default resource for `:options` method in endpoints (default: default-options-resource) Example: @@ -91,9 +95,10 @@ ([data] (router data nil)) ([data opts] - (let [opts (merge {:coerce coerce-handler - :compile compile-result - ::default-options-handler default-options-handler} opts)] + (let [opts (merge {:coerce coerce-handler + :compile compile-result + ::default-options-resource default-options-resource} + opts)] (r/router data opts)))) (defn routes diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 7c1d3432..e07d9d7d 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -241,7 +241,7 @@ [["/get" {:get (constantly response)}] ["/options" {:options (constantly response)}] ["/any" (constantly response)]] - {::ring/default-options-handler nil}))] + {::ring/default-options-resource nil}))] (testing "endpoint with a non-options handler" (is (= response (app {:request-method :get, :uri "/get"}))) From 4fa3e12b4939300cf9320a6b5b7fdd5842bcf24d Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Tue, 12 May 2020 10:37:13 -0400 Subject: [PATCH 2/5] resource -> endpoint --- modules/reitit-ring/src/reitit/ring.cljc | 12 ++++++------ test/cljc/reitit/ring_test.cljc | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 2411be75..50812e62 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -28,11 +28,11 @@ (update acc method expand opts) acc)) data http-methods)]) -(defn compile-result [[path data] {::keys [default-options-resource] :as opts}] +(defn compile-result [[path data] {::keys [default-options-endpoint] :as opts}] (let [[top childs] (group-keys data) childs (cond-> childs - (and (not (:options childs)) (not (:handler top)) default-options-resource) - (assoc :options default-options-resource)) + (and (not (:options childs)) (not (:handler top)) default-options-endpoint) + (assoc :options default-options-endpoint)) ->endpoint (fn [p d m s] (-> (middleware/compile-result [p d] opts s) (map->Endpoint) @@ -65,7 +65,7 @@ ([request respond _] (respond (handle request)))))) -(def default-options-resource +(def default-options-endpoint {:no-doc true :handler default-options-handler}) @@ -82,7 +82,7 @@ | ----------------------------------------|------------- | `:reitit.middleware/transform` | Function or vector of functions of type `[Middleware] => [Middleware]` to transform the expanded Middleware (default: identity) | `:reitit.middleware/registry` | Map of `keyword => IntoMiddleware` to replace keyword references into Middleware - | `:reitit.ring/default-options-resource` | Default resource for `:options` method in endpoints (default: default-options-resource) + | `:reitit.ring/default-options-endpoint` | Default resource for `:options` method in endpoints (default: default-options-endpoint) Example: @@ -97,7 +97,7 @@ ([data opts] (let [opts (merge {:coerce coerce-handler :compile compile-result - ::default-options-resource default-options-resource} + ::default-options-endpoint default-options-endpoint} opts)] (r/router data opts)))) diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index e07d9d7d..4a5ecbb4 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -241,7 +241,7 @@ [["/get" {:get (constantly response)}] ["/options" {:options (constantly response)}] ["/any" (constantly response)]] - {::ring/default-options-resource nil}))] + {::ring/default-options-endpoint nil}))] (testing "endpoint with a non-options handler" (is (= response (app {:request-method :get, :uri "/get"}))) From a98fe22992957af49a7c4029329350f6003984e5 Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Tue, 12 May 2020 10:37:47 -0400 Subject: [PATCH 3/5] style --- modules/reitit-ring/src/reitit/ring.cljc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 50812e62..2f63dfdc 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -95,8 +95,8 @@ ([data] (router data nil)) ([data opts] - (let [opts (merge {:coerce coerce-handler - :compile compile-result + (let [opts (merge {:coerce coerce-handler + :compile compile-result ::default-options-endpoint default-options-endpoint} opts)] (r/router data opts)))) From 5e3e552c80811f796ba4e6a352dbde1bf7ed3060 Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Tue, 12 May 2020 10:41:34 -0400 Subject: [PATCH 4/5] add assertion for breaking change --- modules/reitit-ring/src/reitit/ring.cljc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 2f63dfdc..3585caf8 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -99,6 +99,8 @@ :compile compile-result ::default-options-endpoint default-options-endpoint} opts)] + (assert (not (contains? opts ::default-options-handler)) + "Option `::default-options-handler` is deprecated. Use `::default-options-endpoint` instead.") (r/router data opts)))) (defn routes From 029894b9845f2237b89ba4fbe47f5d1f789ab458 Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Tue, 12 May 2020 10:45:14 -0400 Subject: [PATCH 5/5] testing assertion --- modules/reitit-ring/src/reitit/ring.cljc | 4 +++- test/cljc/reitit/ring_test.cljc | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 3585caf8..c408d891 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -100,7 +100,9 @@ ::default-options-endpoint default-options-endpoint} opts)] (assert (not (contains? opts ::default-options-handler)) - "Option `::default-options-handler` is deprecated. Use `::default-options-endpoint` instead.") + (str + "Option `:reitit.ring/default-options-handler` is deprecated." + " Use `:reitit.ring/default-options-endpoint` instead.")) (r/router data opts)))) (defn routes diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 4a5ecbb4..cdb919f9 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -205,6 +205,10 @@ (is (= 405 (:status (app {:request-method :post, :uri "/ping"})))))))))) (deftest default-options-handler-test + (testing "Assertion fails when using deprecated options-handler" + (is (thrown? java.lang.AssertionError (ring/router [] {::ring/default-options-handler (fn [_] )}))))) + +(deftest default-options-endpoint-test (let [response {:status 200, :body "ok"}] (testing "with defaults"