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

[Question] JSONApi disallowes SLASH in namespaced model names #3801

Closed
artych opened this issue Sep 27, 2015 · 11 comments
Closed

[Question] JSONApi disallowes SLASH in namespaced model names #3801

artych opened this issue Sep 27, 2015 · 11 comments

Comments

@artych
Copy link

artych commented Sep 27, 2015

Now JsonApiSerializer preserves slashes: 'docs/ticket' model is serialized as { type: "docs/ticket" }. Related PRs: #3026, #3392, emberjs/ember.js#10980.

Unfortunately, this is not allowed by JSON API, though there are no recommendations which symbol to use, see json-api/json-api#850.

For example rails_api/active_model_serialzer (0.10.rc3) uses underscore, same as for words splitting, so 'Docs::Ticket' and 'DocsTicket' both will be serialized as {type: 'docs_ticket'}. IMHO not comfortable, but bearable.

Question: should SLASH be changed by underscore (or another symbol) in JsonApiSerializer?

@bmac
Copy link
Member

bmac commented Oct 13, 2015

Adding a link to this json-api issue where they are discussing this same problem. json-api/json-api#850

@mattxyzeth
Copy link

Within that thread @ethanresnick mentioned the possibility of defining the namespace in the meta object. I started thinking about this and it really seems like a good place for it. In this situation _extractType in the JSONAPISerializer could look at the meta object and if a namespace is found then just prepend it separated by a SOLIDUS "/".

@mattxyzeth
Copy link

Actually, taking back this recommendation because it feels like a bit much adding this additional layer of logic just to support namespacing when there should be no reason to include a solidus in the type definition.

@ekampp
Copy link

ekampp commented Mar 4, 2016

If this is made a change, I believe it should be opt-in, since some projects are in production, and have solved the dash vs. underscore problem otherwise.

@ivanjolic95
Copy link

I'm working on an app where I use ember + rails and JSONAPI Resources gem to handle communication between backend and frontend.

I've put some models in rails in namespaces, like Itinerary::Item and so on. For each model I've specified a resource like Api::V1::Itinerary::ItemResource. I did the same in ember, I've made models like itinerary/item and everything should be working fine. But it doesn't.

What I get as a response when I try to save/get data is that the resource cannot be found. The type param in JSONAPI object should be api/v1/itinerary/item to make it work but it's itinerary/item.

I suppose one solution is to add namespace api/v1 to my ember models but I could get ugly with using the models in code and pluralization in the JSONAPI adapter (at least I think).

I was wondering if there was a simple way to override the 'type' param model/adapter-wise because handling the namespaces between two programming languages and over JSONAPI is still pretty shaky and undocumented. Thanks!

@fivetanley
Copy link
Member

@ivanjolic95 You can work around this by using http://emberjs.com/api/data/classes/DS.JSONAPISerializer.html#method_modelNameFromPayloadKey for when the request comes in. payloadKeyFromModelName will help you serialize it on the way out if you need that behavior.

@pangratz
Copy link
Member

@fivetanley I was gonna suggest something like that 👍 . There is also a PR #4194 which is located in the modelNameFromPayloadKey space as well. It occurred to me in that PR that the naming for the method is not ideal, well at least not for this use case: it has been originally added to allow overwriting the key which should be used within the hash. But it is already also used within ember-data to normalize/serialize the value as well.

As stated here #4194 (comment) I think there should be a modelNameFromPayloadType / payloadTypeFromModelName (with a better name) which handles the serialization and normalization of the type...

@fernandes
Copy link

my solution is (using JSON API Resource and rails)

getting @ivanjolic95 names as examples:

jsonapi_resources :itinerary_item_resources # create api routes for /api/v1/itinerary_item_resources
class Api::V1::ItineraryItemResource < JSONAPI::Resource
  model_name "Itinerary::ItemResource"
end

we are working on a 6+ months project using this schema and everything is going well, the tips are (our internal guidelines, may help someone):

  1. forget namespaces!
  2. create your API as if you weren't consume it, other people will do
  3. add unique resources names to JSON API

@ivanjolic95
Copy link

@pangratz & @fivetanley it worked on the ember side but JSONAPI resources still couldn't recognize the resource from the type. I guess we'll just need to wait till namespacing is fully specified in JSONAPI.

@fernandes yeah I had to use your approach in the end. Thanks!

@fernandes
Copy link

@ivanjolic95 glad it helped.. 🙇

@pangratz
Copy link
Member

The recently merged ds-payload-hooks feature allows you to customize how the modelName is mapped from the payloads' type and vice versa:

import Serializer from "ember-data/serializers/json-api";

export default Serializer.extend({
  modelNameFromPayloadType(payloadType) {
    // convert "api::v1::my-model" to "my-model"
    return payloadType.replace("api::v1::", "");
  },

  payloadTypeFromModelName(modelName) {
    return `api::v1::${modelName}`:
  }
});

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

No branches or pull requests

8 participants