-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Change Content-Type? #386
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
Change Content-Type? #386
Conversation
Right, we override I'd like a PR for this. We will need to support |
Would this be the spot where that override happens? I don't know a lot about how this part of Rails all fits together, but I'll see about going on a fishing expedition. |
OK. I am confused. That method seems to be the one that does the intercepting... but I have no idea how. Looking around in the Rails code, I couldn't figure out if that method was defined... maybe dynamically? Or if AMS was getting Rails to call it in some way... Confused. |
Sorry if I'm spamming inboxes, here. I'll try to stop commenting so often and say more at once. Here's what I've figured out and I'm writing it down before I take a break:
What I'm concerned about is that Rails will load all it's shit, and AM::Serializers will get loaded as part of this, triggering this line, which currently overrides the default json renderer... and then the application's initializers will get loaded that define custom mime types. This means that if we, say, just added hal support by overriding This implies to me that we might need to either require that users register custom mime types that they want AM::Serializers to intercept with the gem after they've declared the mime type, or that we'll have to shoehorn our way into Neither makes me super happy. I'm going to continue to experiment within the test suite, but would love some guidance. |
Yeah, I'm clearly missing something. Even with some serious hackery, and getting a |
I got some stuff sorted out. Check this compare out: https://github.com/benhamill/active_model_serializers/compare/playing_with_mime_types I'm not sure how to leverage this understanding... Maybe we have a (Side note: If my HAL-based PR gets merged, I think it would make sense to go ahead and register that mime type and add the renderer for it, but that's probably another deal). |
I decided to code up the first idea I had just to see what it would look like and I decided it didn't seem bad, so I wrote it up. Let me know if I should do something else in terms of style or documentation or... whatever. You know... like how Pull Requests normally work. 😓 |
On my side I am changing the Content-Type thanks to Rails built-in content negotiation:
Mime::Type.register "application/vnd.api+json", :jsonapi
respond_to do |format|
format.jsonapi { render json: @my_model }
end I am not sure to understand why we need additional support from AMS ? |
By default, Rails::API removes the middlewares that give you Does that make sense? |
That makes sense, but still, is AMS meant to serialize anything else than JSON ? ActiveModel::Serializer.register_mime_type(Mime::XML) does not make any sense and has to be caught. |
Maybe that's an argument for AMS not to remove |
I want this functionality, but we should build it into 0.10. Let's leave the existing behavior as-is for now. |
If I register a new MIME type with Rails (say, application/hal+json) and I beat on my serializers so that they'll return valid HAL JSON... is there a way to make
ActiveModel::Serializers
report that new MIME type in theContent-Type
header?At first, I was hoping that something like this in the controller would work:
But then Rails::API goes looking for a template, and ignores my
serializer
option.If this isn't something that's currently possible, is this something folks would be interested? If people are, I could see about taking a crack.