dry-struct
dry-struct copied to clipboard
Subclasses don't recognize properly when attribute described as Types::Array.of(Parent)
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