metosin / metosin/compojure-api

:body-params turns record instances into plain maps

Offen
#441 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Clojure
Sterne
1.1k
Forks
146
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

## 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))))
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne bei compojure.api.coercion/coerce-request! und der Keywordisierungskonfiguration in src/compojure/api/meta.clj um Zeile 367. Reproduziere die mitgelieferten Fälle für body und body-params mit den Transit-Record-Handlern und füge anschließend einen Regressionstest hinzu oder aktualisiere ihn, sodass body-params die Record-Instanz beibehält, während das bestehende Keywordisierungsverhalten intakt bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
clojure
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.