Open DawidJanczak opened 3 years ago
I'm not sure I follow, dry-struct doesn't define as_json
, why should it care? Serializing to JSON is not part of the gem and I'm against mixing concerns in general.
Apologies @flash-gordon, I should have explained a bit more. It's not really about to_json
per se - that just highlights the bug. The main problem here is that the struct is not failing on missing key where I believe it should. I had a brief discussion with @solnic about it here https://discourse.dry-rb.org/t/dry-struct-missing-attribute-and-maybe-type/1198
Thanks, I'll look into that. My gut feeling tells me such a change would break a lot of code.
@flash-gordon do you know why this happens though? Was it on purpose?
Describe the bug
When I have a field that’s required, but its value is a maybe (attribute :missing, Types::String.maybe
for example) not passing that field does not fail when I create an object with that attribute missing, but it does fail later. In our case it fails when we call
as_json` on the resulting struct, but the error itself seems to be coming from dry-types so I suspect this isn’t the only scenario when this would fail.To Reproduce
Reproducible script can be found under https://github.com/DawidJanczak/dry-struct-maybe-attribute-oddity/blob/master/test.rb
Expected behavior
Either instantiating a struct should fail earlier (this would be a potentially breaking change) or
as_json
should work as expected.My environment
Linux hp 5.10.11-arch1-1 #1 SMP PREEMPT Wed, 27 Jan 2021 13:53:16 +0000 x86_64 GNU/Linux