Skip to content

Commit

Permalink
Configurable to skip certain paths via skip_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
choznerol committed Nov 20, 2020
1 parent aebb84d commit ff4ea5f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
14 changes: 8 additions & 6 deletions lib/json_key_transformer_middleware/incoming_json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ module JsonKeyTransformerMiddleware
class IncomingJsonFormatter < Middleware

def call(env)
object = Oj.load(env['rack.input'].read)
transformed_object = HashKeyTransformer.send(middleware_config.incoming_strategy, object, middleware_config.incoming_strategy_options)
result = Oj.dump(transformed_object, mode: :compat)
unless should_skip?(env)
object = Oj.load(env['rack.input'].read)
transformed_object = HashKeyTransformer.send(middleware_config.incoming_strategy, object, middleware_config.incoming_strategy_options)
result = Oj.dump(transformed_object, mode: :compat)

env['rack.input'] = StringIO.new(result)
# Rails uses this elsewhere to parse 'rack.input', it must be updated to avoid truncation
env['CONTENT_LENGTH'] = result.length.to_s
env['rack.input'] = StringIO.new(result)
# Rails uses this elsewhere to parse 'rack.input', it must be updated to avoid truncation
env['CONTENT_LENGTH'] = result.length.to_s
end

@app.call(env)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module JsonKeyTransformerMiddleware
class IncomingParamsFormatter < Middleware

def call(env)
parsed_params = Rack::Utils.parse_nested_query(env['QUERY_STRING'])
transformed_params = HashKeyTransformer.send(middleware_config.incoming_strategy, parsed_params, middleware_config.incoming_strategy_options)
env['QUERY_STRING'] = Rack::Utils.build_nested_query(transformed_params)
unless should_skip?(env)
parsed_params = Rack::Utils.parse_nested_query(env['QUERY_STRING'])
transformed_params = HashKeyTransformer.send(middleware_config.incoming_strategy, parsed_params, middleware_config.incoming_strategy_options)
env['QUERY_STRING'] = Rack::Utils.build_nested_query(transformed_params)
end

@app.call(env)
end
Expand Down
14 changes: 14 additions & 0 deletions lib/json_key_transformer_middleware/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@ def initialize(app, middleware_config)
@middleware_config = middleware_config
end

protected

def should_skip?(env)
middleware_config.skip_paths.any? do |skip_path|
case skip_path
when String
skip_path == env['PATH_INFO']
when Regexp
skip_path.match? env['PATH_INFO']
end
end
end

private

attr_reader :app, :middleware_config

end

end
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class OutgoingJsonFormatter < Middleware
def call(env)
status, headers, body = @app.call(env)

return [status, headers, body] if should_skip?(env)

new_body = build_new_body(body)

[status, headers, new_body]
Expand Down
1 change: 1 addition & 0 deletions lib/json_key_transformer_middleware/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Railtie < Rails::Railtie
config.json_key_transformer_middleware.incoming_strategy_options = ActiveSupport::OrderedOptions.new
config.json_key_transformer_middleware.outgoing_strategy = :transform_underscore_to_camel
config.json_key_transformer_middleware.outgoing_strategy_options = ActiveSupport::OrderedOptions.new
config.json_key_transformer_middleware.skip_paths = []

config.app_middleware.insert_after(Rails::Rack::Logger, JsonKeyTransformerMiddleware::IncomingParamsFormatter, config.json_key_transformer_middleware)
config.app_middleware.insert_after(Rails::Rack::Logger, JsonKeyTransformerMiddleware::IncomingJsonFormatter, config.json_key_transformer_middleware)
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The Railtie provides these configuration options:
* `incoming_strategy_options` - no options set by default.
* `outgoing_strategy` - default value of `:transform_underscore_to_camel`.
* `outgoing_strategy_options` - no options set by default.
* `skip_paths` - skip the transformation if HTTP request path matches. e.g. `[/^\/admin/, '/graphql']`

Here is an example Rails initializer which turns on the `outgoing_strategy_options.keep_lead_underscore` option:

Expand Down

0 comments on commit ff4ea5f

Please sign in to comment.