taoensso / nippy

The fastest serialization library for Clojure
https://www.taoensso.com/nippy
Eclipse Public License 1.0
1.04k stars 60 forks source link

Thawing a frozen record returns back an older version of that Record Class #93

Closed smee closed 7 years ago

smee commented 7 years ago

I have the same problem as the original submitter of ticket #44. Somehow nippy seems to keep the old class definition of records when thawing an instance. As a demonstration please see here: I create a simple record with a custom toString method so we can see which version of the record we have:

user> (defrecord Foo [] Object (toString [_] "old"))
;; => user.Foo
user> (str (Foo.))
;; => old
user> (def frozen (taoensso.nippy/freeze (Foo.)))
;; => #'user/frozen
user> (str (taoensso.nippy/thaw frozen))
;; => old
user> (defrecord Foo [] Object (toString [_] "redefined"))
;; => user.Foo
user> (str (Foo.))
;; => redefined
user> (str (taoensso.nippy/thaw frozen))
;; => old
user> 

I'm using clojure 1.9.0-alpha14 and nippy 2.12.2

smee commented 7 years ago

If I replace the line in https://github.com/ptaoussanis/nippy/blob/master/src/taoensso/nippy.clj#L1100 with

(let [class  (clojure.lang.RT/classForName class-name)

I get a thawed instance of the redefined record. Transcript:

user> (in-ns 'taoensso.nippy) 
;; => #namespace[taoensso.nippy]
taoensso.nippy> (defn- read-record [in class-name]
  (let [content (thaw-from-in! in)]
    (try
      (let [class  (clojure.lang.RT/classForName class-name)
            method (.getMethod class "create" class-method-sig)]
        (.invoke method class (into-array Object [content])))
      (catch Exception e
        {:type :record
         :throwable e
         :nippy/unthawable {:class-name class-name :content content}}))))
;; => #'taoensso.nippy/read-record
taoensso.nippy> (in-ns 'user)
;; => #namespace[user]
user> (str (taoensso.nippy/thaw frozen))
;; => redefined
ptaoussanis commented 7 years ago

Hi Steffen, appreciate the clear example - and for digging into this!

That looks like a promising modification. Will investigate further and get a change up if there's no unexpected issues.

Much thanks, cheers! :-)

smee commented 7 years ago

You are welcome! Thanks for this great library.

ptaoussanis commented 7 years ago

Just pushed [com.taoensso/nippy "2.14.0-alpha1"] to Clojars with this change, cheers!