-
Notifications
You must be signed in to change notification settings - Fork 41
Allow Proc for enum values in documentation #77
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
Conversation
Enables the use of a Proc for the `values` option within Grape::Entity documentation. This allows for dynamic generation of enum values based on runtime logic when generating Swagger specifications. For example, this is useful when you want to specify a model's enum for the `values` option and need to lazy-load the model, like this: ```ruby expose :some, documentation: { values: -> { Model.some_enum.keys } } ``` This provides consistency, as Proc is also allowed for the `values` option in request parameters: https://github.com/ruby-grape/grape-swagger/blob/3e842a138b0f5edd2538d9da4aa23276e63e5d5c/lib/grape-swagger/doc_methods/parse_params.rb#L179-L180
474d169
to
c5c122f
Compare
@numbata Want to help review this? |
when Array | ||
type[:enum] = values | ||
when Proc | ||
type[:enum] = values.call |
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.
Should we have an extra check that the `.call' returns an array? To avoid any surprises in the schema. 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.
@numbata Thanks for the review.
I've fixed it to check if the return value of Proc is an Array.
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.
It’s simple and clear in my opinion. @krororo, thanks for your contribution!
Cut a release. If anyone wants to do more work this gem could use some active maintainers. There are some issues that look interesting. |
Enables the use of a Proc for the
values
option within Grape::Entity documentation. This allows for dynamic generation of enum values based on runtime logic when generating Swagger specifications.For example, this is useful when you want to specify a model's enum for the
values
option and need to lazy-load the model, like this:This provides consistency, as Proc is also allowed for the
values
option in request parameters:https://github.com/ruby-grape/grape-swagger/blob/3e842a138b0f5edd2538d9da4aa23276e63e5d5c/lib/grape-swagger/doc_methods/parse_params.rb#L179-L180