Skip to content

Commit

Permalink
Output JSON in :pretty mode by default
Browse files Browse the repository at this point in the history
Set defaults consistently from defaults constant
Fix issue with write_docs where apis parameter was effectively ignored
Ensure defaults are used when no configuration is set
Use DEFAULT_VER constant in tests instead of "1.0"
  • Loading branch information
richhollis committed Dec 16, 2013
1 parent 6ce5f9a commit 3f03827
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
public
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ The following table shows all the current configuration options and their defaul

<tr>
<td><b>formatting</b></td>
<td>Specifies which formatting method to apply to the JSON that is written. Available options: pretty</td>
<td></td>
<td>Specifies which formatting method to apply to the JSON that is written. Available options: :none, :pretty</td>
<td>:pretty</td>
</tr>

</tbody>
Expand Down
25 changes: 19 additions & 6 deletions lib/swagger/docs/generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
module Swagger
module Docs
class Generator

DEFAULT_VER = "1.0"
DEFAULT_CONFIG = {
:api_file_path => "public/",
:base_path => "/",
:clean_directory => false,
:formatting => :pretty
}

class << self

def camelize_keys_deep!(h)
Expand Down Expand Up @@ -51,17 +60,20 @@ def set_real_methods
Config.base_api_controller.send(:include, Methods) # replace impotent methods with live ones
end

def write_docs(apis)
def write_docs(apis = nil)
apis ||= Config.registered_apis
results = {}
set_real_methods
unless Config.registered_apis.empty?
Config.registered_apis.each do |api_version,config|
unless apis.empty?
apis.each do |api_version,config|
config.reverse_merge!(DEFAULT_CONFIG)
results[api_version] = write_doc(api_version, config)
end
results[DEFAULT_VER][:default_config] = false
else
config = {:api_file_path => "public/", :base_path => "/"}
puts "No swagger_docs config: Using default config #{config}"
results["1.0"] = write_doc("1.0", config)
puts "No swagger_docs config: Using default config #{DEFAULT_CONFIG}" unless defined?(SPEC_HELPER_PRESENT)
results[DEFAULT_VER] = write_doc(DEFAULT_VER, DEFAULT_CONFIG)
results[DEFAULT_VER][:default_config] = true
end
results
end
Expand Down Expand Up @@ -116,6 +128,7 @@ def write_doc(api_version, config)
write_to_file "#{api_file_path}/api-docs.json", resources, config
results
end

def write_to_file(path, structure, config={})
content = case config[:formatting]
when :pretty; JSON.pretty_generate structure
Expand Down
36 changes: 24 additions & 12 deletions spec/lib/swagger/docs/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ def stub_route(verb, action, controller, spec)
]}

context "without controller base path" do
let(:config) { Swagger::Docs::Config.register_apis({
"1.0" => {:api_file_path => "#{TMP_DIR}api/v1/", :base_path => "http://api.no.where"}
})}
let(:config) {
{
DEFAULT_VER => {:api_file_path => "#{TMP_DIR}api/v1/", :base_path => "http://api.no.where"}
}
}
before(:each) do
Rails.stub_chain(:application, :routes, :routes).and_return(routes)
Swagger::Docs::Generator.set_real_methods
Expand Down Expand Up @@ -79,7 +81,7 @@ def stub_route(verb, action, controller, spec)

context "with controller base path" do
let(:config) { Swagger::Docs::Config.register_apis({
"1.0" => {:controller_base_path => "api/v1", :api_file_path => "#{TMP_DIR}api/v1/", :base_path => "http://api.no.where"}
DEFAULT_VER => {:controller_base_path => "api/v1", :api_file_path => "#{TMP_DIR}api/v1/", :base_path => "http://api.no.where"}
})}
before(:each) do
Rails.stub_chain(:application, :routes, :routes).and_return(routes)
Expand All @@ -97,22 +99,32 @@ def stub_route(verb, action, controller, spec)
end

describe "#write_docs" do
context "no apis registered" do
before(:each) do
Swagger::Docs::Config.register_apis({})
end
it "uses default config when not set" do
expect(Swagger::Docs::Generator.write_docs()[DEFAULT_VER][:default_config]).to be_true
end
end
before(:each) do
generate(config)
end
it "doesn't use default config" do
expect(Swagger::Docs::Generator.write_docs()[DEFAULT_VER][:default_config]).to be_false
end
it "cleans json files in directory when set" do
file_to_delete = TMP_DIR+"api/v1/delete_me.json"
File.open(file_to_delete, 'w') {|f| f.write("{}") }
expect(file_to_delete).to exist

config["1.0"][:clean_directory] = true
config[DEFAULT_VER][:clean_directory] = true
generate(config)
expect(file_to_delete).to_not exist
end
it "keeps non json files in directory when cleaning" do
file_to_keep = TMP_DIR+"api/v1/keep_me"
File.open(file_to_keep, 'w') {|f| f.write("{}") }
config["1.0"][:clean_directory] = true
config[DEFAULT_VER][:clean_directory] = true
generate(config)
expect(file_to_keep).to exist
end
Expand All @@ -124,11 +136,11 @@ def stub_route(verb, action, controller, spec)
end
it "returns results hash" do
results = generate(config)
expect(results["1.0"][:processed].count).to eq 1
expect(results["1.0"][:skipped].count).to eq 1
expect(results[DEFAULT_VER][:processed].count).to eq 1
expect(results[DEFAULT_VER][:skipped].count).to eq 1
end
it "writes pretty json files when set" do
config["1.0"][:formatting] = :pretty
config[DEFAULT_VER][:formatting] = :pretty
generate(config)
resources = File.read FILE_RESOURCES
expect(resources.scan(/\n/).length).to be > 1
Expand All @@ -137,7 +149,7 @@ def stub_route(verb, action, controller, spec)
let(:resources) { FILE_RESOURCES.read }
let(:response) { JSON.parse(resources) }
it "writes version correctly" do
expect(response["apiVersion"]).to eq "1.0"
expect(response["apiVersion"]).to eq DEFAULT_VER
end
it "writes swaggerVersion correctly" do
expect(response["swaggerVersion"]).to eq "1.2"
Expand All @@ -164,7 +176,7 @@ def stub_route(verb, action, controller, spec)
let(:response_msgs) { operations.first["responseMessages"] }
# {"apiVersion":"1.0","swaggerVersion":"1.2","basePath":"/api/v1","resourcePath":"/sample"
it "writes version correctly" do
expect(response["apiVersion"]).to eq "1.0"
expect(response["apiVersion"]).to eq DEFAULT_VER
end
it "writes swaggerVersion correctly" do
expect(response["swaggerVersion"]).to eq "1.2"
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require "json"
require 'pathname'

SPEC_HELPER_PRESENT = true
DEFAULT_VER = Swagger::Docs::Generator::DEFAULT_VER

TMP_DIR = Pathname.new "/tmp/swagger-docs/"
TMP_API_DIR = TMP_DIR+"api/v1"
FILE_RESOURCES = TMP_API_DIR+"api-docs.json"
Expand Down

0 comments on commit 3f03827

Please sign in to comment.