From 509697dc5580742b2cbc44603d6e98fab36b076f Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 1 Jan 2018 22:10:15 +0200 Subject: [PATCH 1/4] Cleanup examples --- examples/just-coercion-with-ring/project.clj | 2 +- examples/just-coercion-with-ring/src/example/server.clj | 8 +++++--- examples/ring-example/project.clj | 6 +++--- examples/ring-example/src/example/dspec.clj | 4 ++-- examples/ring-example/src/example/schema.clj | 4 ++-- examples/ring-example/src/example/spec.clj | 4 ++-- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/just-coercion-with-ring/project.clj b/examples/just-coercion-with-ring/project.clj index 11bbb381..9e0c652d 100644 --- a/examples/just-coercion-with-ring/project.clj +++ b/examples/just-coercion-with-ring/project.clj @@ -1,6 +1,6 @@ (defproject just-coercion-with-ring "0.1.0-SNAPSHOT" :description "Reitit coercion with vanilla ring" - :dependencies [[org.clojure/clojure "1.9.0-RC2"] + :dependencies [[org.clojure/clojure "1.9.0"] [ring "1.6.3"] [metosin/muuntaja "0.4.1"] [metosin/reitit "0.1.0-SNAPSHOT"]]) diff --git a/examples/just-coercion-with-ring/src/example/server.clj b/examples/just-coercion-with-ring/src/example/server.clj index 6066b84c..3697012c 100644 --- a/examples/just-coercion-with-ring/src/example/server.clj +++ b/examples/just-coercion-with-ring/src/example/server.clj @@ -1,5 +1,7 @@ (ns example.server (:require [ring.adapter.jetty :as jetty] + [muuntaja.middleware] + [ring.middleware.params] [reitit.middleware :as middleware] [reitit.ring.coercion :as rrc])) @@ -10,9 +12,9 @@ ;; to be set with :extract-request-format and extract-response-format (defn wrap-coercion [handler resource] (middleware/chain - [rrc/coerce-request-middleware - rrc/coerce-response-middleware - rrc/coerce-exceptions-middleware] + [rrc/coerce-exceptions-middleware + rrc/coerce-request-middleware + rrc/coerce-response-middleware] handler resource)) diff --git a/examples/ring-example/project.clj b/examples/ring-example/project.clj index 11bbb381..1035eafa 100644 --- a/examples/ring-example/project.clj +++ b/examples/ring-example/project.clj @@ -1,6 +1,6 @@ -(defproject just-coercion-with-ring "0.1.0-SNAPSHOT" - :description "Reitit coercion with vanilla ring" - :dependencies [[org.clojure/clojure "1.9.0-RC2"] +(defproject ring-example "0.1.0-SNAPSHOT" + :description "Reitit Ring App" + :dependencies [[org.clojure/clojure "1.9.0"] [ring "1.6.3"] [metosin/muuntaja "0.4.1"] [metosin/reitit "0.1.0-SNAPSHOT"]]) diff --git a/examples/ring-example/src/example/dspec.clj b/examples/ring-example/src/example/dspec.clj index 7b32d737..b463bc80 100644 --- a/examples/ring-example/src/example/dspec.clj +++ b/examples/ring-example/src/example/dspec.clj @@ -1,8 +1,8 @@ (ns example.dspec - (:require [reitit.coercion.spec :as spec-coercion])) + (:require [reitit.coercion.spec])) (def routes - ["/dspec" {:coercion spec-coercion/coercion} + ["/dspec" {:coercion reitit.coercion.spec/coercion} ["/plus" {:name ::plus :responses {200 {:schema {:total int?}}} :get {:summary "plus with query-params" diff --git a/examples/ring-example/src/example/schema.clj b/examples/ring-example/src/example/schema.clj index bd773f08..977a7b0c 100644 --- a/examples/ring-example/src/example/schema.clj +++ b/examples/ring-example/src/example/schema.clj @@ -1,9 +1,9 @@ (ns example.schema (:require [schema.core :as s] - [reitit.coercion.schema :as schema-coercion])) + [reitit.coercion.schema])) (def routes - ["/schema" {:coercion schema-coercion/coercion} + ["/schema" {:coercion reitit.coercion.schema/coercion} ["/plus" {:name ::plus :responses {200 {:schema {:total s/Int}}} :get {:summary "plus with query-params" diff --git a/examples/ring-example/src/example/spec.clj b/examples/ring-example/src/example/spec.clj index 6962041f..d8cfb3af 100644 --- a/examples/ring-example/src/example/spec.clj +++ b/examples/ring-example/src/example/spec.clj @@ -1,7 +1,7 @@ (ns example.spec (:require [clojure.spec.alpha :as s] [spec-tools.spec :as spec] - [reitit.coercion.spec :as spec-coercion])) + [reitit.coercion.spec])) ;; wrap into Spec Records to enable runtime conforming (s/def ::x spec/int?) @@ -9,7 +9,7 @@ (s/def ::total spec/int?) (def routes - ["/spec" {:coercion spec-coercion/coercion} + ["/spec" {:coercion reitit.coercion.spec/coercion} ["/plus" {:name ::plus :responses {200 {:schema (s/keys :req-un [::total])}} :get {:summary "plus with query-params" From 32fb3ca2941efc495a7500136780f3dc62874154 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 1 Jan 2018 22:10:49 +0200 Subject: [PATCH 2/4] :responses have :body, not :schema --- README.md | 2 +- doc/ring/coercion.md | 2 +- examples/ring-example/src/example/dspec.clj | 2 +- examples/ring-example/src/example/schema.clj | 2 +- examples/ring-example/src/example/spec.clj | 2 +- modules/reitit-core/src/reitit/coercion.cljc | 10 +++++----- modules/reitit-core/src/reitit/spec.cljc | 4 ++-- perf-test/clj/reitit/coercion_perf_test.clj | 12 ++++++------ test/cljc/reitit/ring_coercion_test.cljc | 8 ++++---- test/cljc/reitit/ring_spec_test.cljc | 2 +- test/cljc/reitit/spec_test.cljc | 8 ++++---- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a123dba7..bfc54cb2 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ A Ring routing app with input & output coercion using [data-specs](https://githu (ring/router ["/api" ["/math" {:get {:parameters {:query {:x int?, :y int?}} - :responses {200 {:schema {:total pos-int?}}} + :responses {200 {:body {:total pos-int?}}} :handler (fn [{{{:keys [x y]} :query} :parameters}] {:status 200 :body {:total (+ x y)}})}}]] diff --git a/doc/ring/coercion.md b/doc/ring/coercion.md index 9c9f45c2..8e8fa6ec 100644 --- a/doc/ring/coercion.md +++ b/doc/ring/coercion.md @@ -39,7 +39,7 @@ Handler can access the coerced parameters can be read under `:parameters` key in :parameters {:query {:x s/Int} :body {:y s/Int} :path {:z s/Int}} - :responses {200 {:schema {:total PositiveInt}}} + :responses {200 {:body {:total PositiveInt}}} :handler (fn [{:keys [parameters]}] (let [total (+ (-> parameters :query :x) (-> parameters :body :y) diff --git a/examples/ring-example/src/example/dspec.clj b/examples/ring-example/src/example/dspec.clj index b463bc80..0c8ffa03 100644 --- a/examples/ring-example/src/example/dspec.clj +++ b/examples/ring-example/src/example/dspec.clj @@ -4,7 +4,7 @@ (def routes ["/dspec" {:coercion reitit.coercion.spec/coercion} ["/plus" {:name ::plus - :responses {200 {:schema {:total int?}}} + :responses {200 {:body {:total int?}}} :get {:summary "plus with query-params" :parameters {:query {:x int?, :y int?}} :handler (fn [{{{:keys [x y]} :query} :parameters}] diff --git a/examples/ring-example/src/example/schema.clj b/examples/ring-example/src/example/schema.clj index 977a7b0c..13ac4125 100644 --- a/examples/ring-example/src/example/schema.clj +++ b/examples/ring-example/src/example/schema.clj @@ -5,7 +5,7 @@ (def routes ["/schema" {:coercion reitit.coercion.schema/coercion} ["/plus" {:name ::plus - :responses {200 {:schema {:total s/Int}}} + :responses {200 {:body {:total s/Int}}} :get {:summary "plus with query-params" :parameters {:query {:x s/Int, :y s/Int}} :handler (fn [{{{:keys [x y]} :query} :parameters}] diff --git a/examples/ring-example/src/example/spec.clj b/examples/ring-example/src/example/spec.clj index d8cfb3af..61a43020 100644 --- a/examples/ring-example/src/example/spec.clj +++ b/examples/ring-example/src/example/spec.clj @@ -11,7 +11,7 @@ (def routes ["/spec" {:coercion reitit.coercion.spec/coercion} ["/plus" {:name ::plus - :responses {200 {:schema (s/keys :req-un [::total])}} + :responses {200 {:body (s/keys :req-un [::total])}} :get {:summary "plus with query-params" :parameters {:query (s/keys :req-un [::x ::y])} :handler (fn [{{{:keys [x y]} :query} :parameters}] diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index 42461f84..4560db84 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -89,10 +89,10 @@ (request-coercion-failed! result coercion value in request) result)))))) -(defn response-coercer [coercion model {:keys [extract-response-format] - :or {extract-response-format (constantly nil)}}] +(defn response-coercer [coercion body {:keys [extract-response-format] + :or {extract-response-format (constantly nil)}}] (if coercion - (let [coercer (-response-coercer coercion model)] + (let [coercer (-response-coercer coercion body)] (fn [request response] (let [format (extract-response-format request response) value (:body response) @@ -126,8 +126,8 @@ (into {}))) (defn response-coercers [coercion responses opts] - (->> (for [[status {:keys [schema]}] responses :when schema] - [status (response-coercer coercion schema opts)]) + (->> (for [[status {:keys [body]}] responses :when body] + [status (response-coercer coercion body opts)]) (into {}))) (defn- coercers-not-compiled! [match] diff --git a/modules/reitit-core/src/reitit/spec.cljc b/modules/reitit-core/src/reitit/spec.cljc index 88fe97af..d3361f0d 100644 --- a/modules/reitit-core/src/reitit/spec.cljc +++ b/modules/reitit-core/src/reitit/spec.cljc @@ -92,10 +92,10 @@ (s/def :reitit.core.coercion/status (s/or :number number? :default #{:default})) -(s/def :reitit.core.coercion/schema any?) +(s/def :reitit.core.coercion/body any?) (s/def :reitit.core.coercion/description string?) (s/def :reitit.core.coercion/response - (s/keys :opt-un [:reitit.core.coercion/schema + (s/keys :opt-un [:reitit.core.coercion/body :reitit.core.coercion/description])) (s/def :reitit.core.coercion/responses (s/map-of :reitit.core.coercion/status :reitit.core.coercion/response)) diff --git a/perf-test/clj/reitit/coercion_perf_test.clj b/perf-test/clj/reitit/coercion_perf_test.clj index 71e97a4f..e91e660e 100644 --- a/perf-test/clj/reitit/coercion_perf_test.clj +++ b/perf-test/clj/reitit/coercion_perf_test.clj @@ -94,7 +94,7 @@ (suite (str (if coercion (protocol/get-name coercion)))) (let [routes ["/api" ["/ping" {:parameters {:body {:x int?, :y int?}} - :responses {200 {:schema {:total pos-int?}}} + :responses {200 {:body {:total pos-int?}}} :get {:handler (fn [request] (let [{:keys [x y]} (-> request :parameters :body)] {:status 200 @@ -152,7 +152,7 @@ (ring/router ["/api" ["/ping" {:parameters {:body {:x int?, :y int?}} - :responses {200 {:schema {:total pos-int?}}} + :responses {200 {:body {:total pos-int?}}} :get {:handler (fn [{{{:keys [x y]} :body} :parameters}] {:status 200 :body {:total (+ x y)}})}}]] @@ -199,7 +199,7 @@ (let [m (m/create (jsonista-format/with-json-format m/default-options)) app (ring/ring-handler (ring/router - ["/plus" {:post {:responses {200 {:schema {:result Long}}} + ["/plus" {:post {:responses {200 {:body {:result Long}}} :parameters {:body {:x Long, :y Long}} :handler (fn [request] (let [body (-> request :parameters :body)] @@ -224,7 +224,7 @@ (title "schema") (let [app (ring/ring-handler (ring/router - ["/plus" {:post {:responses {200 {:schema {:result Long}}} + ["/plus" {:post {:responses {200 {:body {:result Long}}} :parameters {:body {:x Long, :y Long}} :handler (fn [request] (let [body (-> request :parameters :body)] @@ -248,7 +248,7 @@ (title "data-spec") (let [app (ring/ring-handler (ring/router - ["/plus" {:post {:responses {200 {:schema {:result int?}}} + ["/plus" {:post {:responses {200 {:body {:result int?}}} :parameters {:body {:x int?, :y int?}} :handler (fn [request] (let [body (-> request :parameters :body)] @@ -277,7 +277,7 @@ (title "spec") (let [app (ring/ring-handler (ring/router - ["/plus" {:post {:responses {200 {:schema ::response}} + ["/plus" {:post {:responses {200 {:body ::response}} :parameters {:body ::request} :handler (fn [request] (let [body (-> request :parameters :body)] diff --git a/test/cljc/reitit/ring_coercion_test.cljc b/test/cljc/reitit/ring_coercion_test.cljc index 1909f786..a3e708ea 100644 --- a/test/cljc/reitit/ring_coercion_test.cljc +++ b/test/cljc/reitit/ring_coercion_test.cljc @@ -47,7 +47,7 @@ :form {:c int?} :header {:d int?} :path {:e int?}} - :responses {200 {:schema {:total pos-int?}}} + :responses {200 {:body {:total pos-int?}}} :handler handler}}]] {:data {:middleware middleware :coercion spec/coercion}})))] @@ -102,7 +102,7 @@ :form {:c s/Int} :header {:d s/Int} :path {:e s/Int}} - :responses {200 {:schema {:total (s/constrained s/Int pos? 'positive)}}} + :responses {200 {:body {:total (s/constrained s/Int pos? 'positive)}}} :handler handler}}]] {:data {:middleware middleware :coercion schema/coercion}})))] @@ -139,9 +139,9 @@ (app valid-request)))) (testing "invalid request" - (let [{:keys [status body]} (app invalid-request)] + (let [{:keys [status]} (app invalid-request)] (is (= 400 status)))) (testing "invalid response" - (let [{:keys [status body]} (app invalid-request2)] + (let [{:keys [status]} (app invalid-request2)] (is (= 500 status)))))))))) diff --git a/test/cljc/reitit/ring_spec_test.cljc b/test/cljc/reitit/ring_spec_test.cljc index b965420b..2c930728 100644 --- a/test/cljc/reitit/ring_spec_test.cljc +++ b/test/cljc/reitit/ring_spec_test.cljc @@ -89,7 +89,7 @@ :form {:c string?} :header {:d string?} :path {:e string?}} - :responses {200 {:schema {:total pos-int?}}} + :responses {200 {:body {:total pos-int?}}} :handler identity}}]] {:data {:middleware [rrc/coerce-exceptions-middleware rrc/coerce-request-middleware diff --git a/test/cljc/reitit/spec_test.cljc b/test/cljc/reitit/spec_test.cljc index 3fb68d6a..62012cd2 100644 --- a/test/cljc/reitit/spec_test.cljc +++ b/test/cljc/reitit/spec_test.cljc @@ -116,15 +116,15 @@ (is (s/valid? ::rs/responses - {:responses {200 {:description "ok", :schema string?} + {:responses {200 {:description "ok", :body string?} 400 {:description "fail"} - 500 {:schema string?} + 500 {:body string?} :default {}}})) (is (not (s/valid? ::rs/responses - {:responses {"200" {:description "ok", :schema string?}}}))) + {:responses {"200" {:description "ok", :body string?}}}))) (is (not (s/valid? ::rs/responses - {:responses {200 {:description :ok, :schema string?}}})))) + {:responses {200 {:description :ok, :body string?}}})))) From bd7577a949387ebe6a5cba0159a2a8f2eb6dc669 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 11 Feb 2018 21:40:48 +0200 Subject: [PATCH 3/4] fix example --- doc/ring/coercion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/ring/coercion.md b/doc/ring/coercion.md index 8e8fa6ec..6d584781 100644 --- a/doc/ring/coercion.md +++ b/doc/ring/coercion.md @@ -81,7 +81,7 @@ Here's an full example for applying coercion with Reitit, Ring and Schema: :parameters {:query {:x s/Int} :body {:y s/Int} :path {:z s/Int}} - :responses {200 {:schema {:total PositiveInt}}} + :responses {200 {:body {:total PositiveInt}}} :handler (fn [{:keys [parameters]}] (let [total (+ (-> parameters :query :x) (-> parameters :body :y) From c50d95ec3e6011a75bcd2084daa69be8bfd10463 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 11 Feb 2018 21:44:47 +0200 Subject: [PATCH 4/4] . --- modules/reitit-spec/src/reitit/coercion/spec.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/reitit-spec/src/reitit/coercion/spec.cljc b/modules/reitit-spec/src/reitit/coercion/spec.cljc index e81a24fa..7bee7da6 100644 --- a/modules/reitit-spec/src/reitit/coercion/spec.cljc +++ b/modules/reitit-spec/src/reitit/coercion/spec.cljc @@ -77,7 +77,7 @@ (into (empty responses) (for [[k response] responses] - [k (update response :schema #(coercion/-compile-model this % nil))]))))) + [k (update response :body #(coercion/-compile-model this % nil))]))))) (-compile-model [_ model name] (into-spec model (or name (gensym "spec")))) (-open-model [_ spec] spec)