Skip to content

Commit 99e6940

Browse files
authored
Allow Proc for enum values in documentation (#77)
* Allow Proc for enum values in documentation 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 * Set enum only if the return value of Proc is an Array
1 parent 94e3070 commit 99e6940

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ RSpec/DescribeClass:
100100
# Offense count: 4
101101
# Configuration parameters: CountAsOne.
102102
RSpec/ExampleLength:
103-
Max: 186
103+
Max: 187
104104

105105
# Offense count: 24
106106
RSpec/LeakyConstantDeclaration:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Your contribution here.
66
* [#76](https://github.com/ruby-grape/grape-swagger-entity/pull/76): Update ci matrix and gemfile for multi-version grape testing - [@numbata](https://github.com/numbata).
7+
* [#77](https://github.com/ruby-grape/grape-swagger-entity/pull/77): Allow proc for enum values in documentation - [@krororo](https://github.com/krororo).
78

89
#### Fixes
910

lib/grape-swagger/entity/attribute_parser.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def document_data_type(documentation, data_type)
9595

9696
type[:format] = documentation[:format] if documentation.key?(:format)
9797

98-
if (values = documentation[:values]) && values.is_a?(Array)
99-
type[:enum] = values
100-
end
98+
values = documentation[:values]
99+
values = values.call if values.is_a?(Proc)
100+
type[:enum] = values if values.is_a?(Array)
101101

102102
type
103103
end

spec/grape-swagger/entities/response_model_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Values < Grape::Entity
100100
expose :guid, documentation: { desc: 'Some values', values: %w[a b c], default: 'c' }
101101
expose :uuid, documentation: { desc: 'customer uuid', type: String, format: 'own',
102102
example: 'e3008fba-d53d-4bcc-a6ae-adc56dff8020' }
103+
expose :color, documentation: { desc: 'Color', type: String, values: -> { %w[red blue] } }
103104
end
104105

105106
class Kind < Grape::Entity
@@ -231,7 +232,8 @@ def app
231232
'properties' => {
232233
'guid' => { 'type' => 'string', 'enum' => %w[a b c], 'default' => 'c', 'description' => 'Some values' },
233234
'uuid' => { 'type' => 'string', 'format' => 'own', 'description' => 'customer uuid',
234-
'example' => 'e3008fba-d53d-4bcc-a6ae-adc56dff8020' }
235+
'example' => 'e3008fba-d53d-4bcc-a6ae-adc56dff8020' },
236+
'color' => { 'type' => 'string', 'enum' => %w[red blue], 'description' => 'Color' }
235237
}
236238
)
237239
expect(subject['TheseApi_Entities_Kind']).to eql(

0 commit comments

Comments
 (0)