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

Incorrect processing of nil value for array with nested structure #108

Open
StanisLove opened this issue Oct 24, 2024 · 3 comments
Open

Comments

@StanisLove
Copy link

StanisLove commented Oct 24, 2024

Describe the bug

I get a KeyError when pass the nil value for option, but it's ok when the option is omitted

To Reproduce

class X
  extend Dry::Initializer

  option :fields, [], optional: true, default: proc { [] } do
    option :name
  end
end

X.new #=> #<X:0x000055cce7f56888 @fields=[]>
X.new(fields: []) #=> #<X:0x000055cce7df0b10 @fields=[]>
X.new(fields: nil) #=> KeyError (X::Fields: option 'name' is required)

Expected behavior

Getting and empty array

My environment

  • Ruby version: 2.7.5
  • OS: Linux
  • dry-initializer (3.1.1)
@StanisLove
Copy link
Author

And if I set the name as optional it doesn't make it much better...

class X
  extend Dry::Initializer

  option :fields, [], optional: true, default: proc { [] } do
    option :name, optional: true
  end
end

X.new(fields: nil) #=> #<X:0x000055cce6fe4760 @fields=[#<X::Fields:0x000055cce6fe45f8 @name=Dry::Initializer::UNDEFINED>]>

@Risto752
Copy link

Hi @StanisLove,

Thank you for opening this issue and providing clear examples. I’ve reviewed the code snippets, but I wasn’t able to locate the relevant code in the project that handles the fields option. As a result, I can't confirm the implementation details leading to the KeyError when passing nil.

From your description, it seems we should receive an empty array instead of an error. I also noticed that setting name as optional still results in unexpected behavior.

Is there any ongoing discussion or documentation about this feature? If not, I would suggest we revisit the handling of nil values for the fields option to ensure it defaults correctly when nil is passed.

If there’s no one currently working on a fix, I’d be happy to assist in implementing a solution once we clarify the intended behavior. Please let me know how you would like to proceed!

Thanks!

@Drowze
Copy link

Drowze commented Dec 11, 2024

Using extend Dry::Initializer[undefined: false] instead should do the trick:

class X
  extend Dry::Initializer

  option :fields, [], optional: true, default: proc { [] } do
    option :name
  end
end

X.new(fields: nil).fields
#=> []

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