diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..27134eb3 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,50 @@ +# Examples + +## buddy-auth +## frontend-auth +## frontend-controllers +## frontend-links +## frontend-prompt +## frontend-re-frame +## frontend +## http-swagger + +Coercion with Spec and Swagger generation. + +Same as ring-spec-swagger? + +Async examples as extra. + +## http + +Async example. + +## just-coercion-with-ring + +Bad name. + +Coercion example for spec, data-spec, Schema. +No Swagger generation or Malli! + +Same as ring-example? + +## pedestal-swagger +## pedestal +## ring-example + +Coercion example for spec, data-spec, Schema. +No Swagger generation or Malli! + +## ring-integrant + +## ring-malli-swagger + +Coercion with Malli and Swagger generation. + +## ring-spec-swagger + +Coercion with Spec and Swagger generation. + +## ring-swagger + +Coercion with Spec and Swagger generation. Same as previous! diff --git a/examples/ring-malli-swagger/src/example/server.clj b/examples/ring-malli-swagger/src/example/server.clj index 1a5d7d89..bc9c3861 100644 --- a/examples/ring-malli-swagger/src/example/server.clj +++ b/examples/ring-malli-swagger/src/example/server.clj @@ -56,13 +56,25 @@ ["/plus" {:get {:summary "plus with malli query parameters" - :parameters {:query [:map [:x int?] [:y int?]]} + :parameters {:query [:map + [:x + {:title "X parameter" + :description "Description for X parameter" + :json-schema/default 42} + int?] + [:y int?]]} :responses {200 {:body [:map [:total int?]]}} :handler (fn [{{{:keys [x y]} :query} :parameters}] {:status 200 :body {:total (+ x y)}})} :post {:summary "plus with malli body parameters" - :parameters {:body [:map [:x int?] [:y int?]]} + :parameters {:body [:map + [:x + {:title "X parameter" + :description "Description for X parameter" + :json-schema/default 42} + int?] + [:y int?]]} :responses {200 {:body [:map [:total int?]]}} :handler (fn [{{{:keys [x y]} :body} :parameters}] {:status 200 diff --git a/examples/ring-spec-swagger/src/example/server.clj b/examples/ring-spec-swagger/src/example/server.clj index 69615800..31ac2bc0 100644 --- a/examples/ring-spec-swagger/src/example/server.clj +++ b/examples/ring-spec-swagger/src/example/server.clj @@ -13,6 +13,7 @@ ; [reitit.ring.middleware.dev :as dev] ; [reitit.ring.spec :as spec] ; [spec-tools.spell :as spell] + [spec-tools.core :as st] [ring.adapter.jetty :as jetty] [muuntaja.core :as m] [clojure.spec.alpha :as s] @@ -25,7 +26,12 @@ (s/def ::size int?) (s/def ::file-response (s/keys :req-un [::name ::size])) -(s/def ::x int?) +;; Use data-specs to provide extra JSON-Schema properties: +;; https://github.com/metosin/spec-tools/blob/master/docs/04_json_schema.md#annotated-specs +(s/def ::x (st/spec {:spec int? + :name "X parameter" + :description "Description for X parameter" + :json-schema/default 42})) (s/def ::y int?) (s/def ::total int?) (s/def ::math-request (s/keys :req-un [::x ::y])) diff --git a/examples/ring-swagger/src/example/server.clj b/examples/ring-swagger/src/example/server.clj index 8416c2b4..58d01810 100644 --- a/examples/ring-swagger/src/example/server.clj +++ b/examples/ring-swagger/src/example/server.clj @@ -11,8 +11,6 @@ [reitit.ring.middleware.parameters :as parameters] ;; Uncomment to use ; [reitit.ring.middleware.dev :as dev] - ; [reitit.ring.spec :as spec] - ; [spec-tools.spell :as spell] [ring.adapter.jetty :as jetty] [muuntaja.core :as m] [clojure.java.io :as io])) @@ -53,13 +51,15 @@ ["/plus" {:get {:summary "plus with spec query parameters" - :parameters {:query {:x int?, :y int?}} + :parameters {:query {:x int? + :y int?}} :responses {200 {:body {:total int?}}} :handler (fn [{{{:keys [x y]} :query} :parameters}] {:status 200 :body {:total (+ x y)}})} :post {:summary "plus with spec body parameters" - :parameters {:body {:x int?, :y int?}} + :parameters {:body {:x int? + :y int?}} :responses {200 {:body {:total int?}}} :handler (fn [{{{:keys [x y]} :body} :parameters}] {:status 200 @@ -80,7 +80,8 @@ ;; encoding response body muuntaja/format-response-middleware ;; exception handling - exception/exception-middleware + (exception/create-exception-middleware + {::exception/default (partial exception/wrap-log-to-console exception/default-handler)}) ;; decoding request body muuntaja/format-request-middleware ;; coercing response bodys diff --git a/modules/reitit-interceptors/src/reitit/http/interceptors/exception.clj b/modules/reitit-interceptors/src/reitit/http/interceptors/exception.clj index f0a47a1d..89d07927 100644 --- a/modules/reitit-interceptors/src/reitit/http/interceptors/exception.clj +++ b/modules/reitit-interceptors/src/reitit/http/interceptors/exception.clj @@ -4,7 +4,7 @@ [clojure.spec.alpha :as s] [clojure.string :as str]) (:import (java.time Instant) - (java.io PrintWriter))) + (java.io PrintWriter Writer))) (s/def ::handlers (s/map-of any? fn?)) (s/def ::spec (s/keys :opt-un [::handlers])) @@ -35,7 +35,7 @@ (wrap error-handler error request) (error-handler error request)))) -(defn print! [^PrintWriter writer & more] +(defn print! [^Writer writer & more] (.write writer (str (str/join " " more) "\n"))) ;; @@ -68,7 +68,7 @@ (defn wrap-log-to-console [handler ^Throwable e {:keys [uri request-method] :as req}] (print! *out* (Instant/now) request-method (pr-str uri) "=>" (.getMessage e)) - (.printStackTrace e ^PrintWriter *out*) + (.printStackTrace e (PrintWriter. ^Writer *out*)) (handler e req)) ;; diff --git a/modules/reitit-middleware/src/reitit/ring/middleware/exception.clj b/modules/reitit-middleware/src/reitit/ring/middleware/exception.clj index 92bbdec5..b14d0fca 100644 --- a/modules/reitit-middleware/src/reitit/ring/middleware/exception.clj +++ b/modules/reitit-middleware/src/reitit/ring/middleware/exception.clj @@ -4,7 +4,7 @@ [clojure.spec.alpha :as s] [clojure.string :as str]) (:import (java.time Instant) - (java.io PrintWriter))) + (java.io Writer PrintWriter))) (s/def ::handlers (s/map-of any? fn?)) (s/def ::spec (s/keys :opt-un [::handlers])) @@ -55,7 +55,7 @@ (catch Throwable e (on-exception handlers e request respond raise))))))) -(defn print! [^PrintWriter writer & more] +(defn print! [^Writer writer & more] (.write writer (str (str/join " " more) "\n"))) ;; @@ -88,7 +88,7 @@ (defn wrap-log-to-console [handler ^Throwable e {:keys [uri request-method] :as req}] (print! *out* (Instant/now) request-method (pr-str uri) "=>" (.getMessage e)) - (.printStackTrace e ^PrintWriter *out*) + (.printStackTrace e (PrintWriter. ^Writer *out*)) (handler e req)) ;;