We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hmm...Is there any way to get the coercion transform a string into a integer from a nested JSON body request?
e.g using the provided coercion examples if I have something like this:
(require '[clojure.spec.alpha :as sp]) (sp/def ::y int?) (sp/def ::ym (st/keys :req-un)) (def app (ring/ring-handler (ring/router ["/api" ["/ping" {:name ::ping :get (fn [_] {:status 200 :body "pong"})}] ["/plus/:z" {:name ::plus :post {:muuntaja m/instance :coercion rcs/coercion :parameters {:query {:x int?} :body ::ym :path {:z int?}} :responses {200 {:body {:total int?}}} :handler (fn [{:keys [parameters]}] (let [total (+ (-> parameters :query :x) (-> parameters :body :y) (-> parameters :path :z))] {:status 200 :body {:total total}}))}}]] {:data {:middleware [muuntaja/format-middleware rrc/coerce-exceptions-middleware rrc/coerce-request-middleware rrc/coerce-response-middleware]}})))
The following works:
(-> (app {:request-method :post :uri "/api/plus/3" :query-params {"x" "1"} :body "{\"y\": 2}" :headers {"content-type" "application/json"}}))
But the following (where "y":"2") doesn't:
(-> (app {:request-method :post :uri "/api/plus/3" :query-params {"x" "1"} :body "{\"y\": \"2\"}" :headers {"content-type" "application/json"}}) :body slurp (cheshire.core/parse-string)) {"spec" "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:mock-service.core/y]), :type :map, :leaf? false})", "problems" [], "type" "reitit.coercion/request-coercion", "coercion" "spec", "value" {"y" "2"}, "in" ["request" "body-params"]}
The text was updated successfully, but these errors were encountered:
You can create a custom coercion to do that. See https://github.com/metosin/reitit/blob/master/modules/reitit-spec/src/reitit/coercion/spec.cljc#L137 how the default coercion is built from options how things are transformed. Just change the json-transformer to string-transformer for "application/json".
json-transformer
string-transformer
Sorry, something went wrong.
That worked! Thanks a lot, I'll close this ticket
No branches or pull requests
Hmm...Is there any way to get the coercion transform a string into a integer from a nested JSON body request?
e.g using the provided coercion examples if I have something like this:
The following works:
But the following (where "y":"2") doesn't:
The text was updated successfully, but these errors were encountered: