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

The JSON API media type should only work with a JSON API handler #23712

Conversation

bf4
Copy link
Contributor

@bf4 bf4 commented Feb 16, 2016

Revises behavior introduced in #20329 #21251

Since the media type 'application/vnd.api+json' is a spec,
it is inappropriate to handle it with the JSON renderer.

Updated Description per discussion

Let's remove it and introduce back only when we have defined the proper API for the handlers.

This PR removes support for the JSON API media type.

We recommend the media type be registered on its own as jsonapi when a jsonapi Renderer and deserializer (Http::Parameters::DEFAULT_PARSERS) are added.

Original Description

This PR adds support for a JSON API media type.

  • It does not currently include a JSON API renderer, but I could add that
    here.
  • The JSON being parsed in the tests is invalid JSON API, but I
    retained the example for test continuity. I'd like to change that as
    well.

I've chosen to register JSON API as api_json per
JSONAPI-resources,
one of the more mature Rails+JSON API gems out there. An alternate name
could be jsonapi. In both cases, I would recommend the renderer be
named after the registered mime type.

Is related to work in #21496

cc @dgeb

@rails-bot
Copy link

r? @senny

(@rails-bot has picked a reviewer for you, use r? to override)

@dgeb
Copy link
Contributor

dgeb commented Feb 16, 2016

Thanks for this, @bf4. It's a step in the right direction.

Since the media type 'application/vnd.api+json' is a spec, it is inappropriate to handle it with the JSON renderer.

I agree. To go further, I don't think the mime type should be registered at all without an appropriate corresponding renderer.

I've chosen to register JSON API as api_json per JSONAPI-resources, one of the more mature Rails+JSON API gems out there. An alternate name could be jsonapi.

We are actually looking to switch to jsonapi in our gem. The current name is based directly on the media type and feels a bit clunky as an alias.

I've been talking with @lgebhardt about proposing a more pluggable rendering solution for mime types, that could cover the base mime types as well as custom registrations. We'll review with @bf4 and others here soon.

@lgebhardt
Copy link

If we're looking for pluggable renderers it might be best to remove the json-api mime type and DEFAULT_PARSERS entries. These could be registered as part of the specific library supporting json-api.

@bf4
Copy link
Contributor Author

bf4 commented Feb 16, 2016

Perhaps the pr should remove the jsonapi code and maybe update the guides with how to add to custom json media type

B mobile phone

On Feb 16, 2016, at 2:00 PM, Larry Gebhardt notifications@github.com wrote:

If we're looking for pluggable renderers it might be best to remove the json-api mime type and DEFAULT_PARSERS entries. These could be registered as part of the specific library supporting json-api.


Reply to this email directly or view it on GitHub.

@rafaelfranca
Copy link
Member

Perhaps the pr should remove the jsonapi code and maybe update the guides with how to add to custom json media type

👍. Let's remove it and introduce back only when we have defined the proper API for the handlers.

@dgeb
Copy link
Contributor

dgeb commented Feb 17, 2016

Let's remove it and introduce back only when we have defined the proper API for the handlers.

sounds good to me 👍

Since the media type 'application/vnd.api+json' is a spec,
it is inappropriate to handle it with the JSON renderer.

This PR removes support for a JSON API media type.

I would recommend the media type be registered on its own as `jsonapi`
when a jsonapi Renderer and deserializer (Http::Parameters::DEFAULT_PARSERS) are added.

Is related to work in rails#21496
@bf4 bf4 force-pushed the incorrect_to_accept_json_api_and_not_render_spec branch from 6f73b19 to c4d90b7 Compare February 17, 2016 03:44
assert_parses(
{"person" => {"name" => "David"}},
{},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this test around both in case anyone is looking for json api support, and because I didn't see the behavior tested elsewhere.

However, I removed the test for rootless JSON as it duplicates what I left here and also isn't relevant to JSON API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Make sense 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha, that was quick! you merged it right before I revised the commit message from 'I' to 'we' and fixed a typo. :) Thanks!

rafaelfranca added a commit that referenced this pull request Feb 17, 2016
…ot_render_spec

The JSON API media type should only work wih a JSON API handler
@rafaelfranca rafaelfranca merged commit 2572584 into rails:master Feb 17, 2016
@@ -37,9 +37,9 @@ def teardown
)
end

