Skip to content

Usage of block inside of an attribute block nullifies attributes hash #259

@kushniryb

Description

@kushniryb

Hey guys 🖖

Seems like there's a pretty big issue with custom attributes definition that makes this gem a little bit unusable in some scenarios.

I've a following serializer in my application

class ProductSerializer
  include FastJsonapi::ObjectSerializer

  set_type :product

  attributes :name, :description, :price, :currency, :display_price

  attribute :backorderable do |product|
    product.variants_including_master.any?(&:backorderable?)
  end
end

Here's the response I get

{
    "data": {
        "id": "3",
        "type": "product",
        "attributes": null
    }
}

It seems there's no way to use a block inside of an attribute do / end block without nullifying the attributes hash.

I've tried changing any?(&:backorderable) to any? { |variant| variant.backorderable? } but it produces the same result.

Changing block contents to product.variants_including_master.first.backorderable? for the sake of testing works though and returns the appropriate response.

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