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" "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 model data] "???") (-get-apidocs [this type data] "???")
(-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

@ -67,20 +67,25 @@
(reify coercion/Coercion (reify coercion/Coercion
(-get-name [_] :spec) (-get-name [_] :spec)
(-get-options [_] opts) (-get-options [_] opts)
(-get-apidocs [this _ {:keys [parameters responses] :as info}] (-get-apidocs [this type {:keys [parameters responses]}]
(cond-> (dissoc info :parameters :responses) (condp = type
parameters (assoc :swagger (merge
::swagger/parameters (if parameters
(into {::swagger/parameters
(empty parameters) (into
(for [[k v] parameters] (empty parameters)
[k (coercion/-compile-model this v nil)]))) (for [[k v] parameters]
responses (assoc [k (coercion/-compile-model this v nil)]))})
::swagger/responses (if responses
(into {::swagger/responses
(empty responses) (into
(for [[k response] responses] (empty responses)
[k (update response :body #(coercion/-compile-model this % nil))]))))) (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] (-compile-model [_ model name]
(into-spec model name)) (into-spec model name))
(-open-model [_ spec] spec) (-open-model [_ spec] spec)

View file

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