Skip to content

Commit 3e5756b

Browse files
authored
Merge pull request ruby-grape#4 from Kris-LIBIS/master
Use property attributes if not present in documentation attribute
2 parents a973304 + c22fb3b commit 3e5756b

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lib/grape-swagger/representable/parser.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ def parse_representer_property(property)
2020
documentation = property[:documentation] ? property[:documentation].dup : {}
2121

2222
if property[:decorator] && property[:nested]
23-
representer_mapping(property[:decorator], documentation, is_a_collection, false, property[:nested])
23+
representer_mapping(property[:decorator], documentation, property, is_a_collection, false, property[:nested])
2424
elsif property[:decorator]
25-
representer_mapping(property[:decorator], documentation, is_a_collection, true)
25+
representer_mapping(property[:decorator], documentation, property, is_a_collection, true)
2626
elsif property[:nested]
27-
representer_mapping(property[:nested], documentation, is_a_collection)
27+
representer_mapping(property[:nested], documentation, property, is_a_collection)
2828
else
2929
memo = {
30-
description: documentation[:desc] || ''
30+
description: documentation[:desc] || property[:desc] || ''
3131
}
3232

33-
data_type = GrapeSwagger::DocMethods::DataType.call(documentation[:type])
33+
data_type = GrapeSwagger::DocMethods::DataType.call(documentation[:type] || property[:type])
3434
if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
3535
data = GrapeSwagger::DocMethods::DataType.mapping(data_type)
3636
memo[:type] = data.first
@@ -39,7 +39,8 @@ def parse_representer_property(property)
3939
memo[:type] = data_type
4040
end
4141

42-
memo[:enum] = documentation[:values] if documentation[:values].is_a?(Array)
42+
values = documentation[:values] || property[:values] || nil
43+
memo[:enum] = values if values.is_a?(Array)
4344

4445
if is_a_collection || documentation[:is_array]
4546
memo = {
@@ -52,7 +53,7 @@ def parse_representer_property(property)
5253
end
5354
end
5455

55-
def representer_mapping(representer, documentation, is_a_collection = false, is_a_decorator = false, nested = nil)
56+
def representer_mapping(representer, documentation, property, is_a_collection = false, is_a_decorator = false, nested = nil)
5657
if nested.nil? && is_a_decorator
5758
name = endpoint.send(:expose_params_from_model, representer)
5859

@@ -62,12 +63,12 @@ def representer_mapping(representer, documentation, is_a_collection = false, is_
6263
items: {
6364
'$ref' => "#/definitions/#{name}"
6465
},
65-
description: documentation[:desc] || ''
66+
description: documentation[:desc] || property[:desc] || ''
6667
}
6768
else
6869
{
6970
'$ref' => "#/definitions/#{name}",
70-
description: documentation[:desc] || ''
71+
description: documentation[:desc] || property[:desc] || ''
7172
}
7273
end
7374
else
@@ -81,13 +82,13 @@ def representer_mapping(representer, documentation, is_a_collection = false, is_
8182
type: :object,
8283
properties: attributes
8384
},
84-
description: documentation[:desc] || ''
85+
description: documentation[:desc] || property[:desc] || ''
8586
}
8687
else
8788
{
8889
type: :object,
8990
properties: attributes,
90-
description: documentation[:desc] || ''
91+
description: documentation[:desc] || property[:desc] || ''
9192
}
9293
end
9394
end

spec/grape-swagger/representers/response_representer_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class Kind < Representable::Decorator
1313
class Relation < Representable::Decorator
1414
include Representable::JSON
1515

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

1919
class Tag < Representable::Decorator
2020
include Representable::JSON
2121

22-
property :name, documentation: { type: 'string', desc: 'Name' }
22+
property :name, type: 'string', desc: 'Name'
2323
end
2424

2525
class Error < Representable::Decorator

0 commit comments

Comments
 (0)