mirror of
https://github.com/penpot/penpot.git
synced 2025-02-03 04:49:03 -05:00
🐛 Fix json encoding truncation issue
This commit is contained in:
parent
06bab212b5
commit
3363793d64
3 changed files with 38 additions and 17 deletions
|
@ -10,16 +10,13 @@
|
||||||
[app.common.logging :as l]
|
[app.common.logging :as l]
|
||||||
[app.common.transit :as t]
|
[app.common.transit :as t]
|
||||||
[app.config :as cf]
|
[app.config :as cf]
|
||||||
[app.util.json :as json]
|
[clojure.data.json :as json]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[ring.request :as rreq]
|
[ring.request :as rreq]
|
||||||
[ring.response :as rres]
|
[ring.response :as rres]
|
||||||
[yetti.adapter :as yt]
|
[yetti.adapter :as yt]
|
||||||
[yetti.middleware :as ymw])
|
[yetti.middleware :as ymw])
|
||||||
(:import
|
(:import
|
||||||
com.fasterxml.jackson.core.JsonParseException
|
|
||||||
com.fasterxml.jackson.core.io.JsonEOFException
|
|
||||||
com.fasterxml.jackson.databind.exc.MismatchedInputException
|
|
||||||
io.undertow.server.RequestTooBigException
|
io.undertow.server.RequestTooBigException
|
||||||
java.io.InputStream
|
java.io.InputStream
|
||||||
java.io.OutputStream))
|
java.io.OutputStream))
|
||||||
|
@ -34,11 +31,22 @@
|
||||||
{:name ::params
|
{:name ::params
|
||||||
:compile (constantly ymw/wrap-params)})
|
:compile (constantly ymw/wrap-params)})
|
||||||
|
|
||||||
(def ^:private json-mapper
|
(defn- get-reader
|
||||||
(json/mapper
|
^java.io.BufferedReader
|
||||||
{:encode-key-fn str/camel
|
[request]
|
||||||
:decode-key-fn (comp keyword str/kebab)
|
(let [^InputStream body (rreq/body request)]
|
||||||
:pretty true}))
|
(java.io.BufferedReader.
|
||||||
|
(java.io.InputStreamReader. body))))
|
||||||
|
|
||||||
|
(defn- read-json-key
|
||||||
|
[k]
|
||||||
|
(-> k str/kebab keyword))
|
||||||
|
|
||||||
|
(defn- write-json-key
|
||||||
|
[k]
|
||||||
|
(if (or (keyword? k) (symbol? k))
|
||||||
|
(str/camel k)
|
||||||
|
(str k)))
|
||||||
|
|
||||||
(defn wrap-parse-request
|
(defn wrap-parse-request
|
||||||
[handler]
|
[handler]
|
||||||
|
@ -53,8 +61,8 @@
|
||||||
(update :params merge params))))
|
(update :params merge params))))
|
||||||
|
|
||||||
(str/starts-with? header "application/json")
|
(str/starts-with? header "application/json")
|
||||||
(with-open [^InputStream is (rreq/body request)]
|
(with-open [reader (get-reader request)]
|
||||||
(let [params (json/decode is json-mapper)]
|
(let [params (json/read reader :key-fn read-json-key)]
|
||||||
(-> request
|
(-> request
|
||||||
(assoc :body-params params)
|
(assoc :body-params params)
|
||||||
(update :params merge params))))
|
(update :params merge params))))
|
||||||
|
@ -74,9 +82,7 @@
|
||||||
:code :request-body-too-large
|
:code :request-body-too-large
|
||||||
:hint (ex-message cause))
|
:hint (ex-message cause))
|
||||||
|
|
||||||
(or (instance? JsonEOFException cause)
|
(instance? java.io.EOFException cause)
|
||||||
(instance? JsonParseException cause)
|
|
||||||
(instance? MismatchedInputException cause))
|
|
||||||
(ex/raise :type :validation
|
(ex/raise :type :validation
|
||||||
:code :malformed-json
|
:code :malformed-json
|
||||||
:hint (ex-message cause)
|
:hint (ex-message cause)
|
||||||
|
@ -128,7 +134,8 @@
|
||||||
(-write-body-to-stream [_ _ output-stream]
|
(-write-body-to-stream [_ _ output-stream]
|
||||||
(try
|
(try
|
||||||
(with-open [^OutputStream bos (buffered-output-stream output-stream buffer-size)]
|
(with-open [^OutputStream bos (buffered-output-stream output-stream buffer-size)]
|
||||||
(json/write! bos data json-mapper))
|
(with-open [^java.io.OutputStreamWriter writer (java.io.OutputStreamWriter. bos)]
|
||||||
|
(json/write data writer :key-fn write-json-key)))
|
||||||
|
|
||||||
(catch java.io.IOException _)
|
(catch java.io.IOException _)
|
||||||
(catch Throwable cause
|
(catch Throwable cause
|
||||||
|
|
|
@ -19,7 +19,8 @@
|
||||||
[app.common.fressian :as fres]
|
[app.common.fressian :as fres]
|
||||||
[app.common.transit :as t]
|
[app.common.transit :as t]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[clojure.core :as c])
|
[clojure.core :as c]
|
||||||
|
[clojure.data.json :as json])
|
||||||
(:import
|
(:import
|
||||||
clojure.lang.Counted
|
clojure.lang.Counted
|
||||||
clojure.lang.IHashEq
|
clojure.lang.IHashEq
|
||||||
|
@ -83,6 +84,10 @@
|
||||||
^:unsynchronized-mutable loaded?
|
^:unsynchronized-mutable loaded?
|
||||||
^:unsynchronized-mutable modified?]
|
^:unsynchronized-mutable modified?]
|
||||||
|
|
||||||
|
json/JSONWriter
|
||||||
|
(-write [this writter options]
|
||||||
|
(json/-write (into {} this) writter options))
|
||||||
|
|
||||||
IHashEq
|
IHashEq
|
||||||
(hasheq [this]
|
(hasheq [this]
|
||||||
(when-not hash
|
(when-not hash
|
||||||
|
|
|
@ -40,7 +40,8 @@
|
||||||
[app.common.transit :as t]
|
[app.common.transit :as t]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[app.util.time :as dt]
|
[app.util.time :as dt]
|
||||||
[clojure.core :as c])
|
[clojure.core :as c]
|
||||||
|
[clojure.data.json :as json])
|
||||||
(:import
|
(:import
|
||||||
clojure.lang.Counted
|
clojure.lang.Counted
|
||||||
clojure.lang.IDeref
|
clojure.lang.IDeref
|
||||||
|
@ -75,6 +76,14 @@
|
||||||
^:unsynchronized-mutable modified?
|
^:unsynchronized-mutable modified?
|
||||||
^:unsynchronized-mutable loaded?]
|
^:unsynchronized-mutable loaded?]
|
||||||
|
|
||||||
|
json/JSONWriter
|
||||||
|
(-write [this writter options]
|
||||||
|
(json/-write {:type "pointer"
|
||||||
|
:id (get-id this)
|
||||||
|
:meta (meta this)}
|
||||||
|
writter
|
||||||
|
options))
|
||||||
|
|
||||||
IPointerMap
|
IPointerMap
|
||||||
(load! [_]
|
(load! [_]
|
||||||
(when-not *load-fn*
|
(when-not *load-fn*
|
||||||
|
|
Loading…
Add table
Reference in a new issue