Skip to content

Commit 8f93e7b

Browse files
committed
rename middleware defaults with -v2 suffix
1 parent c1bd124 commit 8f93e7b

File tree

14 files changed

+182
-221
lines changed

14 files changed

+182
-221
lines changed

CHANGELOG.md

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

3-
## Next
3+
## 2.0.0-alpha34-SNAPSHOT
44
* Lazily load spec and schema coercion
55
* bump spec-tools to 0.10.6
66
* notable changes: swagger `:name` defaults to `"body"` instead of `""` ([diff](https://github.com/metosin/spec-tools/compare/0.10.2...0.10.3))
7-
8-
## 2.0.0-alpha34-SNAPSHOT
9-
* **BREAKING CHANGE**: `:formatter :muuntaja` sometimes required for `api{-middleware}` options
10-
* to prepare for 1.x compatibility, :muuntaja must be explicitly configured
11-
* Migration instructions: run your program and fix the error messages, which will provide specific instructions.
12-
* to circumvent this change, set `-Dcompojure.api.middleware.global-default-formatter=:muuntaja`
13-
* stable 2.x will default `:formatter` to `:ring-middleware-format`
7+
* **BREAKING**: removed `api-defaults` vars
8+
* add `-v2` suffix to vars
149

1510
## 2.0.0-alpha33 (2024-04-30)
1611
* Throw an error on malformed `:{body,query,headers}`, in particular if anything other than 2 elements was provided

src/compojure/api/api.clj

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
[ring.swagger.common :as rsc]
1010
[ring.swagger.middleware :as rsm]))
1111

12-
(def api-defaults
12+
(def api-defaults-v1
1313
(merge
14-
mw/api-middleware-defaults
14+
mw/api-middleware-defaults-v1
15+
{:api {:invalid-routes-fn routes/log-invalid-child-routes
16+
:disable-api-middleware? false}
17+
:swagger {:ui nil, :spec nil}}))
18+
19+
(def api-defaults-v2
20+
(merge
21+
mw/api-middleware-defaults-v2
1522
{:api {:invalid-routes-fn routes/log-invalid-child-routes
1623
:disable-api-middleware? false}
1724
:swagger {:ui nil, :spec nil}}))
@@ -58,17 +65,9 @@
5865
"Compojure-api uses now Muuntaja insted of ring-middleware-format,\n"
5966
"the new formatting options for it should be under [:formats]. See\n"
6067
"[[api-middleware]] documentation for more details.\n"))
61-
_ (when (and (not (:formatter options))
62-
(not (contains? options :formats))
63-
(not (System/getProperty "compojure.api.middleware.global-default-formatter")))
64-
(throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api`.\n"
65-
"e.g., (api {:formatter :muuntaja} routes...)\n"
66-
"To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be\n"
67-
"explicitly chosen if not configured by `:format` (ring-middleware-format) or \n"
68-
"`:formats` (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.\n"
69-
"To globally override the formatter, use -Dcompojure.api.middleware.global-default-formatter=:muuntaja")
70-
{})))
71-
options (rsc/deep-merge api-defaults options)
68+
options (if (:format options)
69+
(assert nil ":format")
70+
(rsc/deep-merge api-defaults-v2 options))
7271
handler (apply c/routes (concat [(swagger/swagger-routes (:swagger options))] handlers))
7372
partial-api-route (routes/map->Route
7473
{:childs [handler]
@@ -78,8 +77,8 @@
7877
lookup (routes/route-lookup-table routes)
7978
swagger-data (get-in options [:swagger :data])
8079
enable-api-middleware? (not (get-in options [:api :disable-api-middleware?]))
81-
api-middleware-options (dissoc (mw/api-middleware-options (assoc (dissoc options :api :swagger) ::via-api true))
82-
::mw/api-middleware-defaults)
80+
api-middleware-options ((if (:format options) mw/api-middleware-options-v1 mw/api-middleware-options-v2)
81+
(dissoc options :api :swagger))
8382
api-handler (-> handler
8483
(cond-> swagger-data (rsm/wrap-swagger-data swagger-data))
8584
(cond-> enable-api-middleware? (mw/api-middleware

src/compojure/api/middleware.clj

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,30 @@
187187
(map middleware-fn)
188188
(apply comp identity)))
189189

190+
;;
191+
;; ring-middleware-format stuff
192+
;;
193+
194+
(def ^:private default-mime-types
195+
{:json "application/json"
196+
:json-kw "application/json"
197+
:edn "application/edn"
198+
:clojure "application/clojure"
199+
:yaml "application/x-yaml"
200+
:yaml-kw "application/x-yaml"
201+
:yaml-in-html "text/html"
202+
:transit-json "application/transit+json"
203+
:transit-msgpack "application/transit+msgpack"})
204+
205+
(defn mime-types
206+
[format]
207+
(get default-mime-types format
208+
(some-> format :content-type)))
209+
210+
(def ^:private response-only-mimes #{:clojure :yaml-in-html})
211+
212+
(defn ->mime-types [formats] (keep mime-types formats))
213+
190214
;;
191215
;; swagger-data
192216
;;
@@ -216,9 +240,20 @@
216240
;; Api Middleware
217241
;;
218242

219-
(def api-middleware-defaults
220-
{::api-middleware-defaults true
221-
:formats ::default
243+
(def api-middleware-defaults-v1
244+
{:format {:formats [:json-kw :yaml-kw :edn :transit-json :transit-msgpack]
245+
:params-opts {}
246+
:response-opts {}}
247+
:exceptions {:handlers {:ring.util.http-response/response ex/http-response-handler
248+
::ex/request-validation ex/request-validation-handler
249+
::ex/request-parsing ex/request-parsing-handler
250+
::ex/response-validation ex/response-validation-handler
251+
::ex/default ex/safe-handler}}
252+
:coercion (constantly default-coercion-matchers)
253+
:ring-swagger nil})
254+
255+
(def api-middleware-defaults-v2
256+
{:formats ::default
222257
:exceptions {:handlers {:ring.util.http-response/response ex/http-response-handler
223258
::ex/request-validation ex/request-validation-handler
224259
::ex/request-parsing ex/request-parsing-handler
@@ -228,8 +263,11 @@
228263
:coercion coercion/default-coercion
229264
:ring-swagger nil})
230265

231-
(defn api-middleware-options [options]
232-
(rsc/deep-merge api-middleware-defaults options))
266+
(defn api-middleware-options-v1 [options]
267+
(rsc/deep-merge api-middleware-defaults-v1 options))
268+
269+
(defn api-middleware-options-v2 [options]
270+
(rsc/deep-merge api-middleware-defaults-v2 options))
233271

234272
;; TODO: test all options! (https://github.com/metosin/compojure-api/issues/137)
235273
(defn api-middleware
@@ -256,15 +294,6 @@
256294
257295
### Options
258296
259-
- **:formatter** either :ring-middleware-format or :muuntaja.
260-
During 2.x pre-releases, this will be a required key, unless
261-
:formats is provided, which is equivalent to setting to :muuntaja.
262-
Stable 2.x releases will default to :ring-middleware-format if
263-
not provided or :format is set, unless :formats is provided,
264-
which is equivalent to setting to :muuntaja.
265-
Stable 2.x will print a deprecation warning if implicitly
266-
or explicitly set to :ring-middleware-format.
267-
268297
- **:exceptions** for *compojure.api.middleware/wrap-exceptions* (nil to unmount it)
269298
- **:handlers** Map of error handlers for different exception types, type refers to `:type` key in ExceptionInfo data.
270299
@@ -286,42 +315,11 @@
286315
:components restructuring. (If you are using api,
287316
you might want to take look at using wrap-components
288317
middleware manually.). Defaults to nil (middleware not mounted)."
289-
([handler]
290-
(throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n"
291-
"e.g., (api-middleware <handler> {:formatter :muuntaja})\n"
292-
"To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be \n"
293-
"explicitly chosen if not configured by `:format` (ring-middleware-format) or\n"
294-
"`:formats` (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.")
295-
{}))
296-
(api-middleware handler api-middleware-defaults))
318+
([handler] (api-middleware handler api-middleware-defaults-v2))
297319
([handler options]
298-
(when (and (::api-middleware-defaults options)
299-
(not (:formatter options))
300-
(not (System/getProperty "compojure.api.middleware.global-default-formatter")))
301-
(throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n"
302-
"e.g., (api-middleware <handler> {:formatter :muuntaja})\n"
303-
"To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be\n"
304-
"explicitly chosen if not configured by `:format` (ring-middleware-format) or\n"
305-
":formats (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.\n"
306-
"To globally override the default formatter, use -Dcompojure.api.middleware.global-default-formatter=:muuntaja")
307-
{})))
308-
(let [formatter (or (:formatter options)
309-
(when (or (contains? options :formats)
310-
(= (System/getProperty "compojure.api.middleware.global-default-formatter")
311-
":muuntaja"))
312-
:muuntaja)
313-
(throw (ex-info (str "ERROR: Please set `:formatter :muuntaja` in the options map of `api-middleware.\n"
314-
"e.g., (api-middleware <handler> {:formatter :muuntaja})\n"
315-
"To prepare for backwards compatibility with compojure-api 1.x, the formatting library must be\n"
316-
"explicitly chosen if not configured by `:format` (ring-middleware-format) or\n"
317-
":formats (muuntaja). Once 2.x is stable, the default will be `:formatter :ring-middleware-format`.\n"
318-
"To globally override the default formatter, use -Dcompojure.api.middleware.global-default-formatter=:muuntaja")
319-
{}))
320-
;; TODO 2.x stable
321-
:ring-middleware-format)
322-
_ (assert (= :muuntaja formatter)
323-
(str "Invalid :formatter: " (pr-str formatter) ". Must be :muuntaja."))
324-
options (api-middleware-options options)
320+
(let [options (if (:format options)
321+
(assert (not (:format options)))
322+
(api-middleware-options-v2 options))
325323
{:keys [exceptions components formats middleware ring-swagger coercion]} options
326324
muuntaja (create-muuntaja formats)]
327325

test/compojure/api/coercion/schema_coercion_test.clj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@
150150

151151
(deftest apis-test
152152
(let [app (api
153-
{:formatter :muuntaja
154-
:swagger {:spec "/swagger.json"}
153+
{:swagger {:spec "/swagger.json"}
155154
:coercion :schema}
156155

157156
(POST "/body" []

test/compojure/api/coercion_test.clj

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
:default {:schema {:value s/Int}}}
3232
(ok {:value (or value "123")}))]
3333
(testing "200"
34-
(is-has-body {:value "123"} (get* (api {:formatter :muuntaja} r-200) "/"))
35-
(is-fails-with 500 (get* (api {:formatter :muuntaja} r-200) "/" {:value 123})))
34+
(is-has-body {:value "123"} (get* (api {} r-200) "/"))
35+
(is-fails-with 500 (get* (api {} r-200) "/" {:value 123})))
3636

3737
(testing "exception data"
38-
(let [ex (get* (api {:formatter :muuntaja} r-200) "/" {:value 123})]
38+
(let [ex (get* (api {} r-200) "/" {:value 123})]
3939
(is (= 500 (first ex)))
4040
(is (= {:type "compojure.api.exception/response-validation"
4141
:coercion "schema",
@@ -46,12 +46,12 @@
4646
(select-keys (second ex) [:type :coercion :in :value :schema :errors])))))
4747

4848
(testing ":default"
49-
(is-has-body {:value "123"} (get* (api {:formatter :muuntaja} r-default) "/"))
50-
(is-fails-with 500 (get* (api {:formatter :muuntaja} r-default) "/" {:value 123})))
49+
(is-has-body {:value "123"} (get* (api {} r-default) "/"))
50+
(is-fails-with 500 (get* (api {} r-default) "/" {:value 123})))
5151

5252
(testing ":default"
53-
(is-has-body {:value "123"} (get* (api {:formatter :muuntaja} r-200-default) "/"))
54-
(is-fails-with 500 (get* (api {:formatter :muuntaja} r-200-default) "/" {:value 123})))))
53+
(is-has-body {:value "123"} (get* (api {} r-200-default) "/"))
54+
(is-fails-with 500 (get* (api {} r-200-default) "/" {:value 123})))))
5555

