-
Notifications
You must be signed in to change notification settings - Fork 64
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
Error when marshalling a Contentful resource object #53
Comments
We generate all classes and methods dynamically on the fly from the JSON response, which makes it hard to create an object that can be marshalled at the current point. It is on our radar to make this possible in a nice way, but there is no timeframe yet when this is going to happen. Let me know what you think also on how caching in the gem could be implemented. |
Thanks for the response. Good news is I did some further exploration and I discovered that the source of the error was actually the Rails logger instance I was passing in to the client on creation. Without that logger the marshal dump/load process appears to be working just fine. I'll probably code up my own workaround to excluding the logger when the client is marshaled but just wanted to share this finding with you guys, as it appears for the most part everything in your library supports marshaling. Thanks again! |
This is interesting, I will have a look and try to reproduce this. |
I know this is an old thread, but I just ran into this today, trying to implement Contentful caching for a client. We're using ContentfulModel, and these can be marshalled and cached fine, but they might have associations that are dynamic. It turns out there's actually a fairly easy fix. module Contentful
class DynamicEntry < Entry
# ...
def self.create(content_type)
# ...
Class.new DynamicEntry do
# ...
end
end
end
end This class is anonymous, and so Ruby will refuse to Marshal it. You can get around this by assigning it to a constant. def self.create
#...
klz = Class.new DynamicEntry do
# ...
end
const_set(:"#{content_type.id.camelize}Class", klz)
end Now it marshals and unmarhals fine. In the current 2.x line of development this DynamicEntry is gone though, so I'm not sure how to proceed.
|
Hey @plexus, Thanks for the heads up regarding the 1.x possible fix. Currently, I would argue in favor of moving to 2.0 (which will be released next Tuesday), in which Entries are no longer anonymous classes and simplify much of their behavior. There is a very small caveat regarding include resolution strategies in cases you have circular references, as there is no longer an entry-level cache, but you can tweak the settings to improve performance on those cases. I've explained it in further detail here: #124 (comment) Basically the move to 2.0 has little to no impact in most codebases, as the only external API that's changing is To use it in In the meantime, if you can overcome the issue by monkey patching it, I think that's a good compromise. As I think this should be a very short-term situation we'll be facing. Hope this helps and clarifies the short-term panorama. Cheers |
So this sounds like a pretty typical use case of this gem, but we've run into an issue where the built in Ruby Marshal class thrown an error when a Contentful::Resource object is dumped.
In a nutshell, we're using Contentful in a Ruby project and we're trying to cache the response we get from our call to the Contentful::Client. The Rails cache stores all in some form or another attempt to marshal an object (i.e. dump the object to a byte string) and save that value in the cache, and then load the object back when the cache result is fetched.
So the error we're hitting is coming up on almost every attempt to a marshal a Contentful::Resource, e.g. given a
response
from a client:This also happens with :dynamic_entries set to :auto.
As you can imagine, caching the results of a remote API call locally is a pretty big use case, so I'd be curious to see if this issue has come up elsewhere or if it's on the roadmap already. Also there's a good chance "I'm doing it wrong" and that there's another piece of the response that can be successfully dumped and loaded in a cache for these purposes.
Any insights would be great. Thanks! Also let me know if this issue would be better serviced via the support email channel.
The text was updated successfully, but these errors were encountered: