@@ -9,8 +9,8 @@ def initialize(index, configuration)
99 def sections
1010 super . map do |section |
1111 routes = section [ :examples ] . group_by ( &:route_uri ) . map do |route_uri , examples |
12- attrs = examples . map { | example | example . metadata [ :attributes ] } . flatten . compact . uniq { | attr | attr [ :name ] }
13- params = examples . map { | example | example . metadata [ :parameters ] } . flatten . compact . uniq { | param | param [ :name ] }
12+ attrs = fields ( :attributes , examples )
13+ params = fields ( :parameters , examples )
1414
1515 methods = examples . group_by ( &:http_method ) . map do |http_method , examples |
1616 {
@@ -42,6 +42,49 @@ def examples
4242 ApiBlueprintExample . new ( example , @configuration )
4343 end
4444 end
45+
46+ private
47+
48+ # APIB has both `parameters` and `attributes`. This generates a hash
49+ # with all of its properties, like name, description, required.
50+ # {
51+ # required: true,
52+ # example: "1",
53+ # type: "string",
54+ # name: "id",
55+ # description: "The id",
56+ # properties_description: "required, string"
57+ # }
58+ def fields ( property_name , examples )
59+ examples
60+ . map { |example | example . metadata [ property_name ] }
61+ . flatten
62+ . compact
63+ . uniq { |property | property [ :name ] }
64+ . map do |property |
65+ properties = [ ]
66+ properties << "required" if property [ :required ]
67+ properties << property [ :type ] if property [ :type ]
68+ if properties . count > 0
69+ property [ :properties_description ] = properties . join ( ", " )
70+ else
71+ property [ :properties_description ] = nil
72+ end
73+
74+ property [ :description ] = nil if description_blank? ( property )
75+ property
76+ end
77+ end
78+
79+ # When no `description` was specified for a parameter, the DSL class
80+ # is making `description = "#{scope} #{name}"`, which is bad because it
81+ # assumes that all formats want this behavior. To avoid changing there
82+ # and breaking everything, I do my own check here and if description
83+ # equals the name, I assume it is blank.
84+ def description_blank? ( property )
85+ !property [ :description ] ||
86+ property [ :description ] . to_s . strip == property [ :name ] . to_s . strip
87+ end
4588 end
4689 end
4790end
0 commit comments