Skip to content

Commit b838b49

Browse files
authored
Merge pull request ruby-grape#12 from onemanstartup/enum_as_proc
Enum as proc
2 parents 433a020 + 1354841 commit b838b49

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/grape-swagger/representable/parser.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ def parse_representer_property(property)
4141
memo[:type] = data_type
4242
end
4343

44+
default_value = documentation[:default] || property[:default] || nil
45+
memo[:default] = default_value unless default_value.nil?
46+
4447
values = documentation[:values] || property[:values] || nil
45-
memo[:enum] = values if values.is_a?(Array)
48+
memo[:enum] = values.is_a?(Proc) ? values.call : values if values
4649

4750
example = documentation[:example] || property[:example] || nil
4851
memo[:example] = example.is_a?(Proc) ? example.call : example if example

spec/grape-swagger/representers/response_inline_representer_spec.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Tag < Representable::Decorator
2121
class Error < Representable::Decorator
2222
include Representable::JSON
2323

24-
property :code, documentation: { type: 'string', desc: 'Error code' }
24+
property :code, default: 403, documentation: { type: 'string', desc: 'Error code' }
2525
property :message, documentation: { type: 'string', desc: 'Error message' }
2626
end
2727

@@ -39,7 +39,7 @@ class Something < Representable::Decorator
3939
property :id, documentation: { required: true }
4040
end
4141
collection :tags, decorator: ThisInlineApi::Representers::Tag, documentation: { desc: 'Tags.' } do
42-
property :color, documentation: { type: String, desc: 'Tag color.' }
42+
property :color, documentation: { type: String, desc: 'Tag color.', values: -> { %w[red blue green] }, default: 'red' }
4343
end
4444
end
4545
end
@@ -116,7 +116,7 @@ def app
116116
'type' => 'object',
117117
'description' => 'This returns something',
118118
'properties' => {
119-
'code' => { 'type' => 'string', 'description' => 'Error code' },
119+
'code' => { 'type' => 'string', 'description' => 'Error code', 'default' => 403 },
120120
'message' => { 'type' => 'string', 'description' => 'Error message' }
121121
}
122122
)
@@ -157,7 +157,12 @@ def app
157157
'type' => 'object',
158158
'properties' => {
159159
'name' => { 'description' => 'Name', 'type' => 'string', 'example' => 'A tag' },
160-
'color' => { 'description' => 'Tag color.', 'type' => 'string' }
160+
'color' => {
161+
'description' => 'Tag color.',
162+
'type' => 'string',
163+
'enum' => %w[red blue green],
164+
'default' => 'red'
165+
}
161166
}
162167
},
163168
'description' => 'Tags.'

0 commit comments

Comments
 (0)