Skip to content

Sum operator not working when default values are defined #158

Description

@npearson72

Describe the bug

Given this setup:

class A < Dry::Struct
  attribute :foo_a, Types::Strict::String.default("foo_a")
  attribute :foo_b, Types::Strict::String.default("foo_b")
end

class B < Dry::Struct
  attribute :bar_a, Types::Strict::String.default("bar_a")
  attribute :bar_b, Types::Strict::String.default("bar_b")
end

class C < Dry::Struct
  attribute :baz, A | B
end

And this data:

data = { baz: { bar_a: "example", bar_b: "example" }}

I would expect:

C.new(data)

#=> #<C baz=#<B bar_a="example" bar_b="example">>

What I get is:

C.new(data)

#=> #<C baz=#<A foo_a="foo_a" foo_b="foo_b">>

I would expect the values I pass in to take precedence over the default values and for the sum operator to evaluate the keys and values to choose which struct to use.

To Reproduce

Please see above

Expected behavior

Please see above

Your environment

  • Affects my production application: Yes
  • Ruby version: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin19]
  • OS: MacOS version: 10.15.4 (19E287)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions