metosin / metosin/compojure-api

:body-params turns record instances into plain maps

Open
#441 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Clojure
Stars
1.1k
Forks
146
PR merge metrics
No merged PRs in 30d

Description

## 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:

```clj
(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](https://github.com/metosin/compojure-api/blob/85738b802c7f150f4f550ffca91a4782c75f8091/src/compojure/api/meta.clj#L367).

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](https://clojurians-log.clojureverse.org/ring-swagger/2017-01-06).

## Workaround

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

```clj
[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))))
```

Contributor guide

Open the contributing guide

Research direction

Start at compojure.api.coercion/coerce-request! and the keywordizing configuration in src/compojure/api/meta.clj around line 367. Reproduce the supplied body versus body-params cases with the Transit record handlers, then add or update a regression test so body-params preserves the record instance while existing keywordization behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.