Skip to content

Commit

Permalink
[housekeeping] format
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Aug 23, 2024
1 parent fb45c72 commit f81a15f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 80 deletions.
64 changes: 32 additions & 32 deletions src/navi/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
; https://opensource.org/licenses/MIT.

(ns navi.core
(:import [java.util Map$Entry]
(:import [io.swagger.v3.oas.models Operation
PathItem
PathItem$HttpMethod]
[io.swagger.v3.oas.models.media MediaType Schema]
[io.swagger.v3.oas.models.parameters PathParameter
HeaderParameter
QueryParameter
RequestBody
Parameter]
[io.swagger.v3.oas.models.responses ApiResponse]
[io.swagger.v3.oas.models Operation
PathItem
PathItem$HttpMethod]
[io.swagger.v3.parser OpenAPIV3Parser]
[io.swagger.v3.parser.core.models ParseOptions]))
[io.swagger.v3.parser.core.models ParseOptions]
[java.util Map$Entry]))

(declare spec)

Expand Down Expand Up @@ -58,7 +58,7 @@
"Given a property and a required keys set, returns a malli spec.
Intended for RequestBody"
[required ^Map$Entry property]
(let [k (.getKey property)
(let [k (.getKey property)
key-schema [(keyword k)]
key-schema (if (contains? required k)
key-schema
Expand Down Expand Up @@ -126,10 +126,10 @@
(let [required (->> schema
.getRequired
(into #{}))
schemas (->> schema
.getProperties
(map #(->prop-schema required %))
(into []))]
schemas (->> schema
.getProperties
(map #(->prop-schema required %))
(into []))]
(into [:map {:closed false}] schemas)))

(defmethod spec
Expand Down Expand Up @@ -169,9 +169,9 @@
.stream
.findFirst
.get)
body-spec (-> content
.getSchema
schema->spec)]
body-spec (-> content
.getSchema
schema->spec)]
{:body (if (.getRequired param)
body-spec
[:or nil? body-spec])}))
Expand Down Expand Up @@ -220,19 +220,19 @@
"Converts a Java Operation to a map of parameters, responses, schemas and handler
that conforms to reitit."
[^Operation op handlers]
(let [params (into [] (.getParameters op))
(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))
responses (-> (.getResponses op)
(update-kvs handle-response-key response->data)) ]
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))
responses (-> (.getResponses op)
(update-kvs handle-response-key response->data))]
(cond-> {:handler (get handlers (.getOperationId op))}
(seq schemas) (assoc :parameters schemas)
(seq responses) (assoc :responses responses))))
Expand Down Expand Up @@ -260,15 +260,15 @@
(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)})
{"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"})})
:body "Ok"})})
(-> "api.yaml"
slurp
(routes-from handlers)
Expand Down
95 changes: 47 additions & 48 deletions test/navi/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
(ns navi.core-test
(:require [clojure.test :refer [deftest testing is]]
[navi.core :as core])
(:import [java.util Map LinkedHashMap]
[io.swagger.v3.oas.models Operation PathItem]
(:import [io.swagger.v3.oas.models Operation PathItem]
[io.swagger.v3.oas.models.media Content StringSchema IntegerSchema JsonSchema
NumberSchema ObjectSchema ArraySchema MediaType UUIDSchema Schema]
[io.swagger.v3.oas.models.parameters Parameter PathParameter HeaderParameter QueryParameter RequestBody]
[io.swagger.v3.oas.models.responses ApiResponses ApiResponse]))
[io.swagger.v3.oas.models.responses ApiResponses ApiResponse]
[java.util Map LinkedHashMap]))

(deftest map-to-malli-spec
(testing "surrounding values of a clojure map to a malli map spec"
Expand Down Expand Up @@ -80,18 +80,18 @@
(let [props (doto (LinkedHashMap.)
(.put "x" (IntegerSchema.))
(.put "y" (StringSchema.)))
obj (doto (ObjectSchema.)
(.setRequired ["y" "x"])
(.setProperties props))
obj (doto (ObjectSchema.)
(.setRequired ["y" "x"])
(.setProperties props))
props-json (doto (LinkedHashMap.)
(.put "x" (doto (Schema.)
(.addType "integer")))
(.put "y" (doto (Schema.)
(.addType "string"))))
obj-json (doto (Schema.)
(.addType "object")
(.setRequired ["y" "x"])
(.setProperties props-json))]
obj-json (doto (Schema.)
(.addType "object")
(.setRequired ["y" "x"])
(.setProperties props-json))]
(is (= [:map {:closed false} [:x int?] [:y string?]]
(core/schema->spec obj)))
(is (= [:map {:closed false} [:x int?] [:y string?]]
Expand Down Expand Up @@ -134,23 +134,22 @@
(core/response->data response)))))
(testing "default media type"
(let [media (doto (MediaType.)
(.setSchema (StringSchema.)))
(.setSchema (StringSchema.)))
content (doto (Content.)
(.put "default" media))
response (doto (ApiResponse.)
(.setContent content)) ]
(.setContent content))]
(is (= {:content {:default {:schema string?}}}
(core/response->data response)))))
(testing "json object response"
(let [media (doto (MediaType.)
(.setSchema (ObjectSchema.)))
(.setSchema (ObjectSchema.)))
content (doto (Content.)
(.put "application/json" media))
response (doto (ApiResponse.)
(.setContent content)) ]
(.setContent content))]
(is (= {:content {"application/json" {:schema [:map {:closed false}]}}}
(core/response->data response)))))
)
(core/response->data response))))))

