Compatibility issue between `pks` and `active_model_serializers` for serialization. `has_many:` syntax causing problems. Need workaround

This message was imported from the Ruby/Rails Modularity Slack server. Find more info in the import thread.

Message originally sent by slack user U72V5TEEY5V

Hey @AlexEvanczuk, I work on the same team as <@U70NN25TNJA>, been working on tooling around modularity and packwerk and loving your rust package. We are currently running into a compatibility issue between pks and active_model_serializers which we use for serialization for our app. This gem and seemingly other top serialization gems use the has_many: syntax to denote relationships for the serialized object, but does not indicate a direct dependency on the underlying model. We are seeing in the code here and the test here that pks code assumes that any use of has_many: indicates an ActiveRecord dependency, but in our case we’re working with serializers and not ActiveRecord models. Any ideas on how we could get around this?

See the docs here for some examples.

My quippy answer is: You found the test! And the code that does work. Submit a PR to improve what pks does!

The detection is clearly too broad for what it is saying that it does.

However, stepping back. How is a reference like this not a dependency on “the thing being referenced?”

Message originally sent by slack user U70VKODTUSA

Appreicate the responses Stephan :pray:. It is a dependency, absolutely. Just for serializers it works a bit different then an ActiveRecord model. For example if we had a

class Example
  has_many :accounts
end

For ActiveRecord, the dependency is on “Account” and if you provide a custom class like:
has_many :accounts, class_name: 'Company' then the dependency is on “Company” I believe (at least from a quick look at Packwerk). This is great for models :+1:

But for serializers it works a bit different. The dependency should be on AccountSerializer instead of Account . The current implementation in rust packs, would flag Account here. And if you provide a custom class like has_many :accounts, serializer: CompanySerializer then the dependency will be on CompanySerializer (which works today as its a class constant and not a string, it just also false positives for a dependency on Account which isn’t quite right).

Now the hard part is how to fix this? How do you differentiate between ActiveRecord and Serializers for when has_many is being used given a class. Are you even able too :thinking_face:? If you know a good idea, we be happy to work on a PR for this :smile:

The non-perfect solution is to create the context for what class you’re in and what that class inherits from. If serializers… well, you’re laying out how there’s a whole new chunk of logic the tool is missing today. If ActiveRecord… go with what exist today

I don’t see class-context parsing code though… so this might be a pretty high lift