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"
(-get-name [this] "Keyword name for the coercion")
(-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")
(-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")

View file

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

View file

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

View file

@ -17,11 +17,11 @@
(def swagger-feature
"Feature for handling swagger-documentation for routes.
Works both with Middleware & Interceptors. Does not participate
in actual request processing, just provides specs for the extra
valid keys for the route data. Should be accompanied by a
in actual request processing, just provides specs for the new
documentation keys for the route data. Should be accompanied by a
[[swagger-spec-handler]] to expose the swagger spec.
Swagger-spesific keys:
Swagger-specific keys:
| key | description |
| --------------|-------------|
@ -69,28 +69,20 @@
"Ring handler to emit swagger spec."
[{:keys [::r/router ::r/match :request-method]}]
(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
(let [paths (->> router
(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
(let [paths (->> router (r/routes) (filter this-swagger?) (map transform-path) (into {}))]
{:status 200
:body (meta-merge
swagger
{:paths paths})}))))
:body (meta-merge swagger {:paths paths})}))))