Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Forest JSON schema duplicate fields #542

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

madalinoprea
Copy link

Given the following model hierarchy

module Screen
  class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true
  end
end

module Screen
  class Remediation < Screen::ApplicationRecord
    self.abstract_class = true
  end
end

module Screen
  class SctInRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end

module Screen
  class SctOutRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end

ForestLiana.models will include these child models twice, which will cause Forest
to generate a JSON schema with duplicated fields for the duplicated models.

ForestLiana.models.group_by{ |e| e }.select { |k, v| v.size > 1 }.map(&:first)
=> [Screen::SctInRemediation (call 'Screen::SctInRemediation.connection' to establish a connection),
 Screen::SctOutRemediation (call 'Screen::SctOutRemediation.connection' to establish a connection),
 Screen::SddInRemediation (call 'Screen::SddInRemediation.connection' to establish a connection)]
❯ cat .forestadmin-schema.json | jq '.collections[24].name'
"Screen__SctInRemediation"

❯ cat .forestadmin-schema.json | jq '.collections[24].fields[] | .field ' | sort
comment"
"created_at"
"created_at"
"id"
"id"
"status"
"status"
"updated_at"
"updated_at"

Given the following model hierarchy

```
module Screen
  class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true
  end
end

module Screen
  class Remediation < Screen::ApplicationRecord
    self.abstract_class = true
  end
end

module Screen
  class SctInRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end

module Screen
  class SctOutRemediation < Screen::Remediation
    self.table_name = "remediations"
  end
end
```

`ForestLiana.models` will include these child models twice, which will cause Forest
to generate a JSON schema with duplicated fields for the duplicated models.

```ruby
ForestLiana.models.group_by{ |e| e }.select { |k, v| v.size > 1 }.map(&:first)
=> [Screen::SctInRemediation (call 'Screen::SctInRemediation.connection' to establish a connection),
 Screen::SctOutRemediation (call 'Screen::SctOutRemediation.connection' to establish a connection),
 Screen::SddInRemediation (call 'Screen::SddInRemediation.connection' to establish a connection)]
```

```bash
❯ cat .forestadmin-schema.json | jq '.collections[24].name'
"Screen__SctInRemediation"

❯ cat .forestadmin-schema.json | jq '.collections[24].fields[] | .field ' | sort
comment"
"created_at"
"created_at"
"id"
"id"
"status"
"status"
"updated_at"
"updated_at"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant