Merge branch 'master' into frontend-fixes

This commit is contained in:
Juho Teperi 2020-10-15 09:49:14 +03:00 committed by GitHub
commit 845240d330
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 232 additions and 56 deletions

View file

@ -12,13 +12,22 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino
[breakver]: https://github.com/ptaoussanis/encore/blob/master/BREAK-VERSIONING.md
## Unreleased
## UNRELEASED
### `reitit-frontend`
- `reitit.frontend.easy/start!` now correctly removes old event listeners
when called repeatedly (e.g. with hot code reload workflow)
([#438](https://github.com/metosin/reitit/pull/438))
* updated deps:
```clj
[metosin/malli "0.1.0"] is available but we use "0.0.1-20200924.063109-27"
[com.fasterxml.jackson.core/jackson-core "2.11.3"] is available but we use "2.11.2"
[com.fasterxml.jackson.core/jackson-databind "2.11.3"] is available but we use "2.11.2"
[expound "0.8.6"] is available but we use "0.8.5"
[ring/ring-core "1.8.2"] is available but we use "1.8.1"
```
## 0.5.6 (2020-09-26)

View file

@ -6,12 +6,12 @@ A fast data-driven router for Clojure(Script).
* Route [conflict resolution](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/basics/route-conflicts/)
* First-class [route data](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/basics/route-data/)
* Bi-directional routing
* [Pluggable coercion](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/coercion/coercion/) ([malli](https://github.com/metosin/malli), [schema](https://github.com/plumatic/schema) & [clojure.spec](https://clojure.org/about/spec))
* Helpers for [ring](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/ring/ring/), [http](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/http/interceptors/), [pedestal](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/http/pedestal/) & [frontend](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/frontend/basics/)
* [Pluggable coercion](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/coercion/coercion-explained) ([malli](https://github.com/metosin/malli), [schema](https://github.com/plumatic/schema) & [clojure.spec](https://clojure.org/about/spec))
* Helpers for [ring](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/ring/ring-router), [http](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/http/interceptors/), [pedestal](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/http/pedestal/) & [frontend](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/frontend/basics/)
* Friendly [Error Messages](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/basics/error-messages/)
* Extendable
* Modular
* [Fast](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/performance/)
* [Fast](https://cljdoc.org/d/metosin/reitit/CURRENT/doc/misc/performance)
Presentations:
* [Reitit, The Ancient Art of Data-Driven](https://www.slideshare.net/mobile/metosin/reitit-clojurenorth-2019-141438093), Clojure/North 2019, [video](https://youtu.be/cSntRGAjPiM)
@ -151,6 +151,7 @@ All examples are in https://github.com/metosin/reitit/tree/master/examples
* [startrek-ui](https://git.sr.ht/~dharrigan/startrek-ui)
* https://www.learnreitit.com/
* Lipas, liikuntapalvelut: https://github.com/lipas-liikuntapaikat/lipas
* Implementation of the Todo-Backend API spec, using Clojure, Ring/Reitit and next-jdbc: https://github.com/PrestanceDesign/todo-backend-clojure-reitit
## More info

View file

@ -9,6 +9,16 @@
(defn- displace [x] (with-meta x {:displace true}))
(defn- stripped [x] (select-keys x [:enter :leave :error]))
(defn- publish-swagger-data? [{:keys [form body]}]
(not (and (some? form)
(nil? body))))
(defn- swagger-data [parameters muuntaja]
(if (publish-swagger-data? parameters)
{:data {:swagger {:produces (displace (m/encodes muuntaja))
:consumes (displace (m/decodes muuntaja))}}}
{}))
(defn format-interceptor
"Interceptor for content-negotiation, request and response formatting.
@ -22,6 +32,8 @@
Encodes the response body using the `:muuntaja/response` key in request if the response
doesn't have `Content-Type` header already set.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
Optionally takes a default muuntaja instance as argument.
| key | description |
@ -32,12 +44,11 @@
([default-muuntaja]
{:name ::format
:spec ::spec
:compile (fn [{:keys [muuntaja]} _]
:compile (fn [{:keys [muuntaja parameters]} _]
(if-let [muuntaja (or muuntaja default-muuntaja)]
(merge
(stripped (muuntaja.interceptor/format-interceptor muuntaja))
{:data {:swagger {:produces (displace (m/encodes muuntaja))
:consumes (displace (m/decodes muuntaja))}}})))}))
(swagger-data parameters muuntaja))))}))
(defn format-negotiate-interceptor
"Interceptor for content-negotiation.
@ -66,6 +77,8 @@
Decodes the request body into `:body-params` using the `:muuntaja/request` key in request
if the `:body-params` doesn't already exist.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
Optionally takes a default muuntaja instance as argument.
| key | description |
@ -76,11 +89,12 @@
([default-muuntaja]
{:name ::format-request
:spec ::spec
:compile (fn [{:keys [muuntaja]} _]
:compile (fn [{:keys [muuntaja parameters]} _]
(if-let [muuntaja (or muuntaja default-muuntaja)]
(merge
(stripped (muuntaja.interceptor/format-request-interceptor muuntaja))
{:data {:swagger {:consumes (displace (m/decodes muuntaja))}}})))}))
(when (publish-swagger-data? parameters)
{:data {:swagger {:consumes (displace (m/decodes muuntaja))}}}))))}))
(defn format-response-interceptor
"Interceptor for response formatting.
@ -88,6 +102,8 @@
Encodes the response body using the `:muuntaja/response` key in request if the response
doesn't have `Content-Type` header already set.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
Optionally takes a default muuntaja instance as argument.
| key | description |
@ -98,8 +114,9 @@
([default-muuntaja]
{:name ::format-response
:spec ::spec
:compile (fn [{:keys [muuntaja]} _]
:compile (fn [{:keys [muuntaja parameters]} _]
(if-let [muuntaja (or muuntaja default-muuntaja)]
(merge
(stripped (muuntaja.interceptor/format-response-interceptor muuntaja))
{:data {:swagger {:produces (displace (m/encodes muuntaja))}}})))}))
(when (publish-swagger-data? parameters)
{:data {:swagger {:produces (displace (m/encodes muuntaja))}}}))))}))

