~jomco/ring-openapi-validator

992e852a278c5bc0e66374105da4e7d307693886 — Joost Diepenmaat 3 years ago a2dcc74
Update to swagger-request-validator-core "2.8.3"
2 files changed, 28 insertions(+), 9 deletions(-)

M project.clj
M src/nl/zeekat/ring_openapi_validator.clj
M project.clj => project.clj +2 -2
@@ 4,8 4,8 @@
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.0" :scope "provided"]
                 [com.atlassian.oai/swagger-request-validator-core "2.8.3"]]
  :profiles {:dev {:dependencies [[org.clojure/data.json "0.2.7"]]
                 [com.atlassian.oai/swagger-request-validator-core "2.18.1"]]
  :profiles {:dev {:dependencies [[org.clojure/data.json "2.4.0"]]
                   :resource-paths ["dev-resources"]
                   :plugins [[lein-codox "0.10.7"]]
                   :codox {:metadata {:doc/format :markdown}

M src/nl/zeekat/ring_openapi_validator.clj => src/nl/zeekat/ring_openapi_validator.clj +26 -7
@@ 4,10 4,13 @@
           com.atlassian.oai.validator.model.Request
           com.atlassian.oai.validator.model.Request$Method
           com.atlassian.oai.validator.model.Response
           com.atlassian.oai.validator.model.StringBody
           com.atlassian.oai.validator.report.ValidationReport
           com.atlassian.oai.validator.report.ValidationReport$Level
           com.atlassian.oai.validator.report.ValidationReport$MessageContext$Location
           java.util.Optional))
           java.util.Optional
           java.nio.charset.Charset
           java.nio.charset.StandardCharsets))

(def ^:private ring->Method
  {:get     Request$Method/GET


@@ 36,6 39,23 @@
             {}
             headers))

(defn- content-type->Charset
  [ctype]
  (let [charset (when ctype
                  (-> ctype
                      (string/replace #".*; *charset=" "")
                      (string/replace #" .*" "")))]
    (when (seq charset)
      (Charset/forName charset))))

(defn- ->StringBody
  [headers body]
  (when body
    (StringBody. body (-> (get headers "content-type")
                          first
                          (content-type->Charset)
                          (or StandardCharsets/UTF_8)))))

(defn- ring->Request
  [{:keys [uri request-method body query-params headers]}]
  (let [headers (normalize-headers headers)]


@@ 46,6 66,8 @@
        (ring->Method request-method))
      (getBody []
        (Optional/ofNullable body))
      (getRequestBody []
        (Optional/ofNullable (->StringBody headers body)))
      (getQueryParameters []
        (->> query-params
             keys


@@ 69,6 91,8 @@
        status)
      (getBody []
        (Optional/ofNullable body))
      (getResponseBody []
        (Optional/ofNullable (->StringBody headers body)))
      (getHeaderValues [n]
        (->coll (get headers (string/lower-case n))))
      (getHeaderValue [n]


@@ 94,11 118,6 @@
  {ValidationReport$MessageContext$Location/REQUEST  :request
   ValidationReport$MessageContext$Location/RESPONSE :response})

(defn- Context->map
  [ctx]
  ;; TODO
  )

(defn- report->coll
  [report]
  (let [coll (mapv Message->map (.getMessages report))]


@@ 127,7 146,7 @@

(defn validate-interaction
  "Validate a `request`/`response` pair using the given `validator`.
  

  If any issues are found, returns a report collection"
  [validator request response]
  (report->coll (.validate validator (ring->Request request) (ring->Response response))))