-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from aaronrenner/make_camelize_model_propertie…
…s_configurable Made camelize model properties configurable and fixed swagger_model name capitalization issue
- Loading branch information
Showing
8 changed files
with
412 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,120 @@ | ||
module Swagger | ||
module Docs | ||
class ApiDeclarationFile | ||
attr_reader :metadata, :apis | ||
|
||
def initialize(metadata, apis, models) | ||
@metadata = metadata | ||
@apis = camelize_keys_deep apis | ||
@models = models | ||
end | ||
|
||
def generate_resource | ||
resource = build_resource_root_hash | ||
# Add the already-normalized models to the resource. | ||
resource = resource.merge({:models => models}) if models.present? | ||
resource | ||
end | ||
|
||
def base_path | ||
metadata.base_path | ||
end | ||
|
||
def path | ||
metadata.path | ||
end | ||
|
||
def swagger_version | ||
metadata.swagger_version | ||
end | ||
|
||
def api_version | ||
metadata.api_version | ||
end | ||
|
||
def controller_base_path | ||
metadata.controller_base_path | ||
end | ||
|
||
def camelize_model_properties | ||
metadata.camelize_model_properties | ||
end | ||
|
||
def resource_path | ||
demod | ||
end | ||
|
||
def resource_file_path | ||
trim_leading_slash(debased_path.to_s.underscore) | ||
end | ||
|
||
def models | ||
normalize_model_properties @models | ||
end | ||
|
||
private | ||
|
||
def build_resource_root_hash | ||
{ | ||
"apiVersion" => api_version, | ||
"swaggerVersion" => swagger_version, | ||
"basePath" => base_path, | ||
"resourcePath" => resource_path, | ||
"apis" => apis, | ||
"resourceFilePath" => resource_file_path | ||
} | ||
end | ||
|
||
def normalize_model_properties(models) | ||
Hash[ | ||
models.map do |k, v| | ||
if camelize_model_properties | ||
[k.to_s, camelize_keys_deep(v)] | ||
else | ||
[k.to_s, stringify_keys_deep(v)] | ||
end | ||
end] | ||
end | ||
|
||
def demod | ||
"#{debased_path.to_s.camelize}".demodulize.camelize.underscore | ||
end | ||
|
||
def debased_path | ||
path.gsub("#{controller_base_path}", "") | ||
end | ||
|
||
def trim_leading_slash(str) | ||
return str if !str | ||
str.gsub(/\A\/ /, '') | ||
end | ||
|
||
def camelize_keys_deep(obj) | ||
process_keys_deep(obj){|key| key.to_s.camelize(:lower)} | ||
end | ||
|
||
def stringify_keys_deep(obj) | ||
process_keys_deep(obj){|key| key.to_s} | ||
end | ||
|
||
def process_keys_deep(obj, &block) | ||
if obj.is_a? Hash | ||
Hash[ | ||
obj.map do |k, v| | ||
new_key = block.call(k) | ||
new_value = process_keys_deep v, &block | ||
[new_key, new_value] | ||
end | ||
] | ||
elsif obj.is_a? Array | ||
new_value = obj.collect do |a| | ||
process_keys_deep a, &block | ||
end | ||
else | ||
obj | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,18 @@ | ||
module Swagger | ||
module Docs | ||
class ApiDeclarationFileMetadata | ||
DEFAULT_SWAGGER_VERSION = "1.2" | ||
|
||
attr_reader :api_version, :path, :base_path, :controller_base_path, :swagger_version, :camelize_model_properties | ||
|
||
def initialize(api_version, path, base_path, controller_base_path, options={}) | ||
@api_version = api_version | ||
@path = path | ||
@base_path = base_path | ||
@controller_base_path = controller_base_path | ||
@swagger_version = options.fetch(:swagger_version, DEFAULT_SWAGGER_VERSION) | ||
@camelize_model_properties = options.fetch(:camelize_model_properties, true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
spec/lib/swagger/docs/api_declaration_file_metadata_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,55 @@ | ||
require 'spec_helper' | ||
|
||
describe Swagger::Docs::ApiDeclarationFileMetadata do | ||
|
||
describe "#initialize" do | ||
it "sets the api_version property" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath") | ||
|
||
expect(metadata.api_version).to eq("1.0") | ||
end | ||
|
||
it "sets the path property" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath") | ||
|
||
expect(metadata.path).to eq("path") | ||
end | ||
|
||
it "sets the base_path property" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath") | ||
|
||
expect(metadata.base_path).to eq("basePath") | ||
end | ||
|
||
it "sets the controller_base_path property" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath") | ||
|
||
expect(metadata.controller_base_path).to eq("controllerBasePath") | ||
end | ||
|
||
it "defaults the swagger_version property to DEFAULT_SWAGGER_VERSION" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath") | ||
|
||
expect(metadata.swagger_version).to eq(described_class::DEFAULT_SWAGGER_VERSION) | ||
end | ||
|
||
it "allows the swagger_version property to be_overriden" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath", swagger_version: "2.0") | ||
|
||
expect(metadata.swagger_version).to eq("2.0") | ||
end | ||
|
||
|
||
it "defaults the camelize_model_properties property to true" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath") | ||
|
||
expect(metadata.camelize_model_properties).to eq(true) | ||
end | ||
|
||
it "allows the camelize_model_properties property to be overidden" do | ||
metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath", camelize_model_properties: false) | ||
|
||
expect(metadata.camelize_model_properties).to eq(false) | ||
end | ||
end | ||
end |
Oops, something went wrong.