From 3db883bea7f1f67cc97c0ddbbb5b3f874cde5e5c Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Mon, 22 Apr 2024 11:53:48 -0500 Subject: [PATCH] msg --- project.clj | 1 + src/compojure/api/meta.clj | 51 +++++++++++++++++++++-------- test/compojure/api/swagger_test.clj | 2 +- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/project.clj b/project.clj index 722a09ac..6bb692b4 100644 --- a/project.clj +++ b/project.clj @@ -48,6 +48,7 @@ [org.immutant/immutant "2.1.10"] [http-kit "2.3.0"] [criterium "0.4.5"]] + :jvm-opts ["-Dcompojure.api.meta.static-context-coach={:default :assert}"] :test-paths ["test19"] :ring {:handler examples.thingie/app :reload-paths ["src" "examples/thingie/src"]} diff --git a/src/compojure/api/meta.clj b/src/compojure/api/meta.clj index 36719c0d..9137c160 100644 --- a/src/compojure/api/meta.clj +++ b/src/compojure/api/meta.clj @@ -659,14 +659,12 @@ (defn- route-args? [arg] (not= arg [])) -(def endpoint-vars (conj (into #{} - (mapcat (fn [n] - (map #(symbol (name %) (name n)) - '[compojure.api.core - compojure.api.sweet]))) - '[GET ANY HEAD PATCH DELETE OPTIONS POST PUT]) - 'compojure.api.sweet/resource - 'compojure.api.resource/resource)) +(def endpoint-vars (into #{} + (mapcat (fn [n] + (map #(symbol (name %) (name n)) + '[compojure.api.core + compojure.api.sweet]))) + '[GET ANY HEAD PATCH DELETE OPTIONS POST PUT])) (def routes-vars #{'compojure.api.sweet/routes 'compojure.api.core/routes}) @@ -688,6 +686,26 @@ (and (routes-vars sym) (static-body? &env (next form)))))))))))) +(def resource-vars '#{compojure.api.sweet/resource + compojure.api.resource/resource}) + +(defn- static-resource? [&env form] + (and (seq? form) + (boolean + (let [sym (first form)] + (when (symbol? sym) + (when-some [v (resolve &env sym)] + (when (var? v) + (let [sym (symbol v)] + (when (and (resource-vars sym) + (= 1 (count form))) + (let [[_ data] form] + ;; TODO only needs to be static in a few places. + ;; is it enough to just test for map?. + (or (static-form? data) + (and (map? data) + ())))))))))))) + (def context-vars (into #{} (mapcat (fn [n] (map #(symbol (name %) (name n)) @@ -898,8 +916,14 @@ (-> info :public :static))) "Cannot be both a :dynamic and :static context.") - static? (not (or (-> info :public :dynamic) - (route-args? route-arg) (seq lets) (seq letks))) + bindings? (boolean (or (route-args? route-arg) (seq lets) (seq letks))) + + _ (assert (not (and (-> info :public :static) + bindings?)) + "A context cannot be :static and also provide bindings. Either push bindings into endpoints or remove :static.") + + static? (not (or (-> info :public :dynamic) bindings?)) + safely-static (or (-> info :public :static) (try (static-body? &env body) (catch Exception e @@ -928,16 +952,17 @@ "use (context ... :static true ...)." "\n\n" "To suppress this message for this namespace use -Dcompojure.api.meta.static-context-coach=" - "{" nsym :off "}" + "{" nsym " " :off "}" (when coach (str "\n\nCurrent coach config: " (pr-str coach))))] - (case coach + (case mode :off nil :print (println msg) :assert (throw (ex-info msg {:form &form :meta (meta &form)})) - (throw (ex-info "compojure.api.meta.static-context-coach must be either :off, :print, or :assert" {:provided coach}))))))) + (throw (ex-info "compojure.api.meta.static-context-coach mode must be either :off, :print, or :assert" {:coach coach + :provided mode}))))))) ;; :dynamic by default static-context? (and static? context? (boolean safely-static)) diff --git a/test/compojure/api/swagger_test.clj b/test/compojure/api/swagger_test.clj index 6254cad3..9269de93 100644 --- a/test/compojure/api/swagger_test.clj +++ b/test/compojure/api/swagger_test.clj @@ -14,7 +14,7 @@ (fact "all compojure.api.core macros are interpreted" (let [app (context "/a" [] (routes - (context "/b" [] + (context "/b" a (let-routes [] (GET "/c" [] identity) (POST "/d" [] identity)