Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

:body-params turns record instances into plain maps

未关闭
#441 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
clojure
领域
api, backend

调研方向

从 compojure.api.coercion/coerce-request! 以及 src/compojure/api/meta.clj 第 367 行附近的 keywordizing 配置开始。使用 Transit record handlers 重现提供的 body 与 body-params 案例,然后添加或更新回归测试,使 body-params 保留 record 实例,同时保持现有的 keywordization 行为不变。

由索引模型根据 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

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

metosin/compojure-api 的其他 Issue

查看 metosin/compojure-api 的全部 Issue

相似的 Issue

更多 Clojure Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。