Skip to content

Commit b10a068

Browse files
committed
Merge branch 'master' of github.com:metosin/compojure-api
2 parents 8f93e7b + 6ed9f6f commit b10a068

File tree

11 files changed

+157
-37
lines changed

11 files changed

+157
-37
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
See also: [compojure-api 1.1.x changelog](./CHANGELOG-1.1.x.md)
22

33
## 2.0.0-alpha34-SNAPSHOT
4-
* Lazily load spec and schema coercion
54
* bump spec-tools to 0.10.6
65
* notable changes: swagger `:name` defaults to `"body"` instead of `""` ([diff](https://github.com/metosin/spec-tools/compare/0.10.2...0.10.3))
6+
* Add compatibility for 1.x coercions
7+
* implies Schema backend
8+
* fn from request->field->schema->coercer
9+
* mostly often just `(constantly nil)` or (constantly {:body matcher})
710
* **BREAKING**: removed `api-defaults` vars
811
* add `-v2` suffix to vars
912

src/compojure/api/api.clj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[compojure.api.request :as request]
66
[compojure.api.routes :as routes]
77
[compojure.api.common :as common]
8-
[compojure.api.request :as request]
98
[ring.swagger.common :as rsc]
109
[ring.swagger.middleware :as rsm]))
1110

src/compojure/api/coercion.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
[compojure.api.request :as request]
55
[compojure.api.coercion.core :as cc]
66
;; side effects
7-
compojure.api.coercion.register-schema
8-
compojure.api.coercion.register-spec)
7+
[compojure.api.coercion.schema :as cschema]
8+
compojure.api.coercion.spec)
99
(:import (compojure.api.coercion.core CoercionError)))
1010

1111
(def default-coercion :schema)
@@ -23,6 +23,7 @@
2323
(nil? coercion) nil
2424
(keyword? coercion) (cc/named-coercion coercion)
2525
(satisfies? cc/Coercion coercion) coercion
26+
(fn? coercion) (cschema/create-coercion coercion)
2627
:else (throw (ex-info (str "invalid coercion " coercion) {:coercion coercion}))))
2728

2829
(defn get-apidocs [maybe-coercion spec info]

src/compojure/api/coercion/register_schema.clj

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/compojure/api/coercion/register_spec.clj

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/compojure/api/coercion/schema.clj

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
[compojure.api.coercion.core :as cc]
66
[clojure.walk :as walk]
77
[schema.core :as s]
8-
[compojure.api.common :as common]
9-
;; side effects
10-
compojure.api.coercion.register-schema)
8+
[compojure.api.common :as common])
119
(:import (java.io File)
1210
(schema.core OptionalKey RequiredKey)
1311
(schema.utils ValidationError NamedError)))
@@ -34,7 +32,11 @@
3432
(common/fifo-memoize sc/coercer 1000))
3533

3634
;; don't use coercion for certain types
37-
(defmulti coerce-response? identity :default ::default)
35+
(defmulti coerce-response? #(if (or (class? %)
36+
(keyword? %))
37+
%
38+
::default)
39+
:default ::default)
3840
(defmethod coerce-response? ::default [_] true)
3941
(defmethod coerce-response? File [_] false)
4042

@@ -55,9 +57,15 @@
5557
(update :errors stringify)))
5658

5759
(coerce-request [_ schema value type format request]
58-
(let [type-options (options type)]
59-
(if-let [matcher (or (get (get type-options :formats) format)
60-
(get type-options :default))]
60+
(let [legacy? (fn? options)
61+
type-options (if legacy?
62+
(when-let [provider (options request)]
63+
(provider type))
64+
(options type))]
65+
(if-let [matcher (if legacy?
66+
type-options
67+
(or (get (get type-options :formats) format)
68+
(get type-options :default)))]
6169
(let [coerce (memoized-coercer schema matcher)
6270
coerced (coerce value)]
6371
(if (su/error? coerced)
@@ -83,6 +91,10 @@
8391
:response {:default (constantly nil)}})
8492

8593
(defn create-coercion [options]
94+
{:pre [(or (map? options)
95+
(fn? options))]}
8696
(->SchemaCoercion :schema options))
8797

8898
(def default-coercion (create-coercion default-options))
99+
100+
(defmethod cc/named-coercion :schema [_] default-coercion)

src/compojure/api/coercion/spec.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
[clojure.walk :as walk]
77
[compojure.api.coercion.core :as cc]
88
[spec-tools.swagger.core :as swagger]
9-
[compojure.api.common :as common]
10-
;; side effects
11-
compojure.api.coercion.register-spec)
9+
[compojure.api.common :as common])
1210
(:import (clojure.lang IPersistentMap)
1311
(schema.core RequiredKey OptionalKey)
1412
(spec_tools.core Spec)
@@ -151,3 +149,5 @@
151149
(->SpecCoercion :spec options))
152150

153151
(def default-coercion (create-coercion default-options))
152+
153+
(defmethod cc/named-coercion :spec [_] default-coercion)

src/compojure/api/middleware.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@
121121
:string coerce/query-schema-coercion-matcher
122122
:response coerce/json-schema-coercion-matcher})
123123

124-
;; 1.1.x
124+
(def no-response-coercion
125+
(constantly (dissoc default-coercion-matchers :response)))
126+
125127
(defn coercion-matchers [request]
126128
(let [options (get-options request)]
127129
(if (contains? options :coercion)

src/compojure/api/resource.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@
154154
Top-level mw are applied first if route matches, method-level
155155
mw are applied next if method matches
156156
157-
- **:coercion** A function from request->type->coercion-matcher, used
157+
- **:coercion** A named coercion or instance of Coercion
158158
in resource coercion for :body, :string and :response.
159-
Setting value to `(constantly nil)` disables both request- &
159+
Setting value to `nil` disables both request- &
160160
response coercion. See tests and wiki for details.
161161
162162
Enhancements to ring-swagger operations map:

test/compojure/api/coercion/schema_coercion_test.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,10 @@
284284

285285
(testing "generates valid swagger spec"
286286
(is (validator/validate app)))))
287+
288+
(def some-spec (s/conditional
289+
identity s/Int))
290+
(defmethod cs/coerce-response? some-spec [_] false)
291+
292+
(deftest coerce-response-memory-leak-test
293+
(is (true? (cs/coerce-response? some-spec))))

0 commit comments

Comments
 (0)