Description
Currently packwerk assumes that all has_many relationships are relationships to models (ActiveRecord for example). Such as example below:
class Example
has_many :accounts
end
Running above code on packwerk assumes that the relationship is to an Account model. However this assumption seems to also apply when the has_many relationship is written for a serializer:
class ExampleSerializer < ActiveModel::Serializer
has_many :accounts
end
packwerk would also assume the above code's has_many relationship is to an Account model, when it should be to the AccountSerializer model. Even if the serializer is explicitly defined, there will still be a violation to the Account model based on the has_many relationship.
To Reproduce
Write a serializer using active_model_serializers with a has_many relationship and put the serializer into a pack, run packwerk check on it and you should see a dependency violation on a model instead of the related serializer.
See this demo project for an example of this https://github.com/huan-ji/has_many_demo
See here for the post serializer, this serializer has a has_many relationship which should point to the CommentSerializer, both of these serializers live in the pack so there shouldn't be any violations, but as you can see here in the package_todo file, there is a violation on the Comment model.
Expected Behaviour
When running packwerk check for a serializer file that contains a has_many relationship, there should not be a violation for the model instead of the serializer, the code should not assume all has_many relationships are to models.
Description Currently
packwerk
assumes that allhas_many
relationships are relationships to models (ActiveRecord for example). Such as example below:Running above code on
packwerk
assumes that the relationship is to anAccount
model. However this assumption seems to also apply when thehas_many
relationship is written for a serializer:packwerk
would also assume the above code'shas_many
relationship is to anAccount
model, when it should be to theAccountSerializer
model. Even if the serializer is explicitly defined, there will still be a violation to theAccount
model based on thehas_many
relationship.This issue seems to apply to any serializers that has the
has_many
relationship syntax to define relationships to other serializers, here are a few examples: https://github.com/rails-api/active_model_serializers https://github.com/yosiat/panko_serializer https://github.com/okuramasafumi/alba https://github.com/cerebris/jsonapi-resourcesTo Reproduce Write a serializer using
active_model_serializers
with ahas_many
relationship and put the serializer into a pack, runpackwerk check
on it and you should see a dependency violation on a model instead of the related serializer. See this demo project for an example of this https://github.com/huan-ji/has_many_demoSee here for the post serializer, this serializer has a
has_many
relationship which should point to theCommentSerializer
, both of these serializers live in the pack so there shouldn't be any violations, but as you can see here in the package_todo file, there is a violation on the Comment model.Expected Behaviour When running
packwerk check
for a serializer file that contains ahas_many
relationship, there should not be a violation for the model instead of the serializer, the code should not assume allhas_many
relationships are to models.Screenshots![image](https://github.com/Shopify/packwerk/assets/13550952/7b455076-bf18-4c83-a7df-04ac7de48476)
Version Information