-
Notifications
You must be signed in to change notification settings - Fork 529
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
How to override type? #618
Comments
Thanks. I tried that and it didn't work. Here's the setup: project table I need the type in the json responses for projects to be 'study'. # still returns type as 'project'
class Api::V2::ProjectResource < JSONAPI::Resource
model_name 'Study', add_model_hint: false
end |
I was able to make it work by changing resource and controller name to the public API name, and using model name to point the public-named resource to the correct backend model. Not sure if this is a good solution since it's not conventional Rails where the controller name be the same as the model name. |
In JR the convention is for the controller to match the resource name. The resource acts as an intermediate layer mapping to the model so the naming disparity is expected. |
Alright cool. Thanks @thibaudgg ! |
@bpourriahi not sure why, but you're welcome anyway! 😄 |
Heh. That may or may not been a 'typo'. Thank you @lgebhardt. |
@lgebhardt I'm using namespaced STI and I think JR type convention doesn't match this use case. As explained in http://jsonapi.org/format/#document-resource-objects:
I'm working on an infrastructure management app that includes a lot of models that are namespaced to avoid any collision. For example, my model IMHO, JR type should actually match the URL namespace, which in my case is This is a little different with namespaced STI like my My first thought was to rename my models like This would be fine except that the That means that the type should actually not match the controller name, but the model name, given the resource attributes could vary in the same controller. In that case, I wouldn't have to rename my models as I don't hink JR should impose a model naming convention. In any case, the type field should be customizable, even if convention is always better. |
Is this something which is being considered? I've tried the workarounds above and had no luck. We are in a similar situation where we have multiple services which might share resource names (e.g. model_name "ExampleServiceName::AssetFile"
def type
"example_service_name/asset_files"
end Even better would be if we can define the prefix globally across each app by defining this in a base class, or ideally have JR pick the namespacing up from the namespaced |
I'm also trying to figure this out. I have STI models Namespaced type is important to me since my frontend Ember app has a similar folder structure with |
Did this by using jsonapi-utils by explicitly defining the actions in the controllers so you know exactly whats going on. # Specify a particular HTTP status code
jsonapi_render json: new_user, status: :created
# Forcing a different resource
jsonapi_render json: User.all, options: { resource: V2::UserResource }
# Using a specific count
jsonapi_render json: User.some_weird_scope, options: { count: User.some_weird_scope_count }
# Hash rendering
jsonapi_render json: { data: { id: 1, first_name: 'Tiago' } }, options: { model: User }
# Collection of Hashes rendering
jsonapi_render json: { data: [{ id: 1, first_name: 'Tiago' }, { id: 2, first_name: 'Doug' }] }, options: { model: User } |
2022 and still the same problem: I have versioned models (Api::V1::ModelName, Api::V2::ModelName) and both get typed as 'model-name'. So, if the server gets such a serialiazed model instance and wants to deserialize it, how is it supposed to know the correct model version? I have tracked this issue to the following code section (in 0.10.7):
So this is where the class name gets demodulized and then, in
the type gets set to the value of
With this, the
Now yields:
Et voilà! Again, this is not really configurable and just as hard coded as the previous solution. However, at the moment it suits my needs. In order to make this portable to other machines, I have forked the master branch, patched the code with a commit and am now installing directly from there. Finally: Thanks to @lgebhardt for this great piece of software! |
We have a model in the backend that we refer to as a different name in our public API. We need to override the type returned but haven't found a way to do this yet.
I tried adding def self._type (as I noticed that's where it was grabbing type from) to my base model class as well as the resource serializer but that didn't change the response.
Any help would be appreciated.
The text was updated successfully, but these errors were encountered: