metosin / metosin/compojure-api

Pure map schemas are broken

Open
#341 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Clojure
Stars
1.1k
Forks
146
PR merge metrics
No merged PRs in 30d

Description

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
...
}
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with compojure.api.coercion.core/named-coercion and compojure.api.coercion.schema/json-coercion-matcher, then trace the Muuntaja JSON decoder configuration shown in the issue. Reproduce a request using the pure map schema {s/Str s/Str}; done means string keys remain strings during deserialization and coercion without breaking schema-keyed maps.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure, openapi
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.