ruby-grape / ruby-grape/grape

Share desc and type across Grape and GrapeEntity

Open
#1,708 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discuss! feature request you can help
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

What I'm trying to do it to re-use type and description across grape and grape-entity.

In the documentation I read the following:

You can use entity documentation directly in the params block with using: Entity.documentation.

module API
  class Statuses < Grape::API
    version 'v1'

    desc 'Create a status'
    params do
      requires :all, except: [:ip], using: API::Entities::Status.documentation.except(:id)
    end
    post '/status' do
      Status.create! params
    end
  end
end

This allows me to use field description and field type from the documentation defined in the Grape Entity.
Whenever I define an API that requires only 1 field of the ones defined in the Grape Entity, I need to do something like the following (which I find kind of dirty):

given:

module Entities
  class Host < Grape::Entity
    expose :mac_address, documentation: { type: String, desc: "The mac address of the host" }
  end
end

I can do:

params do
  requires :mac_address, type: V1::Entities::Host.documentation[:mac_address][:type], desc: V1::Entities::Host.documentation[:mac_address][:desc]
end

I don't like the above solution mainly for 2 reasons:

  1. I don't like to use the field "type" of an helper that was meant to support documentation generation.
  2. It is cumbersome.

Is there a better way to share type and description across the 2 gems?

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 documented Grape::Entity.documentation entry point and the params block examples, especially expose :mac_address and the repeated type and desc values. Determine the intended shared interface between Grape and GrapeEntity; done should provide a clear, documented way to reuse both attributes without manually indexing documentation metadata.

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.