From c1cd08d24fd2d565a11795711b267c7b3fcbeac3 Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Mon, 8 Jul 2024 15:23:09 -0500 Subject: [PATCH] backport arity checks --- CHANGELOG.md | 1 + src/compojure/api/meta.clj | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ea86b85..9160c16c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Backport: use muuntaja in compojure.api.validator * **BREAKING**: removed `api-defaults` vars * add `-v1` suffix to vars +* Backport arity checks for `:body`, `:query`, `:headers ## 1.1.14 (2024-04-30) * Remove potemkin [#445](https://github.com/metosin/compojure-api/issues/445) diff --git a/src/compojure/api/meta.clj b/src/compojure/api/meta.clj index 1b093871..b219ac81 100644 --- a/src/compojure/api/meta.clj +++ b/src/compojure/api/meta.clj @@ -84,7 +84,7 @@ ; :swagger {:responses {200 {:schema User} ; 404 {:schema Error ; :description "Not Found"} } -; :paramerers {:query {:q s/Str} +; :parameters {:query {:q s/Str} ; :body NewUser}}} (defmethod restructure-param :swagger [_ swagger acc] (assoc-in acc [:swagger :swagger] swagger)) @@ -127,7 +127,11 @@ ; second is the Schema to be coerced! against. ; Examples: ; :body [user User] -(defmethod restructure-param :body [_ [value schema] acc] +(defmethod restructure-param :body [_ [value schema :as bv] acc] + (when-not (= "true" (System/getProperty "compojure.api.meta.allow-bad-body")) + (assert (= 2 (count bv)) + (str ":body should be [sym schema], provided: " bv + "\nDisable this check with -Dcompojure.api.meta.allow-bad-body=true"))) (-> acc (update-in [:lets] into [value (src-coerce! schema :body-params :body)]) (assoc-in [:swagger :parameters :body] schema))) @@ -136,7 +140,11 @@ ; second is the Schema to be coerced! against. ; Examples: ; :query [user User] -(defmethod restructure-param :query [_ [value schema] acc] +(defmethod restructure-param :query [_ [value schema :as bv] acc] + (when-not (= "true" (System/getProperty "compojure.api.meta.allow-bad-query")) + (assert (= 2 (count bv)) + (str ":query should be [sym schema], provided: " bv + "\nDisable this check with -Dcompojure.api.meta.allow-bad-query=true"))) (-> acc (update-in [:lets] into [value (src-coerce! schema :query-params :string)]) (assoc-in [:swagger :parameters :query] schema))) @@ -145,7 +153,12 @@ ; second is the Schema to be coerced! against. ; Examples: ; :headers [headers Headers] -(defmethod restructure-param :headers [_ [value schema] acc] +(defmethod restructure-param :headers [_ [value schema :as bv] acc] + (when-not (= "true" (System/getProperty "compojure.api.meta.allow-bad-headers")) + (assert (= 2 (count bv)) + (str ":headers should be [sym schema], provided: " bv + "\nDisable this check with -Dcompojure.api.meta.allow-bad-headers=true"))) + (-> acc (update-in [:lets] into [value (src-coerce! schema :headers :string)]) (assoc-in [:swagger :parameters :header] schema)))