-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
Description
Hey there, we started receiving the following warning after a grape update to our application:
json is already registered with class OurSpecificFormatter::Json
This was surprising, because we only registered it once for one of two Grape root APIs that we have in our application:
class Root < ::API::RootAPI # inherits from Grape::API
format :json
error_representer ::Bim::Bcf::API::V2_1::Errors::ErrorRepresenter, "application/json; charset=utf-8"
# ...
end
Our intention was to only use this formatter in that one API, but keep the default formatter in other APIs.
After browsing the grape source code I found two surprises:
- Just by existing, our error formatter now registers itself globally, presumably as the default formatter for JSON errors
- We only intended to use this error formatter in a comparably narrow scope, not everywhere
- The warning message I got was misleading, because it's the automatic registration of our formatter that caused the warning message already and
json
was previously registered withGrape::ErrorFormatter::Json
- The error lies in this line of code, where we should probably refer to
registry[shortname]
instead ofklass
, to say what was already registered
- The error lies in this line of code, where we should probably refer to
What's the intended way to define an error formatter that's only used in a small part of the API, but not the entire API?
References
- PR introducing dynamic registration of formatters: Dynamic registration #2516
- Our BCF API (which only is a small/additional API next to our core API): https://github.com/opf/openproject/blob/dev/modules/bim/app/controllers/bim/bcf/api/root.rb
Metadata
Metadata
Assignees
Labels
No labels