-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.clj
219 lines (195 loc) · 6.31 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
; 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
(:import [java.util Map$Entry]
[io.swagger.v3.oas.models.media StringSchema
IntegerSchema
ObjectSchema
ArraySchema
NumberSchema
BooleanSchema
UUIDSchema
MediaType]
[io.swagger.v3.oas.models.parameters PathParameter
HeaderParameter
QueryParameter
RequestBody
Parameter]
[io.swagger.v3.oas.models Operation
PathItem]
[io.swagger.v3.parser OpenAPIV3Parser]
[io.swagger.v3.parser.core.models ParseOptions]))
(declare spec)
;; TODO: Better
(defn wrap-map
"Surrounds the key in a map for malli conformance"
[k m]
(if (contains? m k)
(update-in m
[k]
#(into [:map] %))
m))
;; TODO: Better
(defn ->prop-schema
"Given a property and a required keys set, returns a malli spec.
Intended for RequestBody"
[required ^Map$Entry property]
(let [k (.getKey property)
key-schema [(keyword k)]
key-schema (if (contains? required k)
key-schema
(conj key-schema {:optional true}))]
(conj key-schema
(-> property
.getValue
spec))))
(defn ->param-schema
"Given a param applies the similar logic as prop to schema
Intended for Parameter"
[^Parameter param]
(let [key-spec [(-> param
.getName
keyword)]
key-spec (if (.getRequired param)
key-spec
(conj key-spec {:optional true}))]
(conj key-spec
(-> param
.getSchema
spec))))
(defmulti spec class)
(defmethod spec
StringSchema
[_]
string?)
(defmethod spec
IntegerSchema
[_]
int?)
(defmethod spec
NumberSchema
[_]
number?)
(defmethod spec
BooleanSchema
[_]
boolean?)
(defmethod spec
UUIDSchema
[_]
uuid?)
(defmethod spec
ObjectSchema
[^ObjectSchema schema]
(let [required (->> schema
.getRequired
(into #{}))
schemas (->> schema
.getProperties
(map #(->prop-schema required %))
(into []))]
(into [:map {:closed false}] schemas)))
(defmethod spec
ArraySchema
[^ArraySchema schema]
(let [items (.getItems schema)]
[:sequential
(if (nil? items)
any?
(spec items))]))
(defmulti param->data class)
;; TODO: Better. The extra [] is there to help with merge-with into
(defmethod param->data
PathParameter
[param]
{:path [(->param-schema param)]})
(defmethod param->data
HeaderParameter
[param]
{:header [(->param-schema param)]})
(defmethod param->data
QueryParameter
[param]
{:query [(->param-schema param)]})
;; TODO: Handle more kinds of request-bodies
(defmethod param->data
RequestBody
[^RequestBody param]
(let [^MediaType content (-> param
.getContent
.values
.stream
.findFirst
.get)
body-spec (-> content
.getSchema
spec)
maybe-body (if (.getRequired param)
body-spec
[:or nil? body-spec])]
{:body maybe-body}))
(defn operation->data
"Converts an Operation to map of parameters, schemas and handler conforming to reitit"
[^Operation op handlers]
(let [params (into [] (.getParameters op))
request-body (.getRequestBody op)
params (if (nil? request-body)
params
(conj params request-body))
schemas (->> params
(map param->data)
(apply merge-with into)
(wrap-map :path)
(wrap-map :query)
(wrap-map :header))
handler {:handler (get handlers (.getOperationId op))}]
(if (seq schemas)
(assoc handler :parameters schemas)
handler)))
(defn path-item->data
"Converts a path to its corresponding vector of method and the operation map"
[^PathItem path-item handlers]
(->> path-item
.readOperationsMap
(map #(vector (-> ^Map$Entry %
.getKey
.toString
.toLowerCase
keyword)
(-> ^Map$Entry %
.getValue
(operation->data handlers))))
(into {})))
(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))]
(->> (.readContents (OpenAPIV3Parser.) api-spec nil parse-options)
.getOpenAPI
.getPaths
(mapv #(vector (.getKey ^Map$Entry %)
(-> ^Map$Entry %
.getValue
(path-item->data handlers)))))))
(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))