From 3cc9fedcbf241cdc8d1a8f9cd56e446697d5daa9 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 18 Mar 2018 11:21:49 +0200 Subject: [PATCH] =?UTF-8?q?Fixed=20based=20on=20Miikka=E2=80=99s=20comment?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/reitit-core/src/reitit/coercion.cljc | 2 +- .../src/reitit/coercion/schema.cljc | 9 ++-- .../reitit-spec/src/reitit/coercion/spec.cljc | 8 ++-- .../reitit-swagger/src/reitit/swagger.cljc | 44 ++++++++----------- 4 files changed, 28 insertions(+), 35 deletions(-) diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index 36ed1fad..5c833dca 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 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") diff --git a/modules/reitit-schema/src/reitit/coercion/schema.cljc b/modules/reitit-schema/src/reitit/coercion/schema.cljc index 9d9b706a..1f626886 100644 --- a/modules/reitit-schema/src/reitit/coercion/schema.cljc +++ b/modules/reitit-schema/src/reitit/coercion/schema.cljc @@ -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] diff --git a/modules/reitit-spec/src/reitit/coercion/spec.cljc b/modules/reitit-spec/src/reitit/coercion/spec.cljc index ce905451..72e9dc84 100644 --- a/modules/reitit-spec/src/reitit/coercion/spec.cljc +++ b/modules/reitit-spec/src/reitit/coercion/spec.cljc @@ -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) diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index 1347a17e..ee232a99 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -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})}))))