From b32f37720e382fae25e113fa0a0388e85b2e0c66 Mon Sep 17 00:00:00 2001 From: andrew morton Date: Thu, 30 Aug 2018 09:10:50 -0600 Subject: [PATCH] Swagger expects YAML to have string values --- lib/rspec/rails/swagger/formatter.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/rspec/rails/swagger/formatter.rb b/lib/rspec/rails/swagger/formatter.rb index 7de8762..19fc32f 100644 --- a/lib/rspec/rails/swagger/formatter.rb +++ b/lib/rspec/rails/swagger/formatter.rb @@ -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 @@ -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