Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Pure map schemas are broken

Đang mở
#341 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
28/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
clojure, openapi
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu với compojure.api.coercion.core/named-coercion và compojure.api.coercion.schema/json-coercion-matcher, sau đó lần theo cấu hình bộ giải mã JSON của Muuntaja được hiển thị trong issue. Tái hiện một request bằng schema map thuần {s/Str s/Str}; hoàn tất khi các khóa string vẫn là string trong quá trình deserialization và coercion mà không làm hỏng các map có khóa là schema.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Not sure if thats by design, since Swagger doesn't seem to represent well pure maps, i.e. {s/Str s/Str}, but handling of those is broken atm.

What happens is that the default implementation forces Cheshire to keywordize all incoming maps while deserializing, so instead of {s/Str s/Str}, the request winds up as {s/Keyword s/Str} :(

The good (for my use case) news is that since Compojure API is almost infinitely extensible ;) I was able to:

  1. Override the JSON deserialization
  2. Plug a custom coercer

Something like this (only the important bits):

(ns blah.core
  (:require
           ;; just to show what cs and cc below resolve to
            [compojure.api.coercion.core :as cc]
            [compojure.api.coercion.schema :as cs])

;; coercion wiring

(defn keywordize-some
  "Keywordizes only the top-level map keys that are in the ks collection"
  [ks m]
  (reduce (fn [nm k]
            (if (keyword? k)
              (if-let [existing (get nm k)]
                nm
                (let [k-str (name k)]
                  (if-let [ex2 (get nm k-str)]
                    (-> nm
                        (assoc k ex2)
                        (dissoc k-str))
                    nm)))
              nm))
          m
          ks))

(defn json-coercer [schema x]
  "Coerces a map value with a map schema - otherwise falls back to the original coercer"
  (if (map? x)
    (keywordize-some (keys schema) x)
    (if-let [fallback (cs/json-coercion-matcher schema)]
      (fallback x)
      x)))

(defn jcm [schema]
  (if (map? schema)
    (partial json-coercer schema)
    (cs/json-coercion-matcher schema)))

(defmethod cc/named-coercion :json [_] (cs/create-coercion {:body     {:default (constantly nil)
                                                                       :formats {"application/json"    jcm
                                                                                 "application/msgpack" jcm
                                                                                 "application/x-yaml"  jcm}}
                                                            :string   {:default cs/string-coercion-matcher}
                                                            :response {:default (constantly nil)}}))

(api
    ....
     ;; wire a non-keywordizing JSON deserializer
     ;; the actual keywordization will be done in a custom coercer
     :formats    (-> (:formats compojure.api.api/api-defaults)
                     (assoc-in [:formats muuntaja.format.json/json-type] {:decoder [muuntaja.format.json/make-json-decoder {}]
                                                                          :encoder [muuntaja.format.json/make-json-encoder]}))

     ;; our custom coercer registered via defmethod above
     :coercion   :json
    ...
     }
Ngôn ngữ chính
Clojure
Star
1.1k
Fork
144
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của metosin/compojure-api

Tất cả issue của metosin/compojure-api

Issue tương tự

Thêm issue về Clojure

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.