Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.empty? method added to custom body parser #159

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

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