From 3f926254af2154faf44c120e4a1fa2425c8141a0 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Mon, 27 May 2024 14:39:09 -0500 Subject: [PATCH 1/3] backport routing print-method and specialized invoke --- src/compojure/api/routes.clj | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/compojure/api/routes.clj b/src/compojure/api/routes.clj index e4312523..317ddb02 100644 --- a/src/compojure/api/routes.clj +++ b/src/compojure/api/routes.clj @@ -10,7 +10,8 @@ [linked.core :as linked] [compojure.response] [schema.core :as s]) - (:import [clojure.lang AFn IFn Var])) + (:import (clojure.lang AFn IFn Var IDeref) + (java.io Writer))) ;; ;; Route records @@ -47,6 +48,19 @@ (update-in route [0] (fn [uri] (if (str/blank? uri) "/" uri)))) (-get-routes handler options)))) +(defn get-static-context-routes + ([handler] + (get-static-context-routes handler nil)) + ([handler options] + (filter (fn [[_ _ info]] (get info :static-context?)) + (get-routes handler options)))) + +(defn- realize-childs [route] + (update route :childs #(if (instance? IDeref %) @% %))) + +(defn- filter-childs [route] + (update route :childs (partial filter (partial satisfies? Routing)))) + (defrecord Route [path method info childs handler] Routing (-get-routes [this options] @@ -72,12 +86,25 @@ IFn (invoke [_ request] (handler request)) + (invoke [_ request respond raise] + (handler request respond raise)) + (applyTo [this args] (AFn/applyToHelper this args))) (defn create [path method info childs handler] (->Route path method info childs handler)) +(defmethod print-method Route + [this ^Writer w] + (let [childs (some-> this realize-childs filter-childs :childs seq vec)] + (.write w (str "#Route" + (cond-> (dissoc this :handler :childs) + (not (:path this)) (dissoc :path) + (not (seq (:info this))) (dissoc :info) + (not (:method this)) (dissoc :method) + childs (assoc :childs childs)))))) + ;; ;; Invalid route handlers ;; From 8d3c17db11a5a70f287e63501dd8220348401d5d Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Mon, 27 May 2024 14:47:31 -0500 Subject: [PATCH 2/3] backport methods var deref --- src/compojure/api/resource.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compojure/api/resource.clj b/src/compojure/api/resource.clj index 76d5c67b..b3d2a770 100644 --- a/src/compojure/api/resource.clj +++ b/src/compojure/api/resource.clj @@ -1,13 +1,14 @@ (ns compojure.api.resource (:require [compojure.api.routes :as routes] [compojure.api.coerce :as coerce] + [compojure.api.methods :as methods] [ring.swagger.common :as rsc] [schema.core :as s] [plumbing.core :as p] [compojure.api.middleware :as mw])) (def ^:private +mappings+ - {:methods #{:get :head :patch :delete :options :post :put} + {:methods methods/all-methods :parameters {:query-params [:query-params :query :string true] :body-params [:body-params :body :body false] :form-params [:form-params :formData :string true] From 7e4f63b1482849dc8f341c2edf9d753676c12e6c Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Thu, 6 Jun 2024 15:41:47 -0500 Subject: [PATCH 3/3] remove loading race conditions --- src/compojure/api/coercion.clj | 4 ++-- src/compojure/api/coercion/register_schema.clj | 8 -------- src/compojure/api/coercion/register_spec.clj | 8 -------- src/compojure/api/coercion/schema.clj | 6 +++--- src/compojure/api/coercion/spec.clj | 6 +++--- 5 files changed, 8 insertions(+), 24 deletions(-) delete mode 100644 src/compojure/api/coercion/register_schema.clj delete mode 100644 src/compojure/api/coercion/register_spec.clj diff --git a/src/compojure/api/coercion.clj b/src/compojure/api/coercion.clj index 0dd26b04..c891383d 100644 --- a/src/compojure/api/coercion.clj +++ b/src/compojure/api/coercion.clj @@ -4,8 +4,8 @@ [compojure.api.request :as request] [compojure.api.coercion.core :as cc] ;; side effects - compojure.api.coercion.register-schema - compojure.api.coercion.register-spec) + compojure.api.coercion.schema + compojure.api.coercion.spec) (:import (compojure.api.coercion.core CoercionError))) (def default-coercion :schema) diff --git a/src/compojure/api/coercion/register_schema.clj b/src/compojure/api/coercion/register_schema.clj deleted file mode 100644 index e1e8f993..00000000 --- a/src/compojure/api/coercion/register_schema.clj +++ /dev/null @@ -1,8 +0,0 @@ -(ns compojure.api.coercion.register-schema - (:require [compojure.api.coercion.core :as cc])) - -(defmethod cc/named-coercion :schema [_] - (deref - (or (resolve 'compojure.api.coercion.schema/default-coercion) - (do (require 'compojure.api.coercion.schema) - (resolve 'compojure.api.coercion.schema/default-coercion))))) diff --git a/src/compojure/api/coercion/register_spec.clj b/src/compojure/api/coercion/register_spec.clj deleted file mode 100644 index 143320fb..00000000 --- a/src/compojure/api/coercion/register_spec.clj +++ /dev/null @@ -1,8 +0,0 @@ -(ns compojure.api.coercion.register-spec - (:require [compojure.api.coercion.core :as cc])) - -(defmethod cc/named-coercion :spec [_] - (deref - (or (resolve 'compojure.api.coercion.spec/default-coercion) - (do (require 'compojure.api.coercion.spec) - (resolve 'compojure.api.coercion.spec/default-coercion))))) diff --git a/src/compojure/api/coercion/schema.clj b/src/compojure/api/coercion/schema.clj index 9a7e01b0..b308d0c2 100644 --- a/src/compojure/api/coercion/schema.clj +++ b/src/compojure/api/coercion/schema.clj @@ -5,9 +5,7 @@ [compojure.api.coercion.core :as cc] [clojure.walk :as walk] [schema.core :as s] - [compojure.api.common :as common] - ;; side effects - compojure.api.coercion.register-schema) + [compojure.api.common :as common]) (:import (java.io File) (schema.core OptionalKey RequiredKey) (schema.utils ValidationError NamedError))) @@ -86,3 +84,5 @@ (->SchemaCoercion :schema options)) (def default-coercion (create-coercion default-options)) + +(defmethod cc/named-coercion :schema [_] default-coercion) diff --git a/src/compojure/api/coercion/spec.clj b/src/compojure/api/coercion/spec.clj index b5d6ad31..9b20481a 100644 --- a/src/compojure/api/coercion/spec.clj +++ b/src/compojure/api/coercion/spec.clj @@ -6,9 +6,7 @@ [clojure.walk :as walk] [compojure.api.coercion.core :as cc] [spec-tools.swagger.core :as swagger] - [compojure.api.common :as common] - ;; side effects - compojure.api.coercion.register-spec) + [compojure.api.common :as common]) (:import (clojure.lang IPersistentMap) (schema.core RequiredKey OptionalKey) (spec_tools.core Spec) @@ -151,3 +149,5 @@ (->SpecCoercion :spec options)) (def default-coercion (create-coercion default-options)) + +(defmethod cc/named-coercion :spec [_] default-coercion)