5656
(testing "custom coercion"
5757

@@ -62,23 +62,21 @@
6262

6363
(testing "by default, applies response coercion"
6464
(let [app (api
65-
{:formatter :muuntaja}
65+
{}
6666
ping-route)]
6767
(is-fails-with 500 (get* app "/ping"))))
6868

6969
(testing "response-coercion can be disabled"
7070
(testing "separately"
7171
(let [app (api
72-
{:formatter :muuntaja
73-
:coercion (cs/create-coercion (dissoc cs/default-options :response))}
72+
{:coercion (cs/create-coercion (dissoc cs/default-options :response))}
7473
ping-route)]
7574
(let [[status body] (get* app "/ping")]
7675
(is (= 200 status))
7776
(is (= {:pong 123} body)))))
7877
(testing "all coercion"
7978
(let [app (api
80-
{:formatter :muuntaja
81-
:coercion nil}
79+
{:coercion nil}
8280
ping-route)]
8381
(let [[status body] (get* app "/ping")]
8482
(is (= 200 status))
@@ -88,14 +86,14 @@
8886
(binding [*async?* true]
8987
(testing "successful"
9088
(let [app (api
91-
{:formatter :muuntaja}
89+
{}
9290
(GET "/async" []
9391
:return s/Str
9492
(a/go (ok "abc"))))]
9593
(is-has-body "abc" (get* app "/async"))))
9694
(testing "failing"
9795
(let [app (api
98-
{:formatter :muuntaja}
96+
{}
9997
(GET "/async" []
10098
:return s/Int
10199
(a/go (ok "foo"))))]
@@ -108,7 +106,7 @@
108106

109107
(testing "by default, applies body coercion (to set)"
110108
(let [app (api
111-
{:formatter :muuntaja}
109+
{}
112110
beer-route)]
113111
(let [[status body] (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]}))]
114112
(is (= 200 status))
@@ -117,15 +115,13 @@
117115
(testing "body-coercion can be disabled"
118116
(let [no-body-coercion (cs/create-coercion (dissoc cs/default-options :body))
119117
app (api
120-
{:formatter :muuntaja
121-
:coercion no-body-coercion}
118+
{:coercion no-body-coercion}
122119
beer-route)]
123120
(let [[status body] (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]}))]
124121
(is (= 200 status))
125122
(is (= {:beers ["ipa" "apa" "ipa"]} body))))
126123
(let [app (api
127-
{:formatter :muuntaja
128-
:coercion nil}
124+
{:coercion nil}
129125
beer-route)]
130126
(let [[status body] (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]}))]
131127
(is (= 200 status))
@@ -134,8 +130,7 @@
134130
(testing "body-coercion can be changed"
135131
(let [nop-body-coercion (cs/create-coercion (assoc cs/default-options :body {:default (constantly nil)}))
136132
app (api
137-
{:formatter :muuntaja
138-
:coercion nop-body-coercion}
133+
{:coercion nop-body-coercion}
139134
beer-route)]
140135
(is-fails-with 400 (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]})))))))
141136

@@ -146,7 +141,7 @@
146141

147142
(testing "by default, applies query coercion (string->int)"
148143
(let [app (api
149-
{:formatter :muuntaja}
144+
{}
150145
query-route)]
151146
(let [[status body] (get* app "/query" {:i 10})]
152147
(is (= 200 status))
@@ -155,8 +150,7 @@
155150
(testing "query-coercion can be disabled"
156151
(let [no-query-coercion (cs/create-coercion (dissoc cs/default-options :string))
157152
app (api
158-
{:formatter :muuntaja
159-
:coercion no-query-coercion}
153+
{:coercion no-query-coercion}
160154
query-route)]
161155
(let [[status body] (get* app "/query" {:i 10})]
162156
(is (= 200 status))
@@ -165,14 +159,13 @@
165159
(testing "query-coercion can be changed"
166160
(let [nop-query-coercion (cs/create-coercion (assoc cs/default-options :string {:default (constantly nil)}))
167161
app (api
168-
{:formatter :muuntaja
169-
:coercion nop-query-coercion}
162+
{:coercion nop-query-coercion}
170163
query-route)]
171164
(is-fails-with 400 (get* app "/query" {:i 10}))))))
172165

173166
(testing "route-specific coercion"
174167
(let [app (api
175-
{:formatter :muuntaja}
168+
{}
176169
(GET "/default" []
177170
:query-params [i :- s/Int]
178171
(ok {:i i}))

0 commit comments

Comments
 (0)