grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

Params not recognized by grape "declared" method

Open pcriv opened this issue 9 years ago • 7 comments

When using entity documentation as params they don't show whe calling then 'declared' method.

namespace :cdrs do
  desc 'Shows all Outbound CDRs.', params: Entities::CdrSearch.documentation
  params do
    optional :fields, type: String
  end

  get :out do
    authorize!
    query_by(Entities::OutboundCdr, permitted_params)
  end
end

permitted_params
=> {"fields"=>"asda"}

def permitted_params
  @permitted_params ||= declared(params, include_missing: false, include_parent_namespaces: true)
end

pcriv avatar May 11 '15 20:05 pcriv

Did you expect Entities::CdrSearch.documentation to carry onto params? This would be a feature request. I do agree that there should be some way of combining probably.

dblock avatar May 12 '15 20:05 dblock

Yes that was the behaviour i expected. That the declared method returned the Entities::CdrSearch.documentation merged with the other params.

pcriv avatar May 12 '15 20:05 pcriv

Also, another thing to note: fields will appear twice in Swagger since its documented both in params hash and params block.

sunnyrjuneja avatar Sep 15 '15 21:09 sunnyrjuneja

Any suggested workaround for this? I don't understand why there is a difference in specifying the params be it hash or block.

dedman avatar Dec 01 '15 19:12 dedman

For anyone else with this issue, here is a quick workaround. Basically it just creates the params block for you from the entity documentation, the params then show up in the declared() method like you'd expect.

params do
   Entity::User.documentation.each do |attribute_name, documentation|
      eval "#{documentation[:required] ? 'requires' : 'optional' } :#{attribute_name}, type: #{documentation[:type]}"
   end
end

dedman avatar Dec 01 '15 23:12 dedman

This is how I was expecting it to work. I'd be happy to submit a pull request.

It would look something like:

params do
   entity Entity::User
end

wefarrell avatar Apr 22 '16 19:04 wefarrell

@wefarrell That would be good!

dblock avatar Apr 24 '16 11:04 dblock