compojure-api icon indicating copy to clipboard operation
compojure-api copied to clipboard

:body-params turns record instances into plain maps

Open metametadata opened this issue 4 years ago • 0 comments

Library Version(s)

2.0.0-alpha29

Problem

I pass record instances using transit+json format to api POST handler. It works fine when used with :body. But when :body-params is used: record instances are unexpectedly turned into plain maps.

Test code:

(ns unit.body-params
  (:require [clojure.java.io :as io]
            [clojure.test :refer :all]
            [cognitect.transit :as transit]
            [compojure.api.sweet :as c]
            [muuntaja.core :as muuntaja]
            [peridot.core :as peridot]
            [ring.util.http-response :as r])
  (:import [java.io ByteArrayOutputStream]))

(defrecord -Foo [bar])
(def -foo-tag "Foo")
(defn -read-foo [m] (map->-Foo m))
(defn -write-foo [v] (into {} v))
(def -transit-writer-handlers {-Foo (transit/write-handler (constantly -foo-tag) -write-foo)})
(def -transit-reader-handlers {-foo-tag (transit/read-handler -read-foo)})

(defn -serialize
  [v]
  (let [out (ByteArrayOutputStream.)]
    (transit/write (transit/writer out :json {:handlers -transit-writer-handlers}) v)
    (str out)))

(defn -deserialize
  [s]
  (transit/read (transit/reader (io/input-stream (.getBytes s)) :json {:handlers -transit-reader-handlers})))

(def -api-options
  {:coercion :spec
   :formats  (-> muuntaja/default-options
                 (assoc-in
                   [:formats "application/transit+json" :decoder-opts]
                   {:handlers -transit-reader-handlers})
                 (assoc-in
                   [:formats "application/transit+json" :encoder-opts]
                   {:handlers -transit-writer-handlers}))})

(defn -post
  [handler uri body-payload]
  (-> (peridot/session handler)
      (peridot/request uri
                       :request-method :post
                       :headers {"Accept" "application/transit+json"}
                       :content-type "application/transit+json"
                       :body (-serialize body-payload))
      :response
      (update :body #(-> %
                         slurp
                         -deserialize))))

(deftest passes-for-body
  (let [expected (->-Foo 100)
        handler (c/api
                  -api-options

                  (c/POST "/foo" []
                    :body [foo any?]
                    (r/ok foo)))

        ; Act
        actual (:body (-post handler "/foo" expected))]
    ; Assert
    (is (= expected actual))))

(deftest fails-for-body-params
  (let [expected (->-Foo 100)
        handler (c/api
                  -api-options

                  (c/POST "/foo" []
                    :body-params [foo :- any?]
                    (r/ok foo)))

        ; Act
        actual (:body (-post handler "/foo" {:foo expected}))]
    ; Assert
    (is (= expected actual))))

Cause

I tracked it to compojure.api.coercion/coerce-request! which calls walk/keywordize-keys which in turn recursively turns record instances into maps (which is a questionable behaviour on its own: https://clojure.atlassian.net/browse/CLJ-2505).

It's told to keywordize for :body-params in meta.clj.

This issue looks very similar to this PR about disabling keywordizing in :body: https://github.com/metosin/compojure-api/pull/265. We discussed it on Slack back in 2017.

Workaround

I had to patch walk/postwalk so that it doesn't touch instances of my protocol (using clj-fakes):

[clj-fakes.context :as fc]
[clojure.walk :as walk]
...
(def -patching-ctx (fc/context))

(fc/patch! -patching-ctx
           #'walk/postwalk
           (fn patched-postwalk
             [f form]
             (if (satisfies? MyProtocol form)
               form
               ((fc/original-val -patching-ctx #'walk/postwalk) f form))))

metametadata avatar Mar 22 '20 21:03 metametadata