jsonapi-rails icon indicating copy to clipboard operation
jsonapi-rails copied to clipboard

'source' is missing when model errors are serialized

Open aamir-pk opened this issue 7 years ago • 7 comments

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

aamir-pk avatar Nov 16 '17 04:11 aamir-pk

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).

beauby avatar Nov 16 '17 11:11 beauby

Thanks for your response. I will be thankful if you can show me how to add deserializable_resource in render command.

Thanks

aamir-pk avatar Nov 17 '17 04:11 aamir-pk

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

filipecrosk avatar Jan 22 '18 20:01 filipecrosk

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

JoeWoodward avatar Jan 30 '18 10:01 JoeWoodward

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

filipecrosk avatar Jan 30 '18 18:01 filipecrosk

I had not noticed the railtie specified the mime type. Thanks for pointing that out

JoeWoodward avatar Jan 31 '18 02:01 JoeWoodward

I am also having some trouble with this, some attributes return source and some don't. I am using the deserializable_resource method.

patodevilla avatar Mar 31 '18 07:03 patodevilla