Skip to content

Conversation

krororo
Copy link
Contributor

@krororo krororo commented Apr 19, 2025

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:

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

@dblock
Copy link
Member

dblock commented Apr 22, 2025

@kzaitsev @mscrivo ya'll around? Maybe time for more maintainers?

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
@krororo krororo force-pushed the allow-proc-for-values branch from 474d169 to c5c122f Compare April 23, 2025 00:28
@dblock
Copy link
Member

dblock commented Apr 23, 2025

@numbata Want to help review this?

when Array
type[:enum] = values
when Proc
type[:enum] = values.call
Copy link
Collaborator

@numbata numbata Apr 23, 2025

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

@numbata numbata left a 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!

@dblock dblock merged commit 99e6940 into ruby-grape:master Apr 24, 2025
20 checks passed
@dblock
Copy link
Member

dblock commented Apr 24, 2025

Merged, thanks @numbata and @krororo! I'll cut a release sometime, add your +1 to #78.

@dblock
Copy link
Member

dblock commented Apr 26, 2025

Cut a release. If anyone wants to do more work this gem could use some active maintainers. There are some issues that look interesting.

@krororo krororo deleted the allow-proc-for-values branch April 26, 2025 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants