Skip to content

Commit

Permalink
Auto-detect parameters' types from example values
Browse files Browse the repository at this point in the history
  • Loading branch information
khiav223577 authored and khiav reoy committed Sep 11, 2020
1 parent adb498a commit 6fa0703
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/rspec_api_documentation/views/api_blueprint_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def format_route(example)
# properties_description: "required, string"
# }
def fields(property_name, examples)
possible_parameters_values = examples.map do |example|
parameters = example.metadata[:extended_parameters]
next parameters.map { |parameter| [parameter[:name], parameter[:value]] }.to_h
end

examples
.map { |example| example.metadata[property_name] }
.flatten
Expand All @@ -79,7 +84,19 @@ def fields(property_name, examples)
else
properties << 'optional'
end
properties << property[:type] if property[:type]

if property.key?(:type)
properties << property[:type] if property[:type]
else
possible_values = possible_parameters_values.map { |parameters| parameters[property[:name]] }
possible_types = possible_values.map { |value| guess_type(value) }

possible_types.uniq!
possible_types.compact!

properties.concat(possible_types)
end

if properties.count > 0
property[:properties_description] = properties.join(", ")
else
Expand All @@ -96,6 +113,21 @@ def fields(property_name, examples)
end
end

def guess_type(value)
case value
when TrueClass then :boolean
when FalseClass then :boolean
when Array then :array
when Integer then :integer
when Float then :number
when String then :string
when Rack::Test::UploadedFile then :file
when NilClass then nil
else
puts "Warning: unknown type of value #{value}"
end
end

# When no `description` was specified for a parameter, the DSL class
# is making `description = "#{scope} #{name}"`, which is bad because it
# assumes that all formats want this behavior. To avoid changing there
Expand Down

0 comments on commit 6fa0703

Please sign in to comment.