jsonapi-rails
jsonapi-rails copied to clipboard
'source' is missing when model errors are serialized
Hi,
When my model is invalid I render the response (from controller) like this:
render jsonapi_errors: activity.errors, status: :unprocessable_entity
It serializes the model error as following:
{
"errors":
[
{
"title": "Invalid occurred_at",
"detail": "Occurred at Future time is not allowed",
"source":
{
}
},
{
"title": "Invalid code",
"detail": "Code This status is in conflict with the previous one",
"source":
{
}
}
],
"jsonapi":
{
"version": "1.0"
}
}
It has two issues: 1) 'source' value is missing, 2) The detail of the message shouldn't contain the source attribute. For example instead of 'Occurred at Future time is not allowed' it should be 'Future time is not allowed'.
Thanks
Thanks for bringing this up.
Regarding source
, it will only work out of the box if you're using deserializable_resource
to deserialize the input payload. There's room for improvement there, but that's the current status.
Regarding the detail
, I agree, and I'd merge a PR fixing this behavior (it happens here).
Thanks for your response. I will be thankful if you can show me how to add deserializable_resource
in render
command.
Thanks
I'm trying to use it with deserializable_resource
but the source is not been populated out of the box. I had to add the method jsonapi_pointers
.
For example:
class UserController < ApplicationController
deserializable_resource :user, only: [:create, :update]
def create
user = User.new(user_params)
if user.save
render jsonapi: user
else
render jsonapi_errors: user.errors, status: :unprocessable_entity
end
end
def jsonapi_pointers
{
name: '/data/attributes/name',
email: '/data/attributes/email'
}
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
@aamir-pk by the way, here's how to use the deserializable_resource
in a controller
You don't need to use jsonapi_pointers
. Are you using the correct mime type and have it registered?
FYI deserializable_resource :symbol
will make the deserialized params available as params[:symbol]
I added a comment to this issue which may help https://github.com/jsonapi-rb/jsonapi-rails/issues/79
Thanks @JoeWoodward but unfortunately it didn't fix for me.
I tried to register the mime type as you mentioned on the #79 issue but I don't think it was the case for me. Also, it looks like it is already registered here https://github.com/jsonapi-rb/jsonapi-rails/blob/master/lib/jsonapi/rails/railtie.rb#L35
I'm using Ember data and my frontend is always sending the correct content type: Content-Type:application/vnd.api+json
I had not noticed the railtie specified the mime type. Thanks for pointing that out
I am also having some trouble with this, some attributes return source and some don't. I am using the deserializable_resource method.