diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 79756fd..7a6c202 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -100,7 +100,7 @@ RSpec/DescribeClass: # Offense count: 4 # Configuration parameters: CountAsOne. RSpec/ExampleLength: - Max: 186 + Max: 187 # Offense count: 24 RSpec/LeakyConstantDeclaration: diff --git a/CHANGELOG.md b/CHANGELOG.md index d445e6f..522090d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Your contribution here. * [#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). +* [#77](https://github.com/ruby-grape/grape-swagger-entity/pull/77): Allow proc for enum values in documentation - [@krororo](https://github.com/krororo). #### Fixes diff --git a/lib/grape-swagger/entity/attribute_parser.rb b/lib/grape-swagger/entity/attribute_parser.rb index 220f35e..53259a4 100644 --- a/lib/grape-swagger/entity/attribute_parser.rb +++ b/lib/grape-swagger/entity/attribute_parser.rb @@ -95,9 +95,9 @@ def document_data_type(documentation, data_type) type[:format] = documentation[:format] if documentation.key?(:format) - if (values = documentation[:values]) && values.is_a?(Array) - type[:enum] = values - end + values = documentation[:values] + values = values.call if values.is_a?(Proc) + type[:enum] = values if values.is_a?(Array) type end diff --git a/spec/grape-swagger/entities/response_model_spec.rb b/spec/grape-swagger/entities/response_model_spec.rb index 33770ee..3812aa0 100644 --- a/spec/grape-swagger/entities/response_model_spec.rb +++ b/spec/grape-swagger/entities/response_model_spec.rb @@ -100,6 +100,7 @@ class Values < Grape::Entity expose :guid, documentation: { desc: 'Some values', values: %w[a b c], default: 'c' } expose :uuid, documentation: { desc: 'customer uuid', type: String, format: 'own', example: 'e3008fba-d53d-4bcc-a6ae-adc56dff8020' } + expose :color, documentation: { desc: 'Color', type: String, values: -> { %w[red blue] } } end class Kind < Grape::Entity @@ -231,7 +232,8 @@ def app 'properties' => { 'guid' => { 'type' => 'string', 'enum' => %w[a b c], 'default' => 'c', 'description' => 'Some values' }, 'uuid' => { 'type' => 'string', 'format' => 'own', 'description' => 'customer uuid', - 'example' => 'e3008fba-d53d-4bcc-a6ae-adc56dff8020' } + 'example' => 'e3008fba-d53d-4bcc-a6ae-adc56dff8020' }, + 'color' => { 'type' => 'string', 'enum' => %w[red blue], 'description' => 'Color' } } ) expect(subject['TheseApi_Entities_Kind']).to eql(