elastic / elastic/elasticsearch-dsl-ruby

DSL constantizes & instantiates a class on method_missing

Open
#6 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Ruby
Stars
18
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Any time an `attr_reader` variable names happens to line up with **any** defined classes (even custom user classes), the DSL eats the `method_missing` call and attempts to construct that class. While it might be difficult/ to avoid collisions with builtin filter/query class names, it should be possible to avoid this for unsuspecting classes.

Not familiar with this codebase yet, but it seems like this `const_defined?` might be the issue:
https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-dsl/lib/elasticsearch/dsl/search/query.rb#L28

There may be a better solution, but a class method (e.g `is_elasticsearch_dsl_class?`) would do the trick.

```
require 'elasticsearch/dsl'

class User
def initialize(*args, **kwargs)
puts "Initialized User with args=#{args}, kwargs=#{kwargs}"
end
end

class MyClass
include Elasticsearch::DSL

attr_reader :user

def initialize
@user = "user_string"
end

def build_query_hash
query = search do
query do
user_substr = user[0..5]
match user: user_substr
end
end
query.to_hash
end
end

MyClass.new.build_query_hash
```
yields
```
Initialized User with args=[], kwargs={}
Traceback (most recent call last):
10: from /Users/kevinmcdonough/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `'
9: from /Users/kevinmcdonough/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `load'
8: from /Users/kevinmcdonough/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `'
7: from (irb):59
6: from (irb):55:in `build_query_hash'
5: from /Users/kevinmcdonough/.rvm/gems/ruby-2.6.5/gems/elasticsearch-dsl-0.1.9/lib/elasticsearch/dsl/search.rb:267:in `to_hash'
4: from /Users/kevinmcdonough/.rvm/gems/ruby-2.6.5/gems/elasticsearch-dsl-0.1.9/lib/elasticsearch/dsl/search/query.rb:51:in `to_hash'
3: from /Users/kevinmcdonough/.rvm/gems/ruby-2.6.5/gems/elasticsearch-dsl-0.1.9/lib/elasticsearch/dsl/search/query.rb:42:in `call'
2: from /Users/kevinmcdonough/.rvm/gems/ruby-2.6.5/gems/elasticsearch-dsl-0.1.9/lib/elasticsearch/dsl/search/query.rb:42:in `instance_eval'
1: from (irb):51:in `block (2 levels) in build_query_hash'
NoMethodError (undefined method `[]' for #)
```

EDIT: cleaned up language to be clearer.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.