Fixed based on Miikka’s comments

This commit is contained in:
Tommi Reiman 2018-03-18 11:21:49 +02:00
parent f3058b90fe
commit 3cc9fedcbf
4 changed files with 28 additions and 35 deletions

View file

@ -12,7 +12,7 @@
"Pluggable coercion protocol" "Pluggable coercion protocol"
(-get-name [this] "Keyword name for the coercion") (-get-name [this] "Keyword name for the coercion")
(-get-options [this] "Coercion options") (-get-options [this] "Coercion options")
(-get-apidocs [this type data] "???") (-get-apidocs [this spesification data] "Returns api documentation")
(-compile-model [this model name] "Compiles a model") (-compile-model [this model name] "Compiles a model")
(-open-model [this model] "Returns a new model which allows extra keys in maps") (-open-model [this model] "Returns a new model which allows extra keys in maps")
(-encode-error [this error] "Converts error in to a serializable format") (-encode-error [this error] "Converts error in to a serializable format")

View file

@ -46,8 +46,9 @@
(reify coercion/Coercion (reify coercion/Coercion
(-get-name [_] :schema) (-get-name [_] :schema)
(-get-options [_] opts) (-get-options [_] opts)
(-get-apidocs [this type {:keys [parameters responses]}] (-get-apidocs [this spesification {:keys [parameters responses]}]
(condp = type ;; TODO: this looks identical to spec, refactor when schema is done.
(condp = spesification
:swagger (swagger/swagger-spec :swagger (swagger/swagger-spec
(merge (merge
(if parameters (if parameters
@ -65,8 +66,8 @@
[k (update response :schema #(coercion/-compile-model this % nil))]))}))) [k (update response :schema #(coercion/-compile-model this % nil))]))})))
(throw (throw
(ex-info (ex-info
(str "Can't produce Schem apidocs for " type) (str "Can't produce Schema apidocs for " spesification)
{:type type, :coercion :schema})))) {:type spesification, :coercion :schema}))))
(-compile-model [_ model _] model) (-compile-model [_ model _] model)
(-open-model [_ schema] (st/open-schema schema)) (-open-model [_ schema] (st/open-schema schema))
(-encode-error [_ error] (-encode-error [_ error]

View file

@ -68,8 +68,8 @@
(reify coercion/Coercion (reify coercion/Coercion
(-get-name [_] :spec) (-get-name [_] :spec)
(-get-options [_] opts) (-get-options [_] opts)
(-get-apidocs [this type {:keys [parameters responses]}] (-get-apidocs [this spesification {:keys [parameters responses]}]
(condp = type (condp = spesification
:swagger (swagger/swagger-spec :swagger (swagger/swagger-spec
(merge (merge
(if parameters (if parameters
@ -87,8 +87,8 @@
[k (update response :schema #(coercion/-compile-model this % nil))]))}))) [k (update response :schema #(coercion/-compile-model this % nil))]))})))
(throw (throw
(ex-info (ex-info
(str "Can't produce Spec apidocs for " type) (str "Can't produce Spec apidocs for " spesification)
{:type type, :coercion :spec})))) {:type spesification, :coercion :spec}))))
(-compile-model [_ model name] (-compile-model [_ model name]
(into-spec model name)) (into-spec model name))
(-open-model [_ spec] spec) (-open-model [_ spec] spec)

View file

@ -17,11 +17,11 @@
(def swagger-feature (def swagger-feature
"Feature for handling swagger-documentation for routes. "Feature for handling swagger-documentation for routes.
Works both with Middleware & Interceptors. Does not participate Works both with Middleware & Interceptors. Does not participate
in actual request processing, just provides specs for the extra in actual request processing, just provides specs for the new
valid keys for the route data. Should be accompanied by a documentation keys for the route data. Should be accompanied by a
[[swagger-spec-handler]] to expose the swagger spec. [[swagger-spec-handler]] to expose the swagger spec.
Swagger-spesific keys: Swagger-specific keys:
| key | description | | key | description |
| --------------|-------------| | --------------|-------------|
@ -69,28 +69,20 @@
"Ring handler to emit swagger spec." "Ring handler to emit swagger spec."
[{:keys [::r/router ::r/match :request-method]}] [{:keys [::r/router ::r/match :request-method]}]
(let [{:keys [id] :as swagger} (-> match :result request-method :data :swagger) (let [{:keys [id] :as swagger} (-> match :result request-method :data :swagger)
swagger (set/rename-keys swagger {:id :x-id})] swagger (set/rename-keys swagger {:id :x-id})
this-swagger? #(-> % second :swagger :id (= id))
transform-endpoint (fn [[method endpoint]]
(let [coercion (-> endpoint :data :coercion)]
(if (and endpoint (-> endpoint :data :no-doc not))
[method (meta-merge
(if coercion
(coercion/-get-apidocs coercion :swagger (-> endpoint :data)))
(-> endpoint :data (select-keys [:tags :summary :description]))
(-> endpoint :data :swagger (dissoc :id)))])))
transform-path (fn [[p _ c]]
(if-let [endpoint (some->> c (keep transform-endpoint) (seq) (into {}))]
[p endpoint]))]
(if id (if id
(let [paths (->> router (let [paths (->> router (r/routes) (filter this-swagger?) (map transform-path) (into {}))]
(r/routes)
(filter #(-> % second :swagger :id (= id)))
(map (fn [[p _ c]]
[p (some->> c
(keep
(fn [[m e]]
(let [coercion (-> e :data :coercion)]
(if (and e (-> e :data :no-doc not))
[m (meta-merge
(if coercion
(coercion/-get-apidocs coercion :swagger (-> e :data)))
(-> e :data (select-keys [:tags :summary :description]))
(-> e :data :swagger (dissoc :id)))]))))
(seq)
(into {}))]))
(filter second)
(into {}))]
;; TODO: create the swagger spec
{:status 200 {:status 200
:body (meta-merge :body (meta-merge swagger {:paths paths})}))))
swagger
{:paths paths})}))))