-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcore.clj
45 lines (40 loc) · 1.39 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
; Copyright 2021- Rahul De
;
; Use of this source code is governed by an MIT-style
; license that can be found in the LICENSE file or at
; https://opensource.org/licenses/MIT.
(ns navi.core
(:require
[navi.impl :as i]
[navi.transform]) ;; TODO: Can this be improved?
(:import
[io.swagger.v3.parser OpenAPIV3Parser]
[io.swagger.v3.parser.core.models ParseOptions]))
(defn routes-from
"Takes in the OpenAPI JSON/YAML as string and a map of OperationId to handler fns.
Returns the reitit route map with malli schemas"
[^String api-spec handlers]
(let [parse-options (doto (ParseOptions.)
(.setResolveFully true))
contents (.readContents (OpenAPIV3Parser.) api-spec nil parse-options)
paths (.getPaths (.getOpenAPI contents))]
(->> #(i/path-item->data % handlers)
(i/update-kvs paths identity)
(mapv identity))))
(comment
(require '[clojure.pprint :as pp])
(set! *warn-on-reflection* true)
(def handlers
{"AddGet" (fn [{{{:keys [n1 n2]} :path} :parameters}]
{:status 200
:body (+ n1 n2)})
"AddPost" (fn [{{{:keys [n1 n2]} :body} :parameters}]
{:status 200
:body (+ n1 n2)})
"HealthCheck" (fn [_]
{:status 200
:body "Ok"})})
(-> "api.yaml"
slurp
(routes-from handlers)
pp/pprint))