|
| 1 | +(ns compojure.api.coercion.spec |
| 2 | + (:require [schema.core] |
| 3 | + [clojure.spec.alpha :as s] |
| 4 | + [spec-tools.core :as st] |
| 5 | + [spec-tools.data-spec :as ds] |
| 6 | + [clojure.walk :as walk] |
| 7 | + [compojure.api.coercion.core :as cc] |
| 8 | + [spec-tools.swagger.core :as swagger] |
| 9 | + [compojure.api.common :as common] |
| 10 | + ;; side effects |
| 11 | + compojure.api.coercion.register-spec) |
| 12 | + (:import (clojure.lang IPersistentMap) |
| 13 | + (schema.core RequiredKey OptionalKey) |
| 14 | + (spec_tools.core Spec) |
| 15 | + (spec_tools.data_spec Maybe))) |
| 16 | + |
| 17 | +(def string-transformer |
| 18 | + (st/type-transformer |
| 19 | + st/string-transformer |
| 20 | + st/strip-extra-keys-transformer |
| 21 | + {:name :string})) |
| 22 | + |
| 23 | +(def json-transformer |
| 24 | + (st/type-transformer |
| 25 | + st/json-transformer |
| 26 | + st/strip-extra-keys-transformer |
| 27 | + {:name :json})) |
| 28 | + |
| 29 | +(defn default-transformer |
| 30 | + ([] (default-transformer :default)) |
| 31 | + ([name] (st/type-transformer {:name name}))) |
| 32 | + |
| 33 | +(defprotocol Specify |
| 34 | + (specify [this name])) |
| 35 | + |
| 36 | +(extend-protocol Specify |
| 37 | + IPersistentMap |
| 38 | + (specify [this name] |
| 39 | + (-> (->> |
| 40 | + (walk/postwalk |
| 41 | + (fn [x] |
| 42 | + (if (and (map? x) (not (record? x))) |
| 43 | + (->> (for [[k v] (dissoc x schema.core/Keyword) |
| 44 | + :let [k (cond |
| 45 | + ;; Schema required |
| 46 | + (instance? RequiredKey k) |
| 47 | + (ds/req (schema.core/explicit-schema-key k)) |
| 48 | + |
| 49 | + ;; Schema options |
| 50 | + (instance? OptionalKey k) |
| 51 | + (ds/opt (schema.core/explicit-schema-key k)) |
| 52 | + |
| 53 | + :else |
| 54 | + k)]] |
| 55 | + [k v]) |
| 56 | + (into {})) |
| 57 | + x)) |
| 58 | + this) |
| 59 | + (ds/spec name)) |
| 60 | + (dissoc :name))) |
| 61 | + |
| 62 | + Maybe |
| 63 | + (into-spec [this name] |
| 64 | + (ds/spec name this)) |
| 65 | + |
| 66 | + Spec |
| 67 | + (specify [this _] this) |
| 68 | + |
| 69 | + Object |
| 70 | + (specify [this _] |
| 71 | + (st/create-spec {:spec this}))) |
| 72 | + |
| 73 | +(def memoized-specify |
| 74 | + (common/fifo-memoize #(specify %1 (keyword "spec" (name (gensym "")))) 1000)) |
| 75 | + |
| 76 | +(defn maybe-memoized-specify [spec] |
| 77 | + (if (keyword? spec) |
| 78 | + (specify spec nil) |
| 79 | + (memoized-specify spec))) |
| 80 | + |
| 81 | +(defn stringify-pred [pred] |
| 82 | + (str (if (instance? clojure.lang.LazySeq pred) |
| 83 | + (seq pred) |
| 84 | + pred))) |
| 85 | + |
| 86 | +(defmulti coerce-response? identity :default ::default) |
| 87 | +(defmethod coerce-response? ::default [_] true) |
| 88 | + |
| 89 | +(defrecord SpecCoercion [name options] |
| 90 | + cc/Coercion |
| 91 | + (get-name [_] name) |
| 92 | + |
| 93 | + (get-apidocs [_ _ {:keys [parameters responses] :as info}] |
| 94 | + (cond-> (dissoc info :parameters :responses) |
| 95 | + parameters (assoc |
| 96 | + ::swagger/parameters |
| 97 | + (into |
| 98 | + (empty parameters) |
| 99 | + (for [[k v] parameters] |
| 100 | + [k (maybe-memoized-specify v)]))) |
| 101 | + responses (assoc |
| 102 | + ::swagger/responses |
| 103 | + (into |
| 104 | + (empty responses) |
| 105 | + (for [[k response] responses] |
| 106 | + [k (update response :schema #(some-> % maybe-memoized-specify))]))))) |
| 107 | + |
| 108 | + (make-open [_ spec] spec) |
| 109 | + |
| 110 | + (encode-error [_ error] |
| 111 | + (let [problems (-> error :problems ::s/problems)] |
| 112 | + (-> error |
| 113 | + (update :spec (comp str s/form)) |
| 114 | + (assoc :problems (mapv #(update % :pred stringify-pred) problems))))) |
| 115 | + |
| 116 | + (coerce-request [_ spec value type format _] |
| 117 | + (let [spec (maybe-memoized-specify spec) |
| 118 | + type-options (options type)] |
| 119 | + (if-let [transformer (or (get (get type-options :formats) format) |
| 120 | + (get type-options :default))] |
| 121 | + (let [coerced (st/coerce spec value transformer)] |
| 122 | + (if (s/valid? spec coerced) |
| 123 | + coerced |
| 124 | + (let [conformed (st/conform spec coerced transformer)] |
| 125 | + (if (s/invalid? conformed) |
| 126 | + (let [problems (st/explain-data spec coerced transformer)] |
| 127 | + (cc/map->CoercionError |
| 128 | + {:spec spec |
| 129 | + :problems problems})) |
| 130 | + (s/unform spec conformed))))) |
| 131 | + value))) |
| 132 | + |
| 133 | + (accept-response? [_ spec] |
| 134 | + (boolean (coerce-response? spec))) |
| 135 | + |
| 136 | + (coerce-response [this spec value type format request] |
| 137 | + (cc/coerce-request this spec value type format request))) |
| 138 | + |
| 139 | +(def default-options |
| 140 | + {:body {:default (default-transformer) |
| 141 | + :formats {"application/json" json-transformer |
| 142 | + "application/msgpack" json-transformer |
| 143 | + "application/x-yaml" json-transformer}} |
| 144 | + :string {:default string-transformer} |
| 145 | + :response {:default (default-transformer) |
| 146 | + :formats {"application/json" (default-transformer :json) |
| 147 | + "application/msgpack" (default-transformer :json) |
| 148 | + "application/x-yaml" (default-transformer :json)}}}) |
| 149 | + |
| 150 | +(defn create-coercion [options] |
| 151 | + (->SpecCoercion :spec options)) |
| 152 | + |
| 153 | +(def default-coercion (create-coercion default-options)) |
0 commit comments