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

'source' is missing when model errors are serialized #69

Open
aamir-pk opened this issue Nov 16, 2017 · 7 comments
Open

'source' is missing when model errors are serialized #69

aamir-pk opened this issue Nov 16, 2017 · 7 comments

Comments

@aamir-pk
Copy link

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

@beauby
Copy link
Member

beauby commented Nov 16, 2017

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

@aamir-pk
Copy link
Author

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

Thanks

@filipecrosk
Copy link

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

@JoeWoodward
Copy link

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 #79

@filipecrosk
Copy link

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

@JoeWoodward
Copy link

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

@patodevilla
Copy link

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

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

5 participants