Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Swagger/OpenAPI #19

Closed
ikitommi opened this issue Aug 17, 2017 · 3 comments
Closed

Support for Swagger/OpenAPI #19

ikitommi opened this issue Aug 17, 2017 · 3 comments

Comments

@ikitommi
Copy link
Member

ikitommi commented Aug 17, 2017

Proposal:

  • metosin/reitit-swagger (and metosin/reitit-openapi) module
  • :responses and :parameters data is read from coercion
  • other keys: :no-docs (disable docs), :swagger (any swagger-data)
  • swagger-middleware that doesn't participate in request processing but provides specs for the new keys for route data validation
  • swagger-spec-handler that produces the swagger-spec
  • [:swagger :id] defines the api(docs). All routes with the same id are part of the apidcs
    • enables multiple apidocs per routing tree. Value can be keyword or set of keyword
    • enables route to be part of multiple api-docs

Draft with data-specs:

(require '[reitit.ring :as ring])
(require '[reitit.ring.swagger :as swagger])
(require '[reitit.ring.coercion :as rrc])
(require '[reitit.coercion.spec :as spec])

(def app
  (ring/ring-handler
    (ring/router
      ["/api"
       ;; identify a swagger api
       ;; there can be several in a routing tree
       {:swagger {:id :math}}

       ;; the (undocumented) swagger spec endpoint
       ["/swagger.json"
        {:get {:no-doc true
               :swagger {:info {:title "my-api"}}
               :handler swagger/swagger-spec-handler}}]

       ["/minus"
        {:get {:summary "minus"
               :parameters {:query {:x int?, :y int?}}
               :responses {200 {:body {:total int?}}}
               :handler (fn [{{{:keys [x y]} :query} :parameters}]
                          {:status 200, :body {:total (- x y)}})}}]

       ["/plus"
        {:get {:summary "plus"
               :parameters {:query {:x int?, :y int?}}
               :responses {200 {:body {:total int?}}}
               :handler (fn [{{{:keys [x y]} :query} :parameters}]
                          {:status 200, :body {:total (+ x y)}})}}]]

      {:data {:middleware [;; does not particiate in request processing
                           ;; just defines specs for the extra keys
                           swagger/swagger-middleware
                           rrc/coerce-exceptions-middleware
                           rrc/coerce-request-middleware
                           rrc/coerce-response-middleware]
              :coercion spec/coercion}})))
@ikitommi
Copy link
Member Author

Works with spec (clj + cljs), Schema is wip.

(require '[reitit.ring :as ring])
(require '[reitit.swagger :as swagger])
(require '[reitit.ring.coercion :as rrc])
(require '[reitit.coercion.spec :as spec])
(require '[reitit.coercion.schema :as schema])

(require '[schema.core :refer [Int]])
(require '[muuntaja.middleware])

(ring/ring-handler
  (ring/router
    ["/api"
     {:swagger {:id ::math}}

     ["/swagger.json"
      {:get {:no-doc true
             :swagger {:info {:title "my-api"}}
             :handler swagger/swagger-spec-handler}}]

     ["/spec" {:coercion spec/coercion}
      ["/plus"
       {:get {:summary "plus"
              :parameters {:query {:x int?, :y int?}}
              :responses {200 {:body {:total int?}}}
              :handler (fn [{{{:keys [x y]} :query} :parameters}]
                         {:status 200, :body {:total (+ x y)}})}}]]

     ["/schema" {:coercion schema/coercion}
      ["/plus"
       {:get {:summary "plus"
              :parameters {:query {:x Int, :y Int}}
              :responses {200 {:body {:total Int}}}
              :handler (fn [{{{:keys [x y]} :query} :parameters}]
                         {:status 200, :body {:total (+ x y)}})}}]]]

    {:data {:middleware [muuntaja.middleware/wrap-format
                         swagger/swagger-feature
                         rrc/coerce-exceptions-middleware
                         rrc/coerce-request-middleware
                         rrc/coerce-response-middleware]}}))

@ikitommi
Copy link
Member Author

Schema is done too, all SNAPSHOTS.

@ikitommi
Copy link
Member Author

Ultimate Swagger guide at https://metosin.github.io/reitit/ring/swagger.html

Open Source automation moved this from To Do to Done May 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Open Source
  
Done
Development

No branches or pull requests

1 participant