From b23948d1e9d98633483745c5d880616fccdf8ffe Mon Sep 17 00:00:00 2001 From: Joost Diepenmaat Date: Thu, 22 Jul 2021 13:02:21 +0200 Subject: [PATCH] Cleanup: don't reimplement defaults from swagger-request-validator When using `proxy` we couldn't fallback on default interface method, but swithing to `reify` allows that. This change removes the re-implemented code. Less code is usually better. --- src/nl/zeekat/ring_openapi_validator.clj | 57 ++++++------------------ 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/src/nl/zeekat/ring_openapi_validator.clj b/src/nl/zeekat/ring_openapi_validator.clj index 7f1e408..c6e9791 100644 --- a/src/nl/zeekat/ring_openapi_validator.clj +++ b/src/nl/zeekat/ring_openapi_validator.clj @@ -39,66 +39,37 @@ {} 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)] - (proxy [Request] [] - (getPath [] + (reify Request + (getPath [this] uri) - (getMethod [] + (getMethod [this] (ring->Method request-method)) - (getBody [] + (getBody [this] (Optional/ofNullable body)) - (getRequestBody [] - (Optional/ofNullable (->StringBody headers body))) - (getQueryParameters [] + (getQueryParameters [this] (->> query-params keys (map name))) - (getQueryParameterValues [n] + (getQueryParameterValues [this n] (->coll (get query-params n))) - (getHeaders [] + (getHeaders [this] headers) - (getHeaderValues [n] - (->coll (get headers (string/lower-case n)))) - (getHeaderValue [n] - (Optional/ofNullable (first (.getHeaderValues this n)))) - (getContentType [] - (.getHeaderValue this "content-type"))))) + (getHeaderValues [this n] + (->coll (get headers (string/lower-case n))))))) (defn- ring->Response [{:keys [status body headers] :as response}] (let [headers (normalize-headers headers)] - (proxy [Response] [] - (getStatus [] + (reify Response + (getStatus [this] status) - (getBody [] + (getBody [this] (Optional/ofNullable body)) - (getResponseBody [] - (Optional/ofNullable (->StringBody headers body))) - (getHeaderValues [n] - (->coll (get headers (string/lower-case n)))) - (getHeaderValue [n] - (Optional/ofNullable (first (.getHeaderValues this n)))) - (getContentType [] - (.getHeaderValue this "content-type"))))) + (getHeaderValues [this n] + (->coll (get headers (string/lower-case n))))))) (def ^:private Level->key {ValidationReport$Level/ERROR :error -- 2.45.2