-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Comments
Adding a link to this json-api issue where they are discussing this same problem. json-api/json-api#850 |
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 |
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. |
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. |
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 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 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! |
@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. |
@fivetanley I was gonna suggest something like that 👍 . There is also a PR #4194 which is located in the As stated here #4194 (comment) I think there should be a |
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):
|
@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! |
@ivanjolic95 glad it helped.. 🙇 |
The recently merged 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}`:
}
}); |
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)
usesunderscore
, 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?
The text was updated successfully, but these errors were encountered: