Comments (10)
This is happening here, maybe we should protect the code from extending singletons, nil
being one, maybe there's a better way than comparing to a list? Also is there any case where we would want to represent what would otherwise be a singleton?
from grape-roar.
It appears to be a design consequence of representable using inheritance (via mixin) instead of delegation (in the style of the Presenter pattern). One option aside from checking against a list is to see if the object is dupable.
def represent(object, _options = {})
dup = object.dup
dup.extend self
dup
rescue TypeError
fail TypeError.new "Can't represent #{object.class}"
end
This would be great, because even non-singletons aren't getting polluted when they're represented and then can continue to be used in another context. But this means that we can't represent anything that's not dupable. After a quick google, integers, symbols, true, false and floats aren't dupable and therefore would not be representable. I'm not sure if this is desirable behavior for grape-roar
from grape-roar.
Maybe we can just detect whether these are singleton types and make them non-representable?
from grape-roar.
This bit us suddenly, and very strangely. We also were mistakenly calling present nil, ...
in one endpoint. A later request to the same process would fail with surprising errors.
We're attempting to patch this by simply guarding against the nil
case (object.extend self unless object.nil?
), but as discussed above that might not be a complete fix.
from grape-roar.
@joeyAghion, I'm sorry that this bug has affected you today! My proposed solution can be one of two things:
- We can use a blacklist, even though it is not my preferred solution, but it is well defined and I can have it out for you quickly + later reconcile it out properly if time to fix is a problem.
- We can instead decorate the singleton classes of the instances of the objects that we receive. Additionally, we have a nice predicate for the objects we should avoid touching.
While not immediately obvious, the magic is in doing checks like this:
nil.singleton_class == nil.class #=> true
We would avoid that object, but we wouldn't avoid this one:
irb(main):013:0> class Foo; end;
=> nil
irb(main):014:0> x = Foo.new
=> #<Foo:0x007fc0e00ff138>
irb(main):015:0> x.singleton_class == x.class
=> false
Thoughts?
from grape-roar.
@ashkan18 was this your problem?
from grape-roar.
I'm interested in this (I work with @joeyAghion here :)) - I think the next step should be to properly spec it, @mach-kernel do you think you understand this enough to code up a failing spec?
from grape-roar.
Yes @dblock this was the issue in my case as well.
from grape-roar.
@dblock Yup! I'll draft up a spec for this.
from grape-roar.
I've opened #23 with the spec so we can explore fixes.
from grape-roar.
Related Issues (14)
- Lookup Representers Dynamically HOT 4
- ::nested hypermedia ::link raises an error HOT 3
- Does MultiJson.dump need to be passed env as well?
- Unable to represent properties from hash since grape v0.12.0 HOT 4
- Accessing options inside link method HOT 5
- undefined method `entries' HOT 1
- Nested representers HOT 1
- Represented objects via `extend` are not serializable
- Add support for documentation fields HOT 5
- Next version of Roar defines Representer HOT 2
- Dynamic options to to_json HOT 1
- Add a CRUD extension
- Enable type: presenter with Grape
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grape-roar.