Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Metrics/AbcSize:
# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 112
Max: 115

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand Down Expand Up @@ -100,7 +100,7 @@ RSpec/DescribeClass:
# Offense count: 4
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 186
Max: 187

# Offense count: 24
RSpec/LeakyConstantDeclaration:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion lib/grape-swagger/entity/attribute_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ def document_data_type(documentation, data_type)

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

if (values = documentation[:values]) && values.is_a?(Array)
case (values = documentation[:values])
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.

end

type
Expand Down
4 changes: 3 additions & 1 deletion spec/grape-swagger/entities/response_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down