A custom API no longer works with grape 1.3.1 and grape-entity 0.8.0

61 views
Skip to first unread message

Keith Pitty

unread,
Mar 31, 2020, 9:41:41 AM3/31/20
to Grape Framework Discussion

Hi,


I've recently started work on updating our codebase to the current versions of grapegrape-entity and grape_logging.


Whereas one of our APIs works with grape 1.2.4, grape-entity 0.7.1 and grape_logging 1.8.1, it doesn't with the latest versions.


The API in question defines some params that are entities as required. No matter what params are passed to the call, a 400 response code is returned with an error indicating that each required entity param is invalid.


I have been assured that grape 1.3.1 is compatible with grape-entity 0.8.0.


Here is the abbreviated code:


 

class RegistrationAPI < Grape::API
  resource
:registration do
    desc
'Register a new user',
        entity
: Entities::User,
        http_codes
: [[400, 'Bad Request', Entities::Error],
                     
[405, 'Method not allowed', Entities::Error],
                     
[422, 'Unprocessable Entity', Entities::Error]]
   
params do
      requires
:user, type: Entities::UserRegistration
      requires
:questions, type: Entities::Risk::Questions
   
end
    post
'/' do
     
# Never gets this far
   
end
 
end
end

module Entities
 
class UserRegistration < Grape::Entity
    expose
:name, documentation: { required: true, type: 'String', desc: 'The name of the user' }
   
# and so on
 
end
end

module Entities
 
module Risk
   
class Questions < Grape::Entity
      root
'questions'
      expose
:order, documentation: { required: true, type: 'Integer', desc: 'The order' }
      expose
:text, documentation: { required: true, type: 'String', desc: 'The question' }
      expose answers
, using: Risk::Answers, documentation: { required: true, type: 'Entities::Risk::Answers', desc: 'The available answers to the question' }

     
def order
       
object.order.to_i
     
end
   
end
 
end
end

Whatever values are passed in the parameters, the following is returned: 


{"error"=>"user is invalid, questions is invalid"}


Whilst I've started to attempt to debug this problem, I would be grateful if anyone could offer me some suggestions as to what could be causing these errors.


Keith




Dmitriy Nesteryuk

unread,
Mar 31, 2020, 12:58:29 PM3/31/20
to Grape Framework Discussion
Hi Keith,

Our project also use grape-entity, it works fine with 1.3.1 however, we don't use entities as custom types in request params. To be honest, I didn't find documentation about this feature, could you point out, please? I thought entities are only for responses.


вівторок, 31 березня 2020 р. 16:41:41 UTC+3 користувач Keith Pitty написав:

Keith Pitty

unread,
Mar 31, 2020, 7:20:15 PM3/31/20
to Grape Framework Discussion
Hi Dmitry,

Thanks for your reply. You're right, the documentation doesn't describe using grape entities as parameters. The previous custodians of our codebase must have got lucky with using older versions of grape and grape-entity in this way. I will try to rework our code so that it does not use grape entities for parameters.

Thanks again for your help.

Regards,
Keith

Daniel D.

unread,
Mar 31, 2020, 11:10:02 PM3/31/20
to Grape Framework Discussion
I recall we may have intended it to work out of the box at some point, and were happy it did "out of the box". We have entity support in #present spec-ed out in spec/grape/entity_spec.rb and documented. I suggest writing specs for using grape entities as parameters, having those pass in 1.3.1, and treating that as a regression. Open a PR with the (now) failing specs and let's see if we can fix that? 

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-grape/75632cbe-3e8c-46f8-83e1-4b7c78d96dc8%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages