diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index 4560db84..36ed1fad 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -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") diff --git a/modules/reitit-spec/src/reitit/coercion/spec.cljc b/modules/reitit-spec/src/reitit/coercion/spec.cljc index 52fb65e8..4ecad5e4 100644 --- a/modules/reitit-spec/src/reitit/coercion/spec.cljc +++ b/modules/reitit-spec/src/reitit/coercion/spec.cljc @@ -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) diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index 8e630dde..5e875620 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -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)