test "parses json params for application/vnd.api+json" do
test "does not parses unregistered media types such as application/vnd.api+json" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca typo does not parses should be does not parse. Just leave it for someone else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change it quickly, but if you want to make a PR go ahead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's cleaner if you just fix it, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@ianks
Copy link
Contributor

ianks commented Feb 17, 2016

@bf4 Can we add some documentation about how to register the application/vnd.api+json, last time I tried in Rails 5 the MIME type registration did not work as it did in previous versions of Rails.

@bf4
Copy link
Contributor Author

bf4 commented Feb 17, 2016

@ianks I spiked a first step in 6f73b19#diff-6a80fa5836403509e86a81c2117af391R12 or did you mean in the guides?

@ianks
Copy link
Contributor

ianks commented Feb 17, 2016

Nevermind my comment :)

@rthbound
Copy link
Contributor

I've been developing on a greenfield API project using edge, and have found that the commit in this PR has broken application/vnd.api+json params parsing. Bisecting using 5.0.0.beta2 as good and the head of master as bad, confirmed c4d90b7 was the breaking change.

initializers:

# active_model_initializer.rb
ActiveModelSerializers.config.adapter = :json_api

# wrap_parameters.rb
ActiveSupport.on_load(:action_controller) do
  wrap_parameters format: [:json, :api_json]
end
Processing by DashboardsController#update as */*
  Parameters: {"{\"data\":{\"id\":\"1\",\"attributes\": ... ... ... ... ...

I'm not sure (@bf4, @rafaelfranca), should this be broken right now?... is it pending some development, or should I submit an app or tests for reproduction for the failure to build a good parameters object?

rthbound added a commit to rthbound/ams-json-api-broken-23712 that referenced this pull request Feb 18, 2016
@rthbound
Copy link
Contributor

I went ahead and generated an app for demoing the problem: Repo is here

/cc @bf4, @rafaelfranca

@rafaelfranca
Copy link
Member

Yes it is a "breaking change" by design. That feature was never released officially do we just removed it. If you need it in your application you can register the mime type.

@rthbound
Copy link
Contributor

@rafaelfranca I attempted that last night to no avail. Confirmed in the demo app that it still does not work, even having registered the mime type: link to change

@rafaelfranca
Copy link
Member

As you can see in c4d90b7 the only thing made was to remove application/vnd.api+json from the registered mime type.

@rthbound
Copy link
Contributor

@rafaelfranca something must be awry... On rails master, the following does not work as expected:

# custom_mime_type_initializer.rb
Mime::Type.unregister :json
Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest application/vnd.api+json )

# Confirmed successful re-registration of :json mime type:
Mime::Type.lookup_by_extension("json")
=> #<Mime::Type:0x000000033173f8 @hash=4269350542389124482, @string="application/json", @symbol=:json, @synonyms=["text/x-json", "application/jsonrequest", "application/vnd.api+json"]>

Reverting c4d90b7 resolves the issue (assuming I also remove the above initializer). I appreciate your time, and I'll continue looking into this. Thanks again @rafaelfranca

@rafaelfranca
Copy link
Member

Could you open a new issue with a way to reproduce it?

@bf4
Copy link
Contributor Author

bf4 commented Feb 23, 2016

@rthbound would you mind opening an issue in ams, as well, so we can add a test there? Also to confirm docs are up to date.

@rthbound
Copy link
Contributor

@bf4 : Here it is, #1534

@bf4 bf4 changed the title The JSON API media type should only work wih a JSON API handler The JSON API media type should only work with a JSON API handler Mar 4, 2016
bf4 added a commit to bf4/rails that referenced this pull request May 23, 2016
The tests introduced in
https://github.com/rails/rails/pull/23816/files#diff-384a5a15d8d53de799fb6541688ea5f9R153
register the JSON API media type `application/vnd.api+json` with
`Mime[:json]`. The JSON API media type should not be registered
with `Mime[:json]`, as discussed in rails#23712.  Moreover,
since the actual mime type used in the test is
incidental, I've changed this to a valid, but fictional
`applcation/vnd.rails+json`.

These tests were causing failures in
rails#25050 (comment) where
`Mime[:jsonapi]` is being added, so that JSON API request params are parsed
with the JSONAPI gem.
bf4 referenced this pull request Aug 31, 2017
We will wait until 5.1 to make a decision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants