-
Notifications
You must be signed in to change notification settings - Fork 64
Add library configuration object #17
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
Conversation
spec/config_spec.rb
Outdated
end | ||
|
||
context 'when a custom jsonapi parameter parser is used' do | ||
let(:parser) { lambda {} } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the -> { ... } lambda literal syntax for single line lambdas.
@@ -0,0 +1,23 @@ | |||
require 'rails_helper' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing frozen string literal comment.
lib/jsonapi/rails/railtie.rb
Outdated
else | ||
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER | ||
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = JSONAPI::Rails.config.parameter_parser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [116/80]
lib/jsonapi/rails/railtie.rb
Outdated
@@ -23,9 +22,9 @@ class Railtie < ::Rails::Railtie | |||
Mime::Type.register MEDIA_TYPE, :jsonapi | |||
|
|||
if ::Rails::VERSION::MAJOR >= 5 | |||
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER | |||
::ActionDispatch::Request.parameter_parsers[:jsonapi] = JSONAPI::Rails.config.parameter_parser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [106/80]
lib/jsonapi/rails/configuration.rb
Outdated
} | ||
}.freeze | ||
|
||
def self.configure(&block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as configure(*) if you want the method to accept any arguments but don't care about them.
lib/jsonapi/rails/configuration.rb
Outdated
class Configuration < ActiveSupport::InheritableOptions; end | ||
|
||
DEFAULT_CONFIG = { | ||
parameter_parser: lambda { |body| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using {...} for multi-line blocks.
@@ -0,0 +1,21 @@ | |||
module JSONAPI | |||
module Rails |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing top-level module documentation comment.
@@ -0,0 +1,21 @@ | |||
module JSONAPI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing frozen string literal comment.
Thanks for the PR @ezekg! How do you feel about a simple boolean option to prevent registering the params parser? (e.g. |
I actually experimented with that first, along with adding an option for toggling the registration of the jsonapi mime type in case it's being registered another way. But the library depends on both of those being defined a certain way (jsonapi mime type being under Would you rather implement toggles instead of exposing the params parser? |
@ezekg Right, so rails expects a params parser for the Ideally, jsonapi-rails should allow a more fine-grained configuration of what it declares in the initializers (registering params parser, registering renderer), and ensure it does not redefine the |
Okay yeah, that sounds good to me. I'll modify the PR to implement it that way. |
Implements a global configuration object so that we can conditionally register things such as the renderer, parameter parser and mime type.
if ::Rails::VERSION::MAJOR >= 5 | ||
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER | ||
else | ||
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [86/80]
if ::Rails::VERSION::MAJOR >= 5 | ||
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER | ||
else | ||
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [86/80]
lib/jsonapi/rails/configuration.rb
Outdated
register_renderers: true | ||
}.freeze | ||
|
||
def self.configure(&block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as configure(*) if you want the method to accept any arguments but don't care about them.
lib/jsonapi/rails/configuration.rb
Outdated
register_renderers: true | ||
}.freeze | ||
|
||
def self.configure(&block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused method argument - block. If it's necessary, use _ or _block as an argument name to indicate that it won't be used. You can also write as configure(*) if you want the method to accept any arguments but don't care about them.
First pass on the changes we discussed. Do we want to implement any validation to ensure that the mime-type/parser is never redefined? (Or error handling in the cases where they aren't defined?) I'm now able to implement like so, which is great: # config/initializers/jsonapi.rb
# Register JSON API mime-type
Mime::Type.register "application/vnd.api+json", :jsonapi
# Register parameter parser for JSON API mime-type to transform param keys from
# camel case to snake case
ActionDispatch::Request.parameter_parsers[:jsonapi] = -> (raw_post) {
data = ActiveSupport::JSON.decode raw_post
data = { _json: data } unless data.is_a? Hash
data.deep_transform_keys! &:underscore
data.with_indifferent_access
}
# Make sure jsonapi/rails doesn't overwrite our registrations
JSONAPI::Rails.configure do |config|
config.register_parameter_parser = false
config.register_mime_type = false
end |
Safeguards for not overriding already registered mime type/params parser seems like a good idea (we just have to make sure that jsonapi-rb's code is ran after the user-supplied rails initializer) . |
Yeah, so right now it looks like we're hooking into the controller setup, which I believe is after user-supplied initializers are run. Is that correct? If so, we should be safe to do a little bit of validation. |
I don't know, but I'll look into it. Other than that, this PR looks really good, apologies for taking literally months to review it 🙄 |
Haha It's cool. I've been using my fork in prod for a few months and things are good. 👍 |
Are you still wanting to move forward on this, @beauby? |
@ezekg Yes, definitely. I just want to take care of |
spec/config_spec.rb
Outdated
@@ -0,0 +1,39 @@ | |||
require 'rails_helper' | |||
|
|||
RSpec.describe JSONAPI::Rails.config do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Block has too many lines. [29/25]
@@ -0,0 +1,39 @@ | |||
require 'rails_helper' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing magic comment # frozen_string_literal: true.
@@ -0,0 +1,19 @@ | |||
module JSONAPI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing magic comment # frozen_string_literal: true.
The time has finally come 🙏 |
* upstream/master: Conditionally include controller mixin (jsonapi-rb#40) Add initializer generator. (jsonapi-rb#39) Add ActiveSupport::Notifications instrumentation. (jsonapi-rb#41) Add library configuration object (jsonapi-rb#17) Rename JSONAPI::Rails::ActionController to JSONAPI::Rails::Controller. (jsonapi-rb#38) Add hook for default jsonapi object. (jsonapi-rb#37) Add hook for default exposures. (jsonapi-rb#36) Add hook for automatic pagination links. (jsonapi-rb#35) Upgrade to new jsonapi-deserializable version. (jsonapi-rb#34) Prepare for v0.2.1 release
Implemented along with this is a configuration option to define a custom parameter parser for the
:jsonapi
mime-type, something which I think is a needed addition.