From 82a050b925ad8c7022e5a6a73c6a211153361883 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Tue, 9 Apr 2024 09:01:31 +0200 Subject: [PATCH] [mod] Don't attach empty metadata --- src/taoensso/nippy.clj | 11 ++++++----- test/taoensso/nippy_tests.clj | 6 ++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index d052b4e5..4ecd9ab9 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -596,7 +596,7 @@ (extend-protocol IFreezableWithMeta clojure.lang.IObj ; IMeta => `meta` will work, IObj => `with-meta` will work (-freeze-with-meta! [x ^DataOutput data-output] - (when-let [m (when *incl-metadata?* (meta x))] + (when-let [m (when *incl-metadata?* (not-empty (meta x)))] (write-id data-output id-meta) (write-map data-output m :is-metadata)) (-freeze-without-meta! x data-output)) @@ -1535,10 +1535,11 @@ id-meta-protocol-key ::meta-protocol-key id-meta - (let [m (thaw-from-in! in)] - (if *incl-metadata?* - (with-meta (thaw-from-in! in) (dissoc m ::meta-protocol-key)) - (do (thaw-from-in! in)))) + (let [m (thaw-from-in! in) ; Always consume from stream + x (thaw-from-in! in)] + (if-let [m (when *incl-metadata?* (not-empty (dissoc m ::meta-protocol-key)))] + (with-meta x m) + (do x))) id-cached-0 (thaw-cached 0 in) id-cached-1 (thaw-cached 1 in) diff --git a/test/taoensso/nippy_tests.clj b/test/taoensso/nippy_tests.clj index 3960557d..12e12232 100644 --- a/test/taoensso/nippy_tests.clj +++ b/test/taoensso/nippy_tests.clj @@ -97,8 +97,10 @@ (is (enc/throws? :ex-info "Unfreezable type" (nippy/freeze (fn [])))) (testing "Clojure v1.10+ metadata protocol extensions" - [(is (= (meta (nippy/thaw (nippy/freeze (with-meta [] {:a :A, 'b/c (fn [])})))) {:a :A})) - (is (enc/throws? :ex-info "Unfreezable type" (nippy/freeze (with-meta [] {:a :A, 'b (fn [])}))))]) + [(is (enc/throws? :ex-info "Unfreezable type" (nippy/freeze (with-meta [] {:a :A, 'b (fn [])})))) + (is (= {:a :A} (meta (nippy/thaw (nippy/freeze (with-meta [] {:a :A, 'b/c (fn [])})))))) + (is (= nil (meta (nippy/thaw (nippy/freeze (with-meta [] { 'b/c (fn [])}))))) + "Don't attach empty metadata")]) (is (gen-test 1600 [gen-data] (= gen-data (thaw (freeze gen-data)))) "Generative")])