Skip to content
New issue

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

json body coercion #288

Closed
rinconjc opened this issue Jun 5, 2019 · 2 comments
Closed

json body coercion #288

rinconjc opened this issue Jun 5, 2019 · 2 comments

Comments

@rinconjc
Copy link

rinconjc commented Jun 5, 2019

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"]}
@ikitommi
Copy link
Member

ikitommi commented Jun 5, 2019

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".

@rinconjc
Copy link
Author

rinconjc commented Jun 5, 2019

That worked!
Thanks a lot, I'll close this ticket

@rinconjc rinconjc closed this as completed Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants