Skip to content

Schema-like lets #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## UNRELEASED

Support `:-` in the `:query`, `:body` and `:headers`, e.g. the following are equivalent:

```clj
(POST "/user" []
:return User
:body [user User]
(ok user))

(POST "/user" []
:return User
:body [user :- User]
(ok user))
```

## 2.0.0-alpha15

* updated deps:
Expand Down
52 changes: 40 additions & 12 deletions src/compojure/api/meta.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@
{200 {:schema schema
:description (or (js/json-schema-meta schema) "")}})

(defn value-and-schema [[value schema ?schema :as binding]]
(cond
;; [body :- {:a s/Int}]
(and ?schema (= :- schema))
[value ?schema]

;; [body {:a s/Int}]
(not ?schema)
[value schema]

:else
(throw (ex-info (str "bad let-binding syntax: " binding) {}))))

;;
;; Extension point
;;
Expand Down Expand Up @@ -305,12 +318,17 @@
(help/code
"(POST \"/echo\" []"
" :body [body User]"
" (ok body))"
""
"(POST \"/echo\" []"
" :body [body :- User]"
" (ok body))")))

(defmethod restructure-param :body [_ [value schema] acc]
(-> acc
(update-in [:lets] into [value (src-coerce! schema :body-params :body false)])
(assoc-in [:info :public :parameters :body] schema)))
(defmethod restructure-param :body [_ binding acc]
(let [[value schema] (value-and-schema binding)]
(-> acc
(update-in [:lets] into [value (src-coerce! schema :body-params :body false)])
(assoc-in [:info :public :parameters :body] schema))))

;;
;; query
Expand All @@ -323,12 +341,17 @@
(help/code
"(GET \"/search\" []"
" :query [params {:q s/Str, :max s/Int}]"
" (ok params))"
""
"(GET \"/search\" []"
" :query [params :- {:q s/Str, :max s/Int}]"
" (ok params))")))

(defmethod restructure-param :query [_ [value schema] acc]
(-> acc
(update-in [:lets] into [value (src-coerce! schema :query-params :string)])
(assoc-in [:info :public :parameters :query] schema)))
(defmethod restructure-param :query [_ binding acc]
(let [[value schema] (value-and-schema binding)]
(-> acc
(update-in [:lets] into [value (src-coerce! schema :query-params :string)])
(assoc-in [:info :public :parameters :query] schema))))

;;
;; headers
Expand All @@ -341,12 +364,17 @@
(help/code
"(GET \"/headers\" []"
" :headers [headers HeaderSchema]"
" (ok headers))"
""
"(GET \"/headers\" []"
" :headers [headers :- HeaderSchema]"
" (ok headers))")))

(defmethod restructure-param :headers [_ [value schema] acc]
(-> acc
(update-in [:lets] into [value (src-coerce! schema :headers :string)])
(assoc-in [:info :public :parameters :header] schema)))
(defmethod restructure-param :headers [_ binding acc]
(let [[value schema] (value-and-schema binding)]
(-> acc
(update-in [:lets] into [value (src-coerce! schema :headers :string)])
(assoc-in [:info :public :parameters :header] schema))))

;;
;; body-params
Expand Down
27 changes: 27 additions & 0 deletions test/compojure/api/integration_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@
:return User
:query [user User]
(ok user))
(GET "/user2" []
:return User
:query [user :- User]
(ok user))
(GET "/invalid-user" []
:return User
(ok invalid-user))
Expand All @@ -211,6 +215,10 @@
:return User
:body [user User]
(ok user))
(POST "/user2" []
:return User
:body [user User]
(ok user))
(POST "/user_list" []
:return [User]
:body [users [User]]
Expand All @@ -223,6 +231,10 @@
:return User
:headers [user UserHeaders]
(ok (select-keys user [:id :name])))
(POST "/user_headers2" []
:return User
:headers [user :- UserHeaders]
(ok (select-keys user [:id :name])))
(POST "/user_legacy" {user :body-params}
:return User
(ok user))))]
Expand All @@ -237,11 +249,21 @@
status => 200
body => pertti))

(fact "GET with smart destructuring & :-"
(let [[status body] (get* app "/models/user2" pertti)]
status => 200
body => pertti))

(fact "POST with smart destructuring"
(let [[status body] (post* app "/models/user" (json-string pertti))]
status => 200
body => pertti))

(fact "POST with smart destructuring & :-"
(let [[status body] (post* app "/models/user2" (json-string pertti))]
status => 200
body => pertti))

(fact "POST with smart destructuring - lists"
(let [[status body] (post* app "/models/user_list" (json-string [pertti]))]
status => 200
Expand All @@ -262,6 +284,11 @@
status => 200
body => pertti))

(fact "POST with smart destructuring - headers & :-"
(let [[status body] (headers-post* app "/models/user_headers2" pertti)]
status => 200
body => pertti))

(fact "Validation of returned data"
(let [[status] (get* app "/models/invalid-user")]
status => 500))
Expand Down