Skip to content

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

Open

Description

@aamir-pk

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

Activity

  1. beauby commented on Nov 16, 2017

    @beauby
    Member

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

  2. aamir-pk commented on Nov 17, 2017

    @aamir-pk
    Author

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

    Thanks

  3. filipecrosk commented on Jan 22, 2018

    @filipecrosk

    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

  4. JoeWoodward commented on Jan 30, 2018

    @JoeWoodward

    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

  5. filipecrosk commented on Jan 30, 2018

    @filipecrosk

    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

  6. JoeWoodward commented on Jan 31, 2018

    @JoeWoodward

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

  7. patodevilla commented on Mar 31, 2018

    @patodevilla

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions