Skip to content

Commit 9e2a3d7

Browse files
committed
Add support for property examples
cf. ruby-grape/grape-swagger-entity#28
1 parent d241bee commit 9e2a3d7

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

lib/grape-swagger/representable/parser.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def parse_representer_property(property)
4242
values = documentation[:values] || property[:values] || nil
4343
memo[:enum] = values if values.is_a?(Array)
4444

45+
example = documentation[:example] || property[:example] || nil
46+
memo[:example] = example.is_a?(Proc) ? example.call : example if example
47+
4548
if is_a_collection || documentation[:is_array]
4649
memo = {
4750
type: :array,

spec/grape-swagger/representers/response_inline_representer_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ module Representers
77
class Kind < Representable::Decorator
88
include Representable::JSON
99

10-
property :id, documentation: { type: Integer, desc: 'Title of the kind.' }
10+
property :id, documentation: { type: Integer, desc: 'Title of the kind.', example: 123 }
1111
end
1212

1313
class Tag < Representable::Decorator
1414
include Representable::JSON
1515

16-
property :name, documentation: { type: 'string', desc: 'Name' }
16+
property :name, documentation: { type: 'string', desc: 'Name', example: -> { 'A tag' } }
1717
end
1818

1919
class Error < Representable::Decorator
@@ -125,7 +125,7 @@ def app
125125
'kind2' => {
126126
'type' => 'object',
127127
'properties' => {
128-
'id' => { 'description' => 'Title of the kind.', 'type' => 'integer', 'format' => 'int32' },
128+
'id' => { 'description' => 'Title of the kind.', 'type' => 'integer', 'format' => 'int32', 'example' => 123 },
129129
'name' => { 'description' => 'Kind name.', 'type' => 'string' }
130130
},
131131
'description' => 'Secondary kind.'
@@ -136,7 +136,7 @@ def app
136136
'items' => {
137137
'type' => 'object',
138138
'properties' => {
139-
'name' => { 'description' => 'Name', 'type' => 'string' },
139+
'name' => { 'description' => 'Name', 'type' => 'string', 'example' => 'A tag' },
140140
'color' => { 'description' => 'Tag color.', 'type' => 'string' }
141141
}
142142
},
@@ -147,7 +147,7 @@ def app
147147

148148
expect(subject['definitions'].keys).to include 'Kind'
149149
expect(subject['definitions']['Kind']).to eq(
150-
'type' => 'object', 'properties' => { 'id' => { 'description' => 'Title of the kind.', 'type' => 'integer', 'format' => 'int32' } }
150+
'type' => 'object', 'properties' => { 'id' => { 'description' => 'Title of the kind.', 'type' => 'integer', 'format' => 'int32', 'example' => 123 } }
151151
)
152152
end
153153
end

spec/grape-swagger/representers/response_representer_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ module Representers
77
class Kind < Representable::Decorator
88
include Representable::JSON
99

10-
property :title, documentation: { type: 'string', desc: 'Title of the kind.' }
10+
property :title, documentation: { type: 'string', desc: 'Title of the kind.', example: 123 }
1111
end
1212

1313
class Relation < Representable::Decorator
1414
include Representable::JSON
1515

16-
property :name, type: 'string', desc: 'RelationName', documentation: { type: 'string', desc: 'Name' }
16+
property :name, type: 'string', desc: 'RelationName', documentation: { type: 'string', desc: 'Name', example: -> { 'A relation' } }
1717
end
1818

1919
class Tag < Representable::Decorator
@@ -130,12 +130,12 @@ def app
130130

131131
expect(subject['definitions'].keys).to include 'Kind'
132132
expect(subject['definitions']['Kind']).to eq(
133-
'type' => 'object', 'properties' => { 'title' => { 'type' => 'string', 'description' => 'Title of the kind.' } }
133+
'type' => 'object', 'properties' => { 'title' => { 'type' => 'string', 'description' => 'Title of the kind.', 'example' => 123 } }
134134
)
135135

136136
expect(subject['definitions'].keys).to include 'Relation'
137137
expect(subject['definitions']['Relation']).to eq(
138-
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } }
138+
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name', 'example' => 'A relation' } }
139139
)
140140

141141
expect(subject['definitions'].keys).to include 'Tag'

0 commit comments

Comments
 (0)