mirror of
https://github.com/metosin/reitit.git
synced 2026-02-22 10:09:10 +00:00
Compare commits
4 commits
45951aa82e
...
ef9dd495be
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef9dd495be | ||
|
|
9509e40dae | ||
|
|
67918a3f9c | ||
|
|
f26dc1ab19 |
4 changed files with 43 additions and 6 deletions
|
|
@ -17,6 +17,7 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino
|
||||||
* Allow multimethods as handlers when validating [#755](https://github.com/metosin/reitit/pull/755)
|
* Allow multimethods as handlers when validating [#755](https://github.com/metosin/reitit/pull/755)
|
||||||
* Improve error reporting when generating OpenAPI fails [#754](https://github.com/metosin/reitit/pull/754)
|
* Improve error reporting when generating OpenAPI fails [#754](https://github.com/metosin/reitit/pull/754)
|
||||||
* Allow middleware registry to be used when defining middleware in `ring-handler`. See [docs](./doc/ring/middleware_registry.md). [#739](https://github.com/metosin/reitit/pull/739)
|
* Allow middleware registry to be used when defining middleware in `ring-handler`. See [docs](./doc/ring/middleware_registry.md). [#739](https://github.com/metosin/reitit/pull/739)
|
||||||
|
* Allow passing options (eg. `:malli.transform/add-optional-keys`) to malli's `default-value-transformer`. See [docs](./doc/coercion/malli_coercion.md). [#756](https://github.com/metosin/reitit/pull/756)
|
||||||
|
|
||||||
## 0.9.1 (2025-05-27)
|
## 0.9.1 (2025-05-27)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,9 +73,10 @@ Using `create` with options to create the coercion instead of `coercion`:
|
||||||
{:transformers {:body {:default reitit.coercion.malli/default-transformer-provider
|
{:transformers {:body {:default reitit.coercion.malli/default-transformer-provider
|
||||||
:formats {"application/json" reitit.coercion.malli/json-transformer-provider}}
|
:formats {"application/json" reitit.coercion.malli/json-transformer-provider}}
|
||||||
:string {:default reitit.coercion.malli/string-transformer-provider}
|
:string {:default reitit.coercion.malli/string-transformer-provider}
|
||||||
:response {:default reitit.coercion.malli/default-transformer-provider}}
|
:response {:default reitit.coercion.malli/default-transformer-provider
|
||||||
|
:formats {"application/json" reitit.coercion.malli/json-transformer-provider}}}
|
||||||
;; set of keys to include in error messages
|
;; set of keys to include in error messages
|
||||||
:error-keys #{:type :coercion :in :schema :value :errors :humanized #_:transformed}
|
:error-keys #{:type :coercion :in #_:schema :value #_:errors :humanized #_:transformed}
|
||||||
;; support lite syntax?
|
;; support lite syntax?
|
||||||
:lite true
|
:lite true
|
||||||
;; schema identity function (default: close all map schemas)
|
;; schema identity function (default: close all map schemas)
|
||||||
|
|
@ -87,7 +88,11 @@ Using `create` with options to create the coercion instead of `coercion`:
|
||||||
;; strip-extra-keys (affects only predefined transformers)
|
;; strip-extra-keys (affects only predefined transformers)
|
||||||
:strip-extra-keys true
|
:strip-extra-keys true
|
||||||
;; add/set default values
|
;; add/set default values
|
||||||
|
;; Can be false, true or a map of options to pass to malli.transform/default-value-transformer,
|
||||||
|
;; for example {:malli.transform/add-optional-keys true}
|
||||||
:default-values true
|
:default-values true
|
||||||
|
;; encode-error
|
||||||
|
:encode-error nil
|
||||||
;; malli options
|
;; malli options
|
||||||
:options nil})
|
:options nil})
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@
|
||||||
[malli.swagger :as swagger]
|
[malli.swagger :as swagger]
|
||||||
[malli.transform :as mt]
|
[malli.transform :as mt]
|
||||||
[malli.util :as mu]
|
[malli.util :as mu]
|
||||||
[reitit.coercion :as coercion]
|
[reitit.coercion :as coercion]))
|
||||||
[clojure.string :as string]))
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; coercion
|
;; coercion
|
||||||
|
|
@ -31,7 +30,7 @@
|
||||||
(mt/transformer
|
(mt/transformer
|
||||||
(if strip-extra-keys (mt/strip-extra-keys-transformer))
|
(if strip-extra-keys (mt/strip-extra-keys-transformer))
|
||||||
transformer
|
transformer
|
||||||
(if default-values (mt/default-value-transformer))))))
|
(if default-values (mt/default-value-transformer (if (map? default-values) default-values {})))))))
|
||||||
|
|
||||||
(def string-transformer-provider (-provider (mt/string-transformer)))
|
(def string-transformer-provider (-provider (mt/string-transformer)))
|
||||||
(def json-transformer-provider (-provider (mt/json-transformer)))
|
(def json-transformer-provider (-provider (mt/json-transformer)))
|
||||||
|
|
@ -116,7 +115,9 @@
|
||||||
:enabled true
|
:enabled true
|
||||||
;; strip-extra-keys (affects only predefined transformers)
|
;; strip-extra-keys (affects only predefined transformers)
|
||||||
:strip-extra-keys true
|
:strip-extra-keys true
|
||||||
;; add/set default values
|
;; add/set default values.
|
||||||
|
;; Can be false, true or a map of options to pass to malli.transform/default-value-transformer,
|
||||||
|
;; for example {:malli.transform/add-optional-keys true}
|
||||||
:default-values true
|
:default-values true
|
||||||
;; encode-error
|
;; encode-error
|
||||||
:encode-error nil
|
:encode-error nil
|
||||||
|
|
|
||||||
|
|
@ -140,6 +140,36 @@
|
||||||
(let [m (r/match-by-path r "/none/kikka/abba")]
|
(let [m (r/match-by-path r "/none/kikka/abba")]
|
||||||
(is (= nil (coercion/coerce! m))))))))
|
(is (= nil (coercion/coerce! m))))))))
|
||||||
|
|
||||||
|
(deftest malli-query-parameter-coercion-test
|
||||||
|
(let [router (fn [coercion]
|
||||||
|
(r/router ["/test"
|
||||||
|
{:coercion coercion
|
||||||
|
:parameters {:query [:map
|
||||||
|
[:a [:string {:default "a"}]]
|
||||||
|
[:x {:optional true} [:keyword {:default :a}]]]}}]
|
||||||
|
{:compile coercion/compile-request-coercers}))]
|
||||||
|
(testing "default values for :optional query keys do not get added"
|
||||||
|
(is (= {:query {:a "a"}}
|
||||||
|
(-> (r/match-by-path (router reitit.coercion.malli/coercion) "/test")
|
||||||
|
(assoc :query-params {})
|
||||||
|
(coercion/coerce!)))))
|
||||||
|
(testing "default values for :optional query keys get added when :malli.transform/add-optional-keys is set"
|
||||||
|
(is (= {:query {:a "a" :x :a}}
|
||||||
|
(-> (r/match-by-path (router (reitit.coercion.malli/create
|
||||||
|
(assoc reitit.coercion.malli/default-options
|
||||||
|
:default-values {:malli.transform/add-optional-keys true}))) "/test")
|
||||||
|
(assoc :query-params {})
|
||||||
|
(coercion/coerce!)))))
|
||||||
|
(testing "default values can be disabled"
|
||||||
|
(is (thrown-with-msg?
|
||||||
|
ExceptionInfo
|
||||||
|
#"Request coercion failed"
|
||||||
|
(-> (r/match-by-path (router (reitit.coercion.malli/create
|
||||||
|
(assoc reitit.coercion.malli/default-options
|
||||||
|
:default-values false))) "/test")
|
||||||
|
(assoc :query-params {})
|
||||||
|
(coercion/coerce!)))))))
|
||||||
|
|
||||||
(defn match-by-path-and-coerce! [router path]
|
(defn match-by-path-and-coerce! [router path]
|
||||||
(if-let [match (r/match-by-path router path)]
|
(if-let [match (r/match-by-path router path)]
|
||||||
(assoc match :parameters (coercion/coerce! match))))
|
(assoc match :parameters (coercion/coerce! match))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue