Skip to content

ErrorSerializer not pointing to JSON:API relationship errors appropriately? #1774

Open
@chrisdpeters

Description

@chrisdpeters

Stripped-down model logic:

class ThemeAsset < ActiveRecord::Base
  belongs_to :asset
  validates :asset_id, presence: true
end

And its matching serializer:

class ThemeAssetSerializer < ActiveModel::Serializer
  attributes :id, :path, :created_at, :updated_at
  belongs_to :asset
end

If I submit a blank value for the asset relationship, I get this error payload:

{
  "errors": [
    {
      "source": {
        "pointer": "/data/attributes/asset-id"
      },
      "detail": "can't be blank"
    }
  ]
}

Because of the JSON:API payload that the server must accept, shouldn't the error look more like this?

{
  "errors": [
    {
      "source": {
        "pointer": "/data/relationships/asset/data/id"
      },
      "detail": "can't be blank"
    }
  ]
}

This is the payload that the server expects to receive to further illustrate how the pointer should work:

{
  "data": {
    "type": "theme-assets",
    "attributes": {
      "path": "images/logo.png"
    },
    "relationships": {
      "asset": {
        "data": {
          "type": "assets",
          "id": "f4b297ce-1b88-4773-8dc7-cc546cad5655"
        }
      }
    }
  }
}

This one seems like it would be tricky to implement (would need to involve association reflections and such), but I do think that it would be against the spec until it's resolved.

Environment

ActiveModelSerializers Version (commit ref if not on tag): 0.10.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions