lib/hanami/config.rb in hanami-2.0.0.rc1 vs lib/hanami/config.rb in hanami-2.0.0

- old
+ new

@@ -272,10 +272,12 @@ actions.finalize! views.finalize! logger.finalize! router.finalize! + use_body_parser_middleware + super end # Configures the app's custom inflections. # @@ -369,10 +371,29 @@ def load_from_env self.slices = ENV["HANAMI_SLICES"]&.split(",")&.map(&:strip) end - # @api private + SUPPORTED_MIDDLEWARE_PARSERS = %i[json].freeze + private_constant :SUPPORTED_MIDDLEWARE_PARSERS + + def use_body_parser_middleware + return unless Hanami.bundled?("hanami-controller") + + return if actions.formats.empty? + return if middleware.stack["/"].map(&:first).any? { |klass| klass == "Hanami::Middleware::BodyParser" } + + parsers = SUPPORTED_MIDDLEWARE_PARSERS & actions.formats.values + return if parsers.empty? + + middleware.use( + :body_parser, + [parsers.to_h { |parser_format| + [parser_format, actions.formats.mime_types_for(parser_format)] + }] + ) + end + def load_dependent_config(gem_name) if Hanami.bundled?(gem_name) yield else require_relative "config/null_config"