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

Subclasses don't recognize properly when attribute described as Types::Array.of(Parent) #165

Open
k0va1 opened this issue Feb 6, 2021 · 0 comments

Comments

@k0va1
Copy link

k0va1 commented Feb 6, 2021

Describe the bug

When you try to pass subsclasses of parent class to attribute, dry-struct doesn't parse them as subsclases.

To Reproduce

Here is a code sample:

require 'dry/struct'
module Types
  include Dry.Types()
end

Filter = Class.new(Dry::Struct) do
  attribute :filter_type, Types::String
end

PriceFilter = Class.new(Filter) do
  attribute :min_price, Types::String
  attribute :max_price, Types::String
  attribute :tick_size, Types::String
end

MaxPositionFIlter = Class.new(Filter) do
  attribute :max_position, Types::String
end

Response = Class.new(Dry::Struct) do
  attribute :filters, Types::Array.of(Filter)
end

Response.new(filters: [{filter_type: 'foo', min_price: '1', max_price: '2', tick_size: '3'}, {filter_type: 'bar', max_position: '4'}])
# current behavior: array of FIlter types
=> #<Response filters=[#<Filter filter_type="foo">, #<Filter filter_type="bar">]>

Response = Class.new(Dry::Struct) do
  attribute :filters, Types::Array.of(PriceFilter | MaxPositionFIlter)
end

Response.new(filters: [{filter_type: 'foo', min_price: '1', max_price: '2', tick_size: '3'}, {filter_type: 'bar', max_position: '4'}])
# expected behavior: array of subclasses types
=> #<Response filters=[#<PriceFilter filter_type="foo" min_price="1" max_price="2" tick_size="3">, #<MaxPositionFIlter filter_type="bar" max_position="4">]>

My environment

  • Affects my production application: NO
  • Ruby version: 2.7.0p0
  • OS: Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant