-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from metosin/swagger-routes
Swagger routes
- Loading branch information
Showing
12 changed files
with
212 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,27 +23,27 @@ | |
|
||
(def app | ||
(api | ||
(swagger-ui) | ||
(swagger-docs | ||
{:info {:version "1.0.0" | ||
:title "Thingies API" | ||
:description "the description" | ||
:termsOfService "http://www.metosin.fi" | ||
:contact {:name "My API Team" | ||
:email "[email protected]" | ||
:url "http://www.metosin.fi"} | ||
:license {:name "Eclipse Public License" | ||
:url "http://www.eclipse.org/legal/epl-v10.html"}} | ||
:tags [{:name "math", :description "Math with parameters"} | ||
{:name "pizzas", :description "Pizza API"} | ||
{:name "failing", :description "Handling uncaught exceptions"} | ||
{:name "dates", :description "Dates API"} | ||
{:name "responses", :description "Responses demo"} | ||
{:name "primitives", :description "Returning primitive values"} | ||
{:name "context", :description "context routes"} | ||
{:name "echo", :description "Echoes data"} | ||
{:name "ordered", :description "Ordered routes"} | ||
{:name "file", :description "File upload"}]}) | ||
{:swagger {:ui "/" | ||
:spec "/swagger.json" | ||
:data {:info {:version "1.0.0" | ||
:title "Thingies API" | ||
:description "the description" | ||
:termsOfService "http://www.metosin.fi" | ||
:contact {:name "My API Team" | ||
:email "[email protected]" | ||
:url "http://www.metosin.fi"} | ||
:license {:name "Eclipse Public License" | ||
:url "http://www.eclipse.org/legal/epl-v10.html"}} | ||
:tags [{:name "math", :description "Math with parameters"} | ||
{:name "pizzas", :description "Pizza API"} | ||
{:name "failing", :description "Handling uncaught exceptions"} | ||
{:name "dates", :description "Dates API"} | ||
{:name "responses", :description "Responses demo"} | ||
{:name "primitives", :description "Returning primitive values"} | ||
{:name "context", :description "context routes"} | ||
{:name "echo", :description "Echoes data"} | ||
{:name "ordered", :description "Ordered routes"} | ||
{:name "file", :description "File upload"}]}}} | ||
|
||
(context "/math" [] | ||
:tags ["math"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
(ns compojure.api.swagger | ||
(:require [compojure.api.common :refer :all] | ||
[compojure.api.core :refer [GET undocumented]] | ||
[compojure.api.common :refer [extract-parameters]] | ||
(:require [compojure.api.core :as c] | ||
[compojure.api.common :as common] | ||
[compojure.api.middleware :as mw] | ||
[ring.util.http-response :refer [ok]] | ||
[ring.swagger.common :as rsc] | ||
|
@@ -13,10 +12,6 @@ | |
[compojure.api.routes :as routes] | ||
[cheshire.core :as cheshire])) | ||
|
||
;; | ||
;; generate schema names | ||
;; | ||
|
||
#_(defn ensure-parameter-schema-names [endpoint] | ||
(if (get-in endpoint [:parameters :body]) | ||
(update-in endpoint [:parameters :body] #(swagger/with-named-sub-schemas % "Body")) | ||
|
@@ -33,51 +28,34 @@ | |
responses)))) | ||
endpoint)) | ||
|
||
;; | ||
;; routes | ||
;; | ||
(defn base-path [request] | ||
(let [context (swagger/context request)] | ||
(if (= "" context) "/" context))) | ||
|
||
(defn swagger-spec-path | ||
[app] | ||
(some-> app | ||
routes/get-routes | ||
routes/route-lookup-table | ||
::swagger | ||
keys | ||
first)) | ||
|
||
(defn transform-operations [swagger] | ||
(->> swagger | ||
(swagger2/transform-operations routes/non-nil-routes) | ||
(swagger2/transform-operations routes/strip-no-doc-endpoints))) | ||
|
||
(defn base-path [request] | ||
(let [context (swagger/context request)] | ||
(if (= "" context) "/" context))) | ||
|
||
;; | ||
;; Public api | ||
;; | ||
|
||
(defn swagger-ui [& params] | ||
(undocumented | ||
(c/undocumented | ||
(apply rsui/swagger-ui params))) | ||
|
||
(defn swagger-docs | ||
"Route to serve the swagger api-docs. If the first | ||
parameter is a String, it is used as a url for the | ||
api-docs, otherwise \"/swagger.json\" will be used. | ||
Next Keyword value pairs OR a map for meta-data. | ||
Meta-data can be any valid swagger 2.0 data. Common | ||
case is to introduce API Info and Tags here: | ||
{:info {:version \"1.0.0\" | ||
:title \"Sausages\" | ||
:description \"Sausage description\" | ||
:termsOfService \"http://helloreverb.com/terms/\" | ||
:contact {:name \"My API Team\" | ||
:email \"[email protected]\" | ||
:url \"http://www.metosin.fi\"} | ||
:license {:name: \"Eclipse Public License\" | ||
:url: \"http://www.eclipse.org/legal/epl-v10.html\"}} | ||
:tags [{:name \"sausages\", :description \"Sausage api-set}]}" | ||
[& body] | ||
(defn swagger-docs [& body] | ||
(let [[path body] (if (string? (first body)) | ||
[(first body) (rest body)] | ||
["/swagger.json" body]) | ||
[extra-info] (extract-parameters body)] | ||
(GET path request | ||
[extra-info] (common/extract-parameters body false)] | ||
(c/GET path request | ||
:no-doc true | ||
:name ::swagger | ||
(let [runtime-info (rsm/get-swagger-data request) | ||
|
@@ -88,13 +66,51 @@ | |
spec (swagger2/swagger-json swagger options)] | ||
(ok spec))))) | ||
|
||
(defn swagger-spec-path [app] | ||
(some-> app | ||
routes/get-routes | ||
routes/route-lookup-table | ||
::swagger | ||
keys | ||
first)) | ||
;; | ||
;; Public api | ||
;; | ||
|
||
(def swagger-defaults {:ui "/", :spec "/swagger.json"}) | ||
|
||
(defn swagger-routes | ||
"Returns routes for swagger-articats (ui & spec). Accepts an options map, with the | ||
following options: | ||
**:ui** Uri for the swagger-ui (defaults to \"/\"). | ||
Setting the value to nil will cause the swagger-ui not to be mounted | ||
**:spec** Uri for the swagger-spec (defaults to \"/swagger.json\") | ||
Setting the value to nil will cause the swagger-ui not to be mounted | ||
**:data** Swagger data in the Ring-Swagger format. | ||
**:options** | ||
**:ui** Options to configure the ui | ||
**:spec** Options to configure the spec. Nada at the moment. | ||
Example options: | ||
{:ui \"/api-docs\" | ||
:spec \"/swagger.json\" | ||
:options {:ui {:jsonEditor true} | ||
:spec {}} | ||
:data {:info {:version \"1.0.0\" | ||
:title \"Sausages\" | ||
:description \"Sausage description\" | ||
:termsOfService \"http://helloreverb.com/terms/\" | ||
:contact {:name \"My API Team\" | ||
:email \"[email protected]\" | ||
:url \"http://www.metosin.fi\"} | ||
:license {:name: \"Eclipse Public License\" | ||
:url: \"http://www.eclipse.org/legal/epl-v10.html\"}} | ||
:tags [{:name \"sausages\", :description \"Sausage api-set\"}]}}" | ||
([] (swagger-routes {})) | ||
([options] | ||
(if options | ||
(let [{:keys [ui spec data] {ui-options :ui spec-options :spec} :options} (merge swagger-defaults options)] | ||
(c/routes | ||
(if ui (apply swagger-ui ui (mapcat identity (merge ui-options (if spec {:swagger-docs spec}))))) | ||
(if spec (apply swagger-docs spec (mapcat identity data)))))))) | ||
|
||
(defn validate | ||
"Validates a api. If the api is Swagger-enabled, the swagger-spec | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,8 +38,7 @@ | |
|
||
[compojure.api.swagger | ||
|
||
swagger-ui | ||
swagger-docs] | ||
swagger-routes] | ||
|
||
[ring.swagger.json-schema | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
(ns compojure.api.common-test | ||
(:require [compojure.api.common :refer :all] | ||
(:require [compojure.api.common :as common] | ||
[midje.sweet :refer :all])) | ||
|
||
(fact "group-with" | ||
(group-with pos? [1 -10 2 -4 -1 999]) => [[1 2 999] [-10 -4 -1]] | ||
(group-with pos? [1 2 999]) => [[1 2 999] nil]) | ||
(common/group-with pos? [1 -10 2 -4 -1 999]) => [[1 2 999] [-10 -4 -1]] | ||
(common/group-with pos? [1 2 999]) => [[1 2 999] nil]) | ||
|
||
(fact "extract-parameters" | ||
|
||
(facts "expect body" | ||
(common/extract-parameters [] true) => [{} nil] | ||
(common/extract-parameters [{:a 1}] true) => [{} [{:a 1}]] | ||
(common/extract-parameters [:a 1] true) => [{:a 1} nil] | ||
(common/extract-parameters [{:a 1} {:b 2}] true) => [{:a 1} [{:b 2}]] | ||
(common/extract-parameters [:a 1 {:b 2}] true) => [{:a 1} [{:b 2}]]) | ||
|
||
(facts "don't expect body" | ||
(common/extract-parameters [] false) => [{} nil] | ||
(common/extract-parameters [{:a 1}] false) => [{:a 1} nil] | ||
(common/extract-parameters [:a 1] false) => [{:a 1} nil] | ||
(common/extract-parameters [{:a 1} {:b 2}] false) => [{:a 1} [{:b 2}]] | ||
(common/extract-parameters [:a 1 {:b 2}] false) => [{:a 1} [{:b 2}]])) |
Oops, something went wrong.