Skip to content

Share desc and type across Grape and GrapeEntity #1708

Open
@pinkynrg

Description

@pinkynrg

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions