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

Missing attribute and maybe type #166

Open
DawidJanczak opened this issue Feb 9, 2021 · 4 comments
Open

Missing attribute and maybe type #166

DawidJanczak opened this issue Feb 9, 2021 · 4 comments

Comments

@DawidJanczak
Copy link

Describe the bug

When I have a field that’s required, but its value is a maybe (attribute :missing, Types::String.maybefor 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 callas_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

  • Affects my production application: YES
  • Ruby version: 2.7
  • OS: Linux hp 5.10.11-arch1-1 #1 SMP PREEMPT Wed, 27 Jan 2021 13:53:16 +0000 x86_64 GNU/Linux
@flash-gordon
Copy link
Member

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.

@DawidJanczak
Copy link
Author

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

@flash-gordon
Copy link
Member

Thanks, I'll look into that. My gut feeling tells me such a change would break a lot of code.

@solnic
Copy link
Member

solnic commented Feb 10, 2021

@flash-gordon do you know why this happens though? Was it on purpose?

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

3 participants