-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix unknown validator exception when using requires/optional with Entity #2338
Fix unknown validator exception when using requires/optional with Entity #2338
Conversation
when that entity specifies keywords that grape interprets as custom validators, ie: is_array, param_type, etc. This PR changes it so that it skips looking for a custom validator for those special documentation keywords. This allows you to do something like: ```rb desc 'Create some entity', entity: SomeEntity, params: SomeEntity.documentation params do requires :none, except: %i[field1 field2], using: SomeEntity.documentation end post do ... end ``` and SomeEntity can specify documentation like: ```rb class SomeEntity < Grape::Entity expose :id, documentation: { type: Integer, format: 'int64', desc: 'ID' } expose :name, documentation: { type: String, desc: 'Name', require: true, param_type: 'body' } expose :array_field, documentation: { type: String, desc: 'Array Field', require: true, param_type: 'bold' } end ``` and it won't crash on startup and give you correct documentation and param validation to boot. Open questions: 1. Is this an exhaustive list of keywords? 2. Is there a better way to maintain/get the list of keywords? 3. Is there a better approach altogeher?
Start by writing a test for this? Let's find a better way to implement it, too. |
Sure, but I was hoping for some guidance there since I don't know the codebase as well as others.
Will do, but I don't think it makes sense to until we have an acceptable approach |
I tend to do TDD for things like this, but in either case I don't know what the right fix is. Hardcoding the list that Grape knows nothing about is probably not. I've rarely used this feature too, but it looks like |
There in lies the problem, how do you write a test for this, when the issue is in the integration point between the multiple repos? Seems like the fix needs to be in grape, but proper testing of it would be done in grape-swagger-entity? I don't know, tbh, I'm confused why grape is even split into so many repos. Seems like it would be a much more robust offering if all combined into one gem with solid integration points. Documentation would be much better too since you wouldn't have to constantly jump between 4 different READMEs to figure out what combination of things you need. But I admit my naivety here, as I really don't know anything about the project's history and thus context for various design decisions. |
You can add an integration test that uses a thirdparty gem here: https://github.com/ruby-grape/grape/tree/master/spec/integration
Cause Grape doesn't want to be Rails! :) but open to more specific suggestions and of course contributions |
I've made it a tiny bit more explicit as to what's going on in this code, ie.
I've modified the existing tests around |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a test that exercises every one of these options? Maybe something like
ParamsScope:: RESERVED_DOCUMENTATION_KEYWORDS.each do |keywords|
context keyword do
it ..
# There are a number of documentation options on entities that don't have | ||
# corresponding validators. Since there is nowhere that enumerates them all, | ||
# we maintain a list of them here and skip looking up validators for them. | ||
reserved_keywords = %i[as required param_type is_array format example] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this a constant RESERVED_KEYWORDS
. Maybe we should also rename it? These are documentation options so RESERVED_DOCUMENTATION_KEYWORDS
? WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me!
…pe-swagger-entity-keywords # Conflicts: # .rubocop_todo.yml
I see where you're coming from, but my counterpoint is that each keyword is a different type, ie |
Thanks for helping with this @mscrivo, merged. |
when that entity specifies keywords that grape interprets as custom validators, ie:
is_array
,param_type
, etc.There have been a couple issues opened about this in the past with no real resolution: #1527 and #1302
This PR changes it so that it skips looking for a custom validator for those special documentation keywords.
This allows you to do something like:
and SomeEntity can specify documentation like:
and it won't crash on startup and give you correct documentation and param validation to boot.
Open questions: