Giter Club home page Giter Club logo

Comments (4)

PhilCoggins avatar PhilCoggins commented on August 22, 2024 7

@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.

yosiat avatar yosiat commented on August 22, 2024 3

@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.

yosiat avatar yosiat commented on August 22, 2024

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.

PhilCoggins avatar PhilCoggins commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.