From 9c7b4da832271931abb208952e5448a230a80c33 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Thu, 27 Jul 2023 16:02:43 +0200 Subject: [PATCH] [fix] [#143] Don't freeze meta info for types that don't support `with-meta` --- src/taoensso/nippy.clj | 4 ++-- test/taoensso/nippy_tests.clj | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index c6860a2c..0a9fa762 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -630,9 +630,9 @@ (defprotocol IFreezable1 (-freeze-without-meta! [x data-output])) (defprotocol IFreezable2 (-freeze-with-meta! [x data-output])) (extend-protocol IFreezable2 ; Must be a separate protocol - clojure.lang.IMeta + clojure.lang.IObj ; IMeta => `meta` will work, IObj => `with-meta` will work (-freeze-with-meta! [x ^DataOutput data-output] - (let [m (when *incl-metadata?* (.meta x))] + (let [m (when *incl-metadata?* (meta x))] (when m (write-id data-output id-meta) (-freeze-without-meta! m data-output))) diff --git a/test/taoensso/nippy_tests.clj b/test/taoensso/nippy_tests.clj index 863280c7..4040c09b 100644 --- a/test/taoensso/nippy_tests.clj +++ b/test/taoensso/nippy_tests.clj @@ -326,6 +326,8 @@ ;;;; Metadata +(def my-var "Just a string") + (deftest _metadata [(is (:has-meta? @@ -355,7 +357,12 @@ {:incl-metadata? true} ))) - "Metadata successfully excluded by freeze")]) + "Metadata successfully excluded by freeze") + + (is (var? (nippy/read-quarantined-serializable-object-unsafe! + (nippy/thaw (nippy/freeze #'my-var)))) + + "Don't try to preserve metadata on var")]) ;;;; Benchmarks