Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging in lazy sequences may interfere with output of Content-Type: application/edn #131

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions modules/muuntaja/src/muuntaja/format/edn.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
core/EncodeToBytes
(encode-to-bytes [_ data charset]
(.getBytes
(pr-str data)
^String charset))
(let [w (new java.io.StringWriter)]
(print-method data w)
(.toString w))
^String charset))
core/EncodeToOutputStream
(encode-to-output-stream [_ data charset]
(fn [^OutputStream output-stream]
Expand Down
19 changes: 19 additions & 0 deletions test/muuntaja/ring_middleware/format_response_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@
;; we do not set the "Content-Length"
#_(is (< 2 (Integer/parseInt (get-in resp [:headers "Content-Length"]))))))

(defn- produce-element-with-log [n]
(prn "reading from db" n)
{:element n})

(defn- dummy-handler-with-lazy-seq [_]
{:status 200
:body (map produce-element-with-log (range 4))})

(deftest lazy-sequences-with-logs
(let [handler (wrap-api-response
dummy-handler-with-lazy-seq
(-> m/default-options
(m/select-formats ["application/edn"])))
resp (handler {})
body (slurp (:body resp))]
;; Lazy sequence realization interferes with `with-out-str`
#_(is (= body "(\"reading from db\" 0\n\"reading from db\" 1\n\"reading from db\" 2\n\"reading from db\" 3\n{:element 0} {:element 1} {:element 2} {:element 3})"))
(is (= body "({:element 0} {:element 1} {:element 2} {:element 3})"))))

(def yaml-echo
(wrap-api-response
identity
Expand Down
Loading