Swagger WIP

This commit is contained in:
Tommi Reiman 2018-03-17 10:53:19 +02:00
parent 32c3082475
commit b43c8cfed9
3 changed files with 33 additions and 21 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 model data] "???")
(-get-apidocs [this type data] "???")
(-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

@ -67,20 +67,25 @@
(reify coercion/Coercion
(-get-name [_] :spec)
(-get-options [_] opts)
(-get-apidocs [this _ {:keys [parameters responses] :as info}]
(cond-> (dissoc info :parameters :responses)
parameters (assoc
::swagger/parameters
(into
(empty parameters)
(for [[k v] parameters]
[k (coercion/-compile-model this v nil)])))
responses (assoc
::swagger/responses
(into
(empty responses)
(for [[k response] responses]
[k (update response :body #(coercion/-compile-model this % nil))])))))
(-get-apidocs [this type {:keys [parameters responses]}]
(condp = type
:swagger (merge
(if parameters
{::swagger/parameters
(into
(empty parameters)
(for [[k v] parameters]
[k (coercion/-compile-model this v nil)]))})
(if responses
{::swagger/responses
(into
(empty responses)
(for [[k response] responses]
[k (update response :body #(coercion/-compile-model this % nil))]))}))
(throw
(ex-info
(str "Can't produce Spec apidocs for " type)
{:type type, :coercion :spec}))))
(-compile-model [_ model name]
(into-spec model name))
(-open-model [_ spec] spec)

View file

@ -2,7 +2,8 @@
(:require [reitit.core :as r]
[meta-merge.core :refer [meta-merge]]
[clojure.spec.alpha :as s]
[clojure.set :as set]))
[clojure.set :as set]
[reitit.coercion :as coercion]))
(s/def ::id keyword?)
(s/def ::no-doc boolean?)
@ -26,7 +27,7 @@
| --------------|-------------|
| :swagger | map of any swagger-data. Must have `:id` to identify the api
The following common route keys also contribute to swagger spec:
The following common keys also contribute to swagger spec:
| key | description |
| --------------|-------------|
@ -34,6 +35,9 @@
| :tags | optional set of strings of keywords tags for an endpoint api docs
| :summary | optional short string summary of an endpoint
| :description | optional long description of an endpoint. Supports http://spec.commonmark.org/
Also the coercion keys contribute to swagger spec:
| :parameters | optional input parameters for a route, in a format defined by the coercion
| :responses | optional descriptions of responess, in a format defined by coercion
@ -74,10 +78,13 @@
[p (some->> c
(keep
(fn [[m e]]
(if (and e (-> e :data :no-doc not))
[m (meta-merge
(-> e :data (select-keys [:tags :summary :description :parameters :responses]))
(-> e :data :swagger))])))
(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))]))))
(seq)
(into {}))]))
(filter second)