Pure map schemas are broken
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
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:
- Override the JSON deserialization
- 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
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của metosin/compojure-api
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
metosin/compojure-api#489 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
metosin/compojure-api#468 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
metosin/compojure-api#467 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
metosin/compojure-api#464 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
metosin/compojure-api#463 · 3 bình luận ·
Tất cả issue của metosin/compojure-api
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
calcit-lang/calcit#2075 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
.Needs Triage .Team/Gadget Priority:P3 Type:Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
metabase/metabase#84431 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
module:db module:terminology-service-local refactoring
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày