ruby-grape / ruby-grape/grape-entity

Expose default value doesn't work for me

Open
#373 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

Hi,

First of all thanks for the amazing work!
I am using grape entity to serialize ActiveRecord (Rails) objects to a hash, using .respresent(object)

My grape entity looks like this:

class Entities::ProductEntity < Grape::Entity
  expose :platform_id, as: :id
  expose :name
  expose :visible
  expose :main_image
  expose :url
  expose :short_description, default: ''
  expose :brand_id
  expose :category_ids
  expose :relevant_product_ids

  def brand_id
    object.brand&.platform_id if object.respond_to?(:brand)
  end

  def category_ids
    object.categories&.pluck(:platform_id) if object.respond_to?(:categories)
    []
  end

  def relevant_product_ids
    object.relevant_products&.pluck(:platform_id) if object.respond_to?(:relevant_products)
    []
  end
end

The default option on short_description doesn't work, neither did it work if i tried to use it in another entity with default: []

Am i doing something wrong? Or am I on an old version?

I am using https://github.com/ruby-grape/grape-entity#default-value this syntax, and am on grape-entity version 0.10.2

Hope you have some time to help me out, but no worries if not.

Thanks in advance

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the default value documentation linked in the issue and reproduce the behavior using the shown ProductEntity and grape-entity version 0.10.2. Check how short_description and an array default are serialized when the source value is absent; done means the configured defaults appear in the resulting hash or the issue is clarified with the required usage or version behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.