View file

@ -11,6 +11,10 @@
:params - a merged map of all types of parameter"
[]
{:name ::parameters
:compile (fn [{:keys [parameters]} _]
(if (and (some? (:form parameters)) (nil? (:body parameters)))
{:data {:swagger {:consumes ["application/x-www-form-urlencoded"]}}}
{}))
:enter (fn [ctx]
(let [request (:request ctx)]
(assoc ctx :request (params/params-request request))))})

View file

@ -8,6 +8,16 @@
(defn- displace [x] (with-meta x {:displace true}))
(defn- publish-swagger-data? [{:keys [form body]}]
(not (and (some? form)
(nil? body))))
(defn- swagger-data [parameters muuntaja]
(if (publish-swagger-data? parameters)
{:data {:swagger {:produces (displace (m/encodes muuntaja))
:consumes (displace (m/decodes muuntaja))}}}
{}))
(def format-middleware
"Middleware for content-negotiation, request and response formatting.
@ -21,16 +31,18 @@
Encodes the response body using the `:muuntaja/response` key in request if the response
doesn't have `Content-Type` header already set.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
| key | description |
| -------------|-------------|
| `:muuntaja` | `muuntaja.core/Muuntaja` instance, does not mount if not set."
{:name ::format
:spec ::spec
:compile (fn [{:keys [muuntaja]} _]
:compile (fn [{:keys [muuntaja parameters]} _]
(if muuntaja
{:data {:swagger {:produces (displace (m/encodes muuntaja))
:consumes (displace (m/decodes muuntaja))}}
:wrap #(muuntaja.middleware/wrap-format % muuntaja)}))})
(merge
(swagger-data parameters muuntaja)
{:wrap #(muuntaja.middleware/wrap-format % muuntaja)})))})
(def format-negotiate-middleware
"Middleware for content-negotiation.
@ -54,15 +66,19 @@
Decodes the request body into `:body-params` using the `:muuntaja/request` key in request
if the `:body-params` doesn't already exist.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
| key | description |
| -------------|-------------|
| `:muuntaja` | `muuntaja.core/Muuntaja` instance, does not mount if not set."
{:name ::format-request
:spec ::spec
:compile (fn [{:keys [muuntaja]} _]
:compile (fn [{:keys [muuntaja parameters]} _]
(if muuntaja
{:data {:swagger {:consumes (displace (m/decodes muuntaja))}}
:wrap #(muuntaja.middleware/wrap-format-request % muuntaja)}))})
(merge
(when (publish-swagger-data? parameters)
{:data {:swagger {:consumes (displace (m/decodes muuntaja))}}})
{:wrap #(muuntaja.middleware/wrap-format-request % muuntaja)})))})
(def format-response-middleware
"Middleware for response formatting.
@ -70,12 +86,16 @@
Encodes the response body using the `:muuntaja/response` key in request if the response
doesn't have `Content-Type` header already set.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
| key | description |
| -------------|-------------|
| `:muuntaja` | `muuntaja.core/Muuntaja` instance, does not mount if not set."
{:name ::format-response
:spec ::spec
:compile (fn [{:keys [muuntaja]} _]
:compile (fn [{:keys [muuntaja parameters]} _]
(if muuntaja
{:data {:swagger {:produces (displace (m/encodes muuntaja))}}
:wrap #(muuntaja.middleware/wrap-format-response % muuntaja)}))})
(merge
(when (publish-swagger-data? parameters)
{:data {:swagger {:produces (displace (m/encodes muuntaja))}}})
{:wrap #(muuntaja.middleware/wrap-format-response % muuntaja)})))})

View file

@ -10,4 +10,8 @@
:form-params - a map of parameters from the body
:params - a merged map of all types of parameter"
{:name ::parameters
:compile (fn [{:keys [parameters]} _]
(if (and (some? (:form parameters)) (nil? (:body parameters)))
{:data {:swagger {:consumes ["application/x-www-form-urlencoded"]}}}
{}))
:wrap params/wrap-params})

View file

@ -33,18 +33,18 @@
[metosin/muuntaja "0.6.7"]
[metosin/jsonista "0.2.7"]
[metosin/sieppari "0.0.0-alpha13"]
[metosin/malli "0.0.1-20200924.063109-27"]
[metosin/malli "0.1.0"]
;; https://clojureverse.org/t/depending-on-the-right-versions-of-jackson-libraries/5111
[com.fasterxml.jackson.core/jackson-core "2.11.2"]
[com.fasterxml.jackson.core/jackson-databind "2.11.2"]
[com.fasterxml.jackson.core/jackson-core "2.11.3"]
[com.fasterxml.jackson.core/jackson-databind "2.11.3"]
[meta-merge "1.0.0"]
[fipp "0.6.23" :exclusions [org.clojure/core.rrb-vector]]
[expound "0.8.5"]
[expound "0.8.6"]
[lambdaisland/deep-diff "0.0-47"]
[com.bhauman/spell-spec "0.1.2"]
[ring/ring-core "1.8.1"]
[ring/ring-core "1.8.2"]
[io.pedestal/pedestal.service "0.5.8"]]
@ -84,18 +84,18 @@
[metosin/schema-tools "0.12.2"]
[metosin/spec-tools "0.10.4"]
[metosin/muuntaja "0.6.7"]
[metosin/sieppari]
[metosin/sieppari "0.0.0-alpha13"]
[metosin/jsonista "0.2.7"]
[metosin/malli]
[metosin/malli "0.1.0"]
[lambdaisland/deep-diff "0.0-47"]
[meta-merge "1.0.0"]
[com.bhauman/spell-spec "0.1.2"]
[expound "0.8.5"]
[expound "0.8.6"]
[fipp "0.6.23"]
[orchestra "2020.09.18-1"]
[ring "1.8.1"]
[ring "1.8.2"]
[ikitommi/immutant-web "3.0.0-alpha1"]
[metosin/ring-http-response "0.9.1"]
[metosin/ring-swagger-ui "3.25.3"]
@ -109,7 +109,7 @@
[org.clojure/core.async "1.3.610"]
[manifold "0.1.8"]
[funcool/promesa "5.1.0"]
[funcool/promesa "6.0.0"]
[com.clojure-goes-fast/clj-async-profiler "0.4.1"]
[ring-cors "0.1.13"]
@ -128,7 +128,7 @@
[calfpath "0.7.2"]
[org.clojure/core.async "1.3.610"]
[manifold "0.1.8"]
[funcool/promesa "5.1.0"]
[funcool/promesa "6.0.0"]
[metosin/sieppari]
[yada "1.2.16"]
[aleph "0.4.6"]

View file

@ -36,20 +36,25 @@
["/just-edn"
{:muuntaja just-edn
:get identity}]
["/form-params"
{:post {:parameters {:form {:x string?}}
:handler identity}}]
["/swagger.json"
{:get {:no-doc true
:handler (swagger/create-swagger-handler)}}]]
{:data {:muuntaja m/instance
:interceptors [(muuntaja/format-interceptor)]}})
{:executor sieppari/executor})
spec (fn [path]
spec (fn [method path]
(let [path (keyword path)]
(-> {:request-method :get :uri "/swagger.json"}
(app) :body
(->> (m/decode m/instance "application/json"))
:paths path :get)))
produces (comp set :produces spec)
consumes (comp set :consumes spec)]
:paths path method)))
produces (comp set :produces (partial spec :get))
consumes (comp set :consumes (partial spec :get))
post-produces (comp set :produces (partial spec :post))
post-consumes (comp set :consumes (partial spec :post))]
(testing "with defaults"
(let [path "/defaults"]
@ -85,7 +90,12 @@
(let [path "/just-edn"]
(is (= #{"application/edn"}
(produces path)
(consumes path)))))))
(consumes path)))))
(testing "form parameters swagger-data"
(let [path "/form-params"]
(is (= #{}
(post-produces path)
(post-consumes path)))))))
(deftest muuntaja-swagger-parts-test
(let [app (http/ring-handler
@ -103,17 +113,35 @@
(muuntaja/format-response-interceptor)
(muuntaja/format-request-interceptor)]
:get identity}]
["/form-request"
{:interceptors [(muuntaja/format-negotiate-interceptor)
(muuntaja/format-request-interceptor)]
:post {:parameters {:form {:x string?}}
:handler identity}}]
["/form-response"
{:interceptors [(muuntaja/format-negotiate-interceptor)
(muuntaja/format-response-interceptor)]
:post {:parameters {:form {:x string?}}
:handler identity}}]
["/form-with-both"
{:interceptors [(muuntaja/format-negotiate-interceptor)
(muuntaja/format-response-interceptor)
(muuntaja/format-request-interceptor)]
:post {:parameters {:form {:x string?}}
:handler identity}}]
["/swagger.json"
{:get {:no-doc true
:handler (swagger/create-swagger-handler)}}]]
{:data {:muuntaja m/instance}})
{:executor sieppari/executor})
spec (fn [path]
spec (fn [method path]
(-> {:request-method :get :uri "/swagger.json"}
(app) :body :paths (get path) :get))
produces (comp :produces spec)
consumes (comp :consumes spec)]
(app) :body :paths (get path) method))
produces (comp :produces (partial spec :get))
consumes (comp :consumes (partial spec :get))
post-produces (comp :produces (partial spec :post))
post-consumes (comp :consumes (partial spec :post))]
(testing "just request formatting"
(let [path "/request"]
@ -133,7 +161,7 @@
(produces path)))
(is (nil? (consumes path)))))
(testing "just response formatting"
(testing "request and response formatting"
(let [path "/both"]
(is (= #{"application/json"
"application/transit+msgpack"
@ -144,4 +172,16 @@
"application/transit+msgpack"
"application/transit+json"
"application/edn"}
(consumes path)))))))
(consumes path)))))
(testing "just request formatting for form params"
(let [path "/form-request"]
(is (nil? (post-produces path)))
(is (nil? (post-consumes path)))))
(testing "just response formatting for form params"
(let [path "/form-response"]
(is (nil? (post-produces path)))
(is (nil? (post-consumes path)))))
(testing "just request formatting for form params"
(let [path "/form-with-both"]
(is (nil? (post-produces path)))
(is (nil? (post-consumes path)))))))

View file

@ -2,7 +2,8 @@
(:require [clojure.test :refer [deftest testing is]]
[reitit.http.interceptors.parameters :as parameters]
[reitit.http :as http]
[reitit.interceptor.sieppari :as sieppari]))
[reitit.interceptor.sieppari :as sieppari]
[reitit.swagger :as swagger]))
(deftest parameters-test
(let [app (http/ring-handler
@ -15,3 +16,23 @@
(app {:request-method :get
:uri "/ping"
:query-string "kikka=kukka"})))))
(deftest parameters-swagger-test
(let [app (http/ring-handler
(http/router
[["/form-params" {:post {:parameters {:form {:x string?}}
:handler identity}}]
["/body-params" {:post {:parameters {:body {:x string?}}
:handler identity}}]
["/swagger.json" {:get {:no-doc true
:handler (swagger/create-swagger-handler)}}]]
{:data {:interceptors [(parameters/parameters-interceptor)]}})
 {:executor sieppari/executor})
spec (fn [path]
(-> {:request-method :get :uri "/swagger.json"}
app
(get-in [:body :paths path :post])))]
(testing "with form parameters"
(is (= ["application/x-www-form-urlencoded"] (:consumes (spec "/form-params")))))
(testing "with body parameters"
(is (= nil (:consumes (spec "/body-params")))))))

