Skip to content

Commit

Permalink
Merge pull request #41 from drewish/better-yaml-formatting
Browse files Browse the repository at this point in the history
Swagger expects YAML to have string values
  • Loading branch information
drewish committed Aug 30, 2018
2 parents ac1d350 + b32f377 commit f8fb9d4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/rspec/rails/swagger/formatter.rb
Expand Up @@ -73,7 +73,7 @@ def example_output(notification)
def write_file(name, document)
output =
if %w(.yaml .yml).include? File.extname(name)
YAML.dump(deep_stringify_keys(document))
YAML.dump(deep_stringify(document))
else
JSON.pretty_generate(document) + "\n"
end
Expand All @@ -85,15 +85,19 @@ def write_file(name, document)
target.write(output)
end

# Lifted from ActiveSupport's Hash _deep_transform_keys_in_object
def deep_stringify_keys(object)
# Converts hash keys and symbolic values into strings.
#
# Based on ActiveSupport's Hash _deep_transform_keys_in_object
def deep_stringify(object)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[key.to_s] = deep_stringify_keys(value)
result[key.to_s] = deep_stringify(value)
end
when Array
object.map { |e| deep_stringify_keys(e) }
object.map { |e| deep_stringify(e) }
when Symbol
object.to_s
else
object
end
Expand Down

0 comments on commit f8fb9d4

Please sign in to comment.