Skip to content

Commit

Permalink
Small refactor for better errors, adding jira regression test #103
Browse files Browse the repository at this point in the history
  • Loading branch information
oliyh committed Mar 8, 2021
1 parent 8779bf6 commit c84365e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
25 changes: 17 additions & 8 deletions core/src/martian/openapi.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,27 @@
(:properties schema)))
(leaf-schema schema))))))

(defn- get-matching-schema [object content-types]
(if-let [content-type (first (filter #(get-in object [:content (keyword %) :schema]) content-types))]
[(get-in object [:content (keyword content-type) :schema])
(defn- stringify-ns-keyword [k]
(if (keyword? k)
(if-let [ns (namespace k)]
(str ns "/" (name k))
(name k))
k))

(defn- get-matching-schema [{:keys [content]} content-types header-name]
(if-let [content-type (first (filter #(contains? content (keyword %)) content-types))]
[(get-in content [(keyword content-type) :schema])
content-type]
(when (seq (:content object))
(when (seq content)
#?(:clj (println "No matching content-type available" {:supported-content-types content-types
:available-content-types (map name (keys (:content object)))})
:available-content-types (map stringify-ns-keyword (keys content))
:header header-name})
:cljs (js/console.warn "No matching content-type available" {:supported-content-types content-types
:available-content-types (keys (get object "content"))})))))
:available-content-types (map stringify-ns-keyword (keys content))
:header header-name})))))

(defn- process-body [body components content-types]
(when-let [[json-schema content-type] (get-matching-schema body content-types)]
(when-let [[json-schema content-type] (get-matching-schema body content-types "Accept")]
(let [required (:required body)]
{:schema {(if required :body (s/optional-key :body))
(openapi->schema json-schema components)}
Expand All @@ -87,7 +96,7 @@
(defn- process-responses [responses components content-types]
(for [[status-code value] responses
:let [status-code (name status-code)
[json-schema content-type] (get-matching-schema value content-types)]]
[json-schema content-type] (get-matching-schema value content-types "Content-Type")]]
{:status (if (= status-code "default")
s/Any
(s/eq (if (number? status-code) status-code (read-string (name status-code)))))
Expand Down
1 change: 1 addition & 0 deletions core/test-resources/jira-openapi-v3.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions core/test/martian/openapi_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
(def openapi-2-json
(json-resource "openapi2.json"))

(def jira-openapi-v3-json
(json-resource "jira-openapi-v3.json"))

(deftest openapi-sanity-check
(testing "parses each handler"
(is (= {:summary "Update an existing pet"
Expand Down Expand Up @@ -92,3 +95,10 @@
(->> (filter #(= (:route-name %) :get-project-configuration)))
first
(dissoc :openapi-definition))))))

(deftest jira-openapi-v3-test
(is (= 410
(-> jira-openapi-v3-json
(openapi->handlers {:encodes ["json"]
:decodes ["json"]})
count))))

0 comments on commit c84365e

Please sign in to comment.