Skip to content

Conversation

@foxweb
Copy link

@foxweb foxweb commented Nov 29, 2022

I wanted to add my own JSON body parser, like Oj.
I looked and did as described in the documentation, but got an error in the RSpec tests.
I saw that the .empty? method is missing somewhere and added it. But I don't understand where it is being called from.

NoMethodError:
       undefined method `empty?' for #<OjParser:0x0000000101363df8>

                 return DEFAULT_BODY_PARSERS if parser_specs.empty?
                                                            ^^^^^^^

The working version of the code looks like this:
But I'm not sure if this fix is in the right place.

# config/app.rb

config.middleware.use :body_parser, OjParser.new

# lib/oj_parser.rb

class OjParser
  def mime_types
    ['application/json']
  end

  def parse(body)
    Oj.load(body) unless body.empty?
  end

  def empty?
    false
  end
end

@foxweb
Copy link
Author

foxweb commented Nov 30, 2022

Correct code:

config.middleware.use :body_parser, [:json, OjParser.new]

@foxweb
Copy link
Author

foxweb commented Jan 9, 2023

2f76f75888a0b9ffc426fa2227cb08a2

@parndt parndt requested a review from timriley October 19, 2025 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant