From 992e852a278c5bc0e66374105da4e7d307693886 Mon Sep 17 00:00:00 2001 From: Joost Diepenmaat Date: Thu, 22 Jul 2021 12:12:23 +0200 Subject: [PATCH] Update to swagger-request-validator-core "2.8.3" --- project.clj | 4 +-- src/nl/zeekat/ring_openapi_validator.clj | 33 +++++++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/project.clj b/project.clj index 8ac11d2..5c20914 100644 --- a/project.clj +++ b/project.clj @@ -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} diff --git a/src/nl/zeekat/ring_openapi_validator.clj b/src/nl/zeekat/ring_openapi_validator.clj index 964b5db..0559034 100644 --- a/src/nl/zeekat/ring_openapi_validator.clj +++ b/src/nl/zeekat/ring_openapi_validator.clj @@ -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)))) -- 2.45.2