Module: Serega::SeregaPlugins::DepthLimit

Defined in:
lib/serega/plugins/depth_limit/depth_limit.rb

Overview

Plugin :depth_limit

Guards against malicious queries that serialize too much, or against accidentally serializing objects with cyclic relations.

Depth limit is checked when a serialization plan is built, i.e. when #new is called (SomeSerializer.new(with: params[:with])) — including internally by every class-level serialization method. Instantiating the serializer early surfaces depth errors sooner.

When the limit is exceeded, Serega::DepthLimitError is raised; details are available via Serega::DepthLimitError#details.

Limit can be checked or changed with config options:

- config.depth_limit.limit
- config.depth_limit.limit=

There is no default limit — it must be set when enabling the plugin.

Examples:


class AppSerializer < Serega
  plugin :depth_limit, limit: 10 # set limit for all child classes
end

class UserSerializer < AppSerializer
  config.depth_limit.limit = 5 # overrides limit for UserSerializer
end

Defined Under Namespace

Modules: ConfigInstanceMethods, PlanInstanceMethods Classes: DepthLimitConfig

Class Method Summary collapse

Class Method Details

.after_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Adds config options and runs other callbacks after plugin was loaded

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    Plugin options



85
86
87
88
89
90
# File 'lib/serega/plugins/depth_limit/depth_limit.rb', line 85

def self.after_load_plugin(serializer_class, **opts)
  config = serializer_class.config
  limit = opts.fetch(:limit) { raise SeregaError, "Please provide :limit option. Example: `plugin :depth_limit, limit: 10`" }
  config.opts[:depth_limit] = {}
  config.depth_limit.limit = limit
end

.before_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Checks requirements

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    Plugin options



51
52
53
54
55
56
57
58
59
60
# File 'lib/serega/plugins/depth_limit/depth_limit.rb', line 51

def self.before_load_plugin(serializer_class, **opts)
  allowed_keys = %i[limit]
  opts.each_key do |key|
    next if allowed_keys.include?(key)

    raise SeregaError,
      "Plugin #{plugin_name.inspect} does not accept the #{key.inspect} option. Allowed options:\n" \
      "  - :limit [Integer] - Maximum serialization depth."
  end
end

.load_plugin(serializer_class, **_opts) ⇒ void

This method returns an undefined value.

Applies plugin code to specific serializer

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Plugin options



71
72
73
74
# File 'lib/serega/plugins/depth_limit/depth_limit.rb', line 71

def self.load_plugin(serializer_class, **_opts)
  serializer_class::SeregaPlan.include(PlanInstanceMethods)
  serializer_class::SeregaConfig.include(ConfigInstanceMethods)
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



39
40
41
# File 'lib/serega/plugins/depth_limit/depth_limit.rb', line 39

def self.plugin_name
  :depth_limit
end