(deftest parameters-to-malli-spec
(testing "path"
Expand All @@ -174,72 +173,72 @@
(is (= {:header [[:x int?]]}
(core/param->data param)))))
(testing "required request body"
(let [media (doto (MediaType.)
(.setSchema (ObjectSchema.)))
(let [media (doto (MediaType.)
(.setSchema (ObjectSchema.)))
content (doto (Content.)
(.put "application/json" media))
param (doto (RequestBody.)
(.setRequired true)
(.setContent content))]
param (doto (RequestBody.)
(.setRequired true)
(.setContent content))]
(is (= {:body [:map {:closed false}]}
(core/param->data param)))))
(testing "optional request body"
(let [media (doto (MediaType.)
(.setSchema (ObjectSchema.)))
(let [media (doto (MediaType.)
(.setSchema (ObjectSchema.)))
content (doto (Content.)
(.put "application/json" media))
param (doto (RequestBody.)
(.setRequired false)
(.setContent content))]
param (doto (RequestBody.)
(.setRequired false)
(.setContent content))]
(is (= {:body [:or nil? [:map {:closed false}]]}
(core/param->data param)))))
(testing "implicitly optional request body"
(let [media (doto (MediaType.)
(.setSchema (ObjectSchema.)))
(let [media (doto (MediaType.)
(.setSchema (ObjectSchema.)))
content (doto (Content.)
(.put "application/json" media))
param (doto (RequestBody.)
(.setContent content))]
param (doto (RequestBody.)
(.setContent content))]
(is (= {:body [:or nil? [:map {:closed false}]]}
(core/param->data param))))))

(deftest openapi-operation-to-malli-spec
(testing "OpenAPI operation to reitit ring handler"
(let [param (doto (PathParameter.)
(.setName "x")
(.setSchema (IntegerSchema.)))
hparam (doto (HeaderParameter.)
(.setName "y")
(.setSchema (StringSchema.)))
response (doto (ApiResponse.)
(.setContent (doto (Content.)
(.put "application/json"
(doto (MediaType.)
(.setSchema (ObjectSchema.)))))))
(let [param (doto (PathParameter.)
(.setName "x")
(.setSchema (IntegerSchema.)))
hparam (doto (HeaderParameter.)
(.setName "y")
(.setSchema (StringSchema.)))
response (doto (ApiResponse.)
(.setContent (doto (Content.)
(.put "application/json"
(doto (MediaType.)
(.setSchema (ObjectSchema.)))))))
responses (doto (ApiResponses.)
(.put "200" response))
operation (doto (Operation.)
(.setParameters [param hparam])
(.setResponses responses)
(.setOperationId "TestOp"))
handlers {"TestOp" "a handler"}]
(is (= {:handler "a handler"
handlers {"TestOp" "a handler"}]
(is (= {:handler "a handler"
:parameters {:path [:map [:x int?]]
:header [:map [:y {:optional true} string?]]}
:responses {200 {:content {"application/json" {:schema [:map {:closed false}]}}}}}
(core/operation->data operation handlers))))))

(deftest openapi-path-to-malli-spec
(testing "OpenAPI path to reitit route"
(let [param (doto (PathParameter.)
(.setName "x")
(.setSchema (IntegerSchema.)))
(let [param (doto (PathParameter.)
(.setName "x")
(.setSchema (IntegerSchema.)))
operation (doto (Operation.)
(.setParameters [param])
(.setOperationId "TestOp"))
handlers {"TestOp" "a handler"}
handlers {"TestOp" "a handler"}
path-item (doto (PathItem.)
(.setGet operation))]
(is (= {:get {:handler "a handler"
(is (= {:get {:handler "a handler"
:parameters {:path [:map [:x int?]]}}}
(core/path-item->data path-item handlers))))))

0 comments on commit f81a15f

Please sign in to comment.