View file

@ -34,19 +34,24 @@
["/just-edn"
{:muuntaja just-edn
:get identity}]
["/form-params"
{:post {:parameters {:form {:x string?}}
:handler identity}}]
["/swagger.json"
{:get {:no-doc true
:handler (swagger/create-swagger-handler)}}]]
{:data {:muuntaja m/instance
:middleware [muuntaja/format-middleware]}}))
spec (fn [path]
spec (fn [method path]
(let [path (keyword path)]
(-> {:request-method :get :uri "/swagger.json"}
(app) :body
(->> (m/decode m/instance "application/json"))
:paths path :get)))
produces (comp set :produces spec)
consumes (comp set :consumes spec)]
:paths path method)))
produces (comp set :produces (partial spec :get))
consumes (comp set :consumes (partial spec :get))
post-produces (comp set :produces (partial spec :post))
post-consumes (comp set :consumes (partial spec :post))]
(testing "with defaults"
(let [path "/defaults"]
@ -82,7 +87,12 @@
(let [path "/just-edn"]
(is (= #{"application/edn"}
(produces path)
(consumes path)))))))
(consumes path)))))
(testing "form parameters swagger-data"
(let [path "/form-params"]
(is (= #{}
(post-produces path)
(post-consumes path)))))))
(deftest muuntaja-swagger-parts-test
(let [app (ring/ring-handler
@ -100,16 +110,34 @@
muuntaja/format-response-middleware
muuntaja/format-request-middleware]
:get identity}]
["/form-request"
{:middleware [muuntaja/format-negotiate-middleware
muuntaja/format-request-middleware]
:post {:parameters {:form {:x string?}}
:handler identity}}]
["/form-response"
{:middleware [muuntaja/format-negotiate-middleware
muuntaja/format-response-middleware]
:post {:parameters {:form {:x string?}}
:handler identity}}]
["/form-with-both"
{:middleware [muuntaja/format-negotiate-middleware
muuntaja/format-response-middleware
muuntaja/format-request-middleware]
:post {:parameters {:form {:x string?}}
:handler identity}}]
["/swagger.json"
{:get {:no-doc true
:handler (swagger/create-swagger-handler)}}]]
{:data {:muuntaja m/instance}}))
spec (fn [path]
spec (fn [method path]
(-> {:request-method :get :uri "/swagger.json"}
(app) :body :paths (get path) :get))
produces (comp :produces spec)
consumes (comp :consumes spec)]
(app) :body :paths (get path) method))
produces (comp :produces (partial spec :get))
consumes (comp :consumes (partial spec :get))
post-produces (comp :produces (partial spec :post))
post-consumes (comp :consumes (partial spec :post))]
(testing "just request formatting"
(let [path "/request"]
@ -129,7 +157,7 @@
(produces path)))
(is (nil? (consumes path)))))
(testing "just response formatting"
(testing "request and response formatting"
(let [path "/both"]
(is (= #{"application/json"
"application/transit+msgpack"
@ -140,4 +168,16 @@
"application/transit+msgpack"
"application/transit+json"
"application/edn"}
(consumes path)))))))
(consumes path)))))
(testing "just request formatting for form params"
(let [path "/form-request"]
(is (nil? (post-produces path)))
(is (nil? (post-consumes path)))))
(testing "just response formatting for form params"
(let [path "/form-response"]
(is (nil? (post-produces path)))
(is (nil? (post-consumes path)))))
(testing "just request formatting for form params"
(let [path "/form-with-both"]
(is (nil? (post-produces path)))
(is (nil? (post-consumes path)))))))

View file

@ -1,7 +1,8 @@
(ns reitit.ring.middleware.parameters-test
(:require [clojure.test :refer [deftest testing is]]
[reitit.ring.middleware.parameters :as parameters]
[reitit.ring :as ring]))
[reitit.ring :as ring]
[reitit.swagger :as swagger]))
(deftest parameters-test
(let [app (ring/ring-handler
@ -13,3 +14,22 @@
(app {:request-method :get
:uri "/ping"
:query-string "kikka=kukka"})))))
(deftest parameters-swagger-test
(let [app (ring/ring-handler
(ring/router
[["/form-params" {:post {:parameters {:form {:x string?}}
:handler identity}}]
["/body-params" {:post {:parameters {:body {:x string?}}
:handler identity}}]
["/swagger.json" {:get {:no-doc true
:handler (swagger/create-swagger-handler)}}]]
{:data {:middleware [parameters/parameters-middleware]}}))
spec (fn [path]
(-> {:request-method :get :uri "/swagger.json"}
app
(get-in [:body :paths path :post])))]
(testing "with form parameters"
(is (= ["application/x-www-form-urlencoded"] (:consumes (spec "/form-params")))))
(testing "with body parameters"
(is (= nil (:consumes (spec "/body-params")))))))