Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

:body-params turns record instances into plain maps

オープン
#441 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
clojure
領域
api, backend

調査の方向性

compojure.api.coercion/coerce-request! と、src/compojure/api/meta.clj の 367 行付近にあるキーワード化の設定から始めます。Transit のレコードハンドラーを使って、提示された body と body-params のケースを再現し、その後、既存のキーワード化の動作を維持しつつ body-params がレコードインスタンスを保持するように、回帰テストを追加または更新します。

索引モデルが issue の本文から書いたものです。

説明

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))))
主要言語
Clojure
スター
1.1k
フォーク
144
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

metosin/compojure-api のほかの issue

metosin/compojure-api の issue をすべて見る

似ている issue

Clojure の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。