Skip to content

Commit

Permalink
middleware.format: print otherwise non-serializable objects as strings
Browse files Browse the repository at this point in the history
Fixes #722
  • Loading branch information
vemv committed Oct 7, 2021
1 parent c3249f8 commit 3db222d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bugs fixed

* [#719](https://github.com/clojure-emacs/cider-nrepl/issues/719): `middleware.test`: gracefully handle exceptions thrown within fixtures.
* [#722](https://github.com/clojure-emacs/cider-nrepl/issues/722): `middleware.format`: print otherwise non-serializable objects as strings.

## 0.27.2 (2021-10-03)

Expand Down
5 changes: 4 additions & 1 deletion src/cider/nrepl/middleware/format.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
(let [reader (readers/string-push-back-reader s)
sentinel (Object.)]
(loop [forms []]
(let [form (edn/read {:eof sentinel} reader)]
(let [form (edn/read {:eof sentinel
:default (fn [_tag value]
(pr-str value))}
reader)]
(if (= sentinel form)
forms
(recur (conj forms form)))))))
Expand Down
13 changes: 13 additions & 0 deletions test/clj/cider/nrepl/middleware/format_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@
(is (= formatted-edn-sample formatted-edn))
(is (= #{"done"} status))))

;; See: https://github.com/clojure-emacs/cider-nrepl/issues/722
(testing "Objects of classes without an associated data-reader function are converted to strings via `pr-str`"
(let [{:keys [formatted-edn status]
:as response} (session/message {:op "format-edn"
:edn (pr-str [1 2 (Object.) 3 4])})
[a b ^String c d e] (read-string formatted-edn)]
(testing (pr-str response)
(is (= [1 2 3 4]
[a b d e]))
(is (string? c))
(is (.contains c "java.lang.Object@"))
(is (= #{"done"} status)))))

(testing "format-edn works for multiple forms"
(let [{:keys [formatted-edn status]} (session/message {:op "format-edn"
:edn ugly-edn-forms-sample})]
Expand Down

0 comments on commit 3db222d

Please sign in to comment.