json-schema_builder icon indicating copy to clipboard operation
json-schema_builder copied to clipboard

Extending objects with `null: true` causes a StackLevelTooDeep exception

Open stex opened this issue 6 years ago • 1 comments

Hi again 😅

today I ran into a problem when using the functionality implemented in #8: If I build a schema by extending an existing object instead of nesting everything and assigning null: true, a StackLevelTooDeep exception will be thrown.

As this seems to only happen with null as attribute which is internally translated to an any_of, I would suspect that it has something to do with this.

I will investigate a bit further myself, but maybe you have an idea what exactly is causing this instantly. I find myself having a few problems following the gem's working due to its architecture.


The following is the smallest example I could produce that still causes the error. It will print the stacktrace once its size reaches 3000.

#!/usr/bin/env ruby

require 'json/schema_builder'

trace_func_proc = proc do |event, file, line, id, binding, classname|
  if event == 'call' && caller.length > 3000
    puts caller
    exit(1)
  end
end

set_trace_func(trace_func_proc)

class SchemaStack
  include JSON::SchemaBuilder

  def schema
    obj = object :bar, null: true
    obj.string :baz
    obj
  end
end

SchemaStack.new.schema.as_json

The repeating portion seems to be:

/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/entity.rb:141:in `extract_types'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/object.rb:30:in `extract_types'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/object.rb:35:in `reinitialize'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/attribute.rb:16:in `block in attribute'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/entity.rb:142:in `extract_types'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/entity.rb:46:in `initialize'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/dsl.rb:11:in `new'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/dsl.rb:11:in `entity'
/Users/stex/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/json-schema_builder-0.8.2/lib/json/schema_builder/dsl.rb:45:in `block (2 levels) in register'

Thanks a lot in advance!

stex avatar Feb 26 '19 17:02 stex

Altering the following method in spec/support/examples/builder_initialization.rb will also produce the error, causing the test to fail:

def settings_for(obj)
  settings = obj.object :settings, null: true
  settings.string :email
  settings
end

Edit: I gave up trying to fix it myself... I am clearly not smart enough to fully understand this gem. A few comments and a little less magic would certainly help, but I'm sure someone else will be able to solve this easily.

stex avatar Mar 04 '19 17:03 stex