Comments (4)
@yosiat You are correct, this was not an issue with gem installation. My UserSerializer
and ContactSerializer
were a circular dependency, where ContactSerializer
would attempt to load UserSerializer
when it encountered the has_one :user
relationship, which in turn would try to load ContactSerializer
when it encountered the has_one :scoped_contact
relationship.
To be honest, I have no idea how my autoloader (Zeitwerk) resolves this, but the reason this was intermittent is because the load order of the files was inconsistent between containers. If the ContactSerializer
was loaded before the UserSerializer
, I would get the error condition with full_name
and photo_url
being nil
(presumably due to some method_missing
logic?). If the UserSerializer
got loaded first, then it would succeed. My best guess is that ContactSerializer
simply stopped loading any code after it encountered the has_one :user
relationship, possibly to resolve the circular dependency, and thus full_name
and photo_url
would never get defined. Or maybe they were defined, but UserSerializer
is holding a reference to a version of ContactSerializer
that has changed after UserSerializer
was defined.
My "fix" was to move the has_one :user
declaration below the full_name
and photo_url
methods, no more intermittent errors 🎉 .
I think this could be made more reliable. When you define relationships on an ActiveRecord model, they allow you to pass an argument class_name
(like passing serializer
in Panko) that is the constant name as a string, that is then lazily loaded. This is very useful as it will likely only be called after all files have been loaded.
Would you consider supporting a similar API that uses serializer: "UserSerializer"
, or alternatively I've seen something like serializer: -> { UserSerializer }
in other libraries. IMO the current behavior should be deprecated - it seems that circular dependencies with these serializers would be fairly common. For example I might have a PostSerializer
and CommentSerializer
that refer to each other when they are serialized (If I want post JSON, then I would to include comments, and if I want comment JSON, then I would it to include the post). The nature of Rails' autoloading makes these types of issues very difficult to debug, I'd hate for your other users to to have to go through this.
I wrote a test script which helped me identify this issue, but it's not a true repro as these single-file test scrips don't load classes and files in the same manner as would happen in a Rails environment. If you need I'd be happy to push a sample Rails app to demo this issue.
from panko_serializer.
@PhilCoggins sorry for replying one year later!
I implemented your suggestion here - #108, I hope it will solve this issue with circular dependency you have.
If you can run you tests with this branch and let me know if it solves the issue it will be amazing.
from panko_serializer.
Hi @PhilCoggins ,
I don't think this can issue can be because of how gems are installed on your system.
Is there any difference between those containers - Are they are serving the same endpoint and sometimes returns different results?
Can you try and create minimal reproduction of your code using https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb - schema, serializers and create an object that has bad serialization and I'll try to reproduce it as well?
from panko_serializer.
Hi @yosiat thank you for the speedy response.
The containers are identical AFAICT, and the test case is the exact same and data is returned as expected, but somehow the attribute methods are not being called (though the standard attribute methods are). I've been working on a reliable reproduction, I will update this issue as soon as I get one.
from panko_serializer.
Related Issues (20)
- Delegate + aliases not working as expected HOT 1
- Adding a custom root name for array serializer HOT 1
- Relax `oj` dependency HOT 3
- Is anyone actively maintaining this project? HOT 2
- Using ActiveRecord's pluck when possible
- Serialize attributes which is used `serialize` in active model definition
- Benchmarks task not working HOT 1
- How do I selectively display a key/value pair? HOT 2
- Alias declaration is ignored when method is added HOT 2
- Migration from AMS HOT 3
- Filter on array serializer? HOT 4
- Conditionally include associations in JSON response HOT 3
- Rendering recursive relations HOT 2
- Add context to relations has_one/has_many
- Ability to skip nil values HOT 2
- Loading dynamic field names during serialization HOT 1
- Documentation questions HOT 3
- Status HOT 1
- Serialization of single object with context / options HOT 2
- How to use a raw JSON string inside a serializer? HOT 2
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 panko_serializer.