ruby-grape / ruby-grape/grape-entity

Subclasses keep the parent's .documentation?

Open
#121 4 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

I'm not sure if I'm using it wrong, or if it's bugged, but if I do this:

# api/entities/my_entity.rb
module API
  module Entities
    class MyEntity < Grape::Entity
      expose :name, documentation: { type: String, required: true }
      expose :message, documentation: { type: String }

      expose :created_at, documentation: { type: DateTime, required: true }
      expose :updated_at, documentation: { type: DateTime, required: true }

      expose :relation, using: API::Entities::Other, documentation: { is_array: true }

      def self.entity_name() "MyEntity" end

      class Update < API::Entities::MyEntity
        unexpose :created_at
        unexpose :updated_at
        unexpose :relation
      end
    end
  end
end

# api/entities/other.rb
module API
  module Entities
    class Other < Grape::Entity
      expose :created_at, documentation: { type: DateTime, required: true }
      expose :updated_at, documentation: { type: DateTime, required: true }
    end
  end
end

The results aren't quite what you'd expect in regards to .documentation:

$ rails console
Loading development environment (Rails 4.2.1)
irb(main):001:0> API::Entities::MyEntity.documentation
=> {:name=>{:type=>String, :required=>true}, :message=>{:type=>String}, :created_at=>{:type=>DateTime, :required=>true}, :updated_at=>{:type=>DateTime, :required=>true}, :relation=>{:is_array=>true}}
irb(main):002:0> API::Entities::MyEntity::Update.documentation
=> {:name=>{:type=>String, :required=>true}, :message=>{:type=>String}, :created_at=>{:type=>DateTime, :required=>true}, :updated_at=>{:type=>DateTime, :required=>true}, :relation=>{:is_array=>true}}

As a result, it currently appears to be impossible to use Entities for parameter reuse, without resorting to copypasting expose … lines into a separate class - making unexpose rather useless.

As a side note, it doesn't appear to be unexpose itself that's broken, as expose in a subclass similarly has no effect.

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

Reproduce the inheritance case from the issue in a local Grape::Entity setup, then inspect how expose, unexpose, and .documentation behave for subclasses. Confirm the behavior with focused tests covering both removing inherited exposures and adding subclass exposures; done means the subclass documentation matches its effective exposures.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.