ruby-grape / ruby-grape/grape-entity

Expose collection of items of different types without keys

Open
#137 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi.

I've recently tried to expose collection of objects of different types, but it seems that it's not possible with a current version.

Here's the result I'm trying to achieve:

{
  included: [
    {
      id: 1,
      type: 'posts',
      attributes: {
        title: 'Title 1',
        date: '2015-05-01'
      }
    },
    {
      id: 2,
      type: 'posts',
      attributes: {
        title: 'Title 2',
        date: '2015-05-02'
      }
    },
    {
      id: 1,
      type: 'users',
      attributes: {
        first_name: 'John',
        last_name: 'Doe',
        email: 'john.doe@example.com'
      }
    }
  ]
}

And classes:

class Result < Grape::Entity
  expose :included, documentation: { is_array: true } do
    expose :posts, using: Post
    expose :user, using: User
  end
end

class Post < Grape::Entity
  expose :id
  expose :type
  expose :attributes do
    expose :title
    expose :date
  end
end

class User < Grape::Entity
  expose :id
  expose :type
  expose :attributes do
    expose :first_name
    expose :last_name
    expose :email
  end
end

Is there something I am missing?

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 by reproducing the Result, Post, and User exposure example from the issue and compare its output with the requested included array. Determine how heterogeneous collections without keys are intended to be represented, then verify that the resulting serialization contains both posts and users in the shown structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.