Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
biow0lf committed Nov 27, 2017
1 parent cbbaa0a commit a46e4db
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ tmp
*.o
*.a
mkmf.log
.idea/

6 changes: 2 additions & 4 deletions lib/swagger/blocks/internal_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ def self.parse_swaggered_classes(swaggered_classes)
# TODO should this merge the contents of the root nodes instead?
def self.limit_root_node(root_nodes)
if root_nodes.length == 0
raise Swagger::Blocks::DeclarationError.new(
'swagger_root must be declared')
raise Swagger::Blocks::DeclarationError.new('swagger_root must be declared')
elsif root_nodes.length > 1
raise Swagger::Blocks::DeclarationError.new(
'Only one swagger_root declaration is allowed.')
raise Swagger::Blocks::DeclarationError.new('Only one swagger_root declaration is allowed.')
end
root_nodes.first
end
Expand Down
9 changes: 7 additions & 2 deletions lib/swagger/blocks/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def as_json
end
return result if !name
# If 'name' is given to this node, wrap the data with a root element with the given name.
{name => result}
{ name => result }
end

def data
Expand All @@ -53,12 +53,17 @@ def key(key, value)
def version
return @version if instance_variable_defined?('@version') && @version
return '2.0' if data.has_key?(:swagger) && data[:swagger] == '2.0'
raise DeclarationError, "You must specify swagger '2.0'"
return '3.0' if data.has_key?(:openapi) && data[:openapi].start_with?('3.0')
raise DeclarationError, "You must specify swagger '2.0' or openapi '3.0.0'"
end

def is_swagger_2_0?
version == '2.0'
end

def is_openapi_3?
version == '3.0'
end
end
end
end
4 changes: 2 additions & 2 deletions lib/swagger/blocks/nodes/all_of_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def as_json
result << r
elsif is_swagger_2_0? && value.is_a?(Hash)
r = {}
value.each_pair {|k, v| r[k] = (v.respond_to?(:as_json) ? v.as_json : v) }
value.each_pair { |k, v| r[k] = (v.respond_to?(:as_json) ? v.as_json : v) }
result << r
else
result = value
end
end
return result if !name
# If 'name' is given to this node, wrap the data with a root element with the given name.
{name => result}
{ name => result }
end

def data
Expand Down
2 changes: 1 addition & 1 deletion lib/swagger/blocks/nodes/items_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def property(name, inline_keys = nil, &block)
self.data[:properties].version = version
self.data[:properties].property(name, inline_keys, &block)
end

def items(inline_keys = nil, &block)
self.data[:items] = Swagger::Blocks::Nodes::ItemsNode.call(version: version, inline_keys: inline_keys, &block)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/swagger/blocks/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Swagger
module Blocks
VERSION = '2.0.2'
VERSION = '2.0.2'.freeze
end
end
15 changes: 15 additions & 0 deletions spec/lib/openapi_v3_api_declaration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"description": "A sample API that uses a petstore as an example to demonstrate features in the openapi-3.0.0 specification",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"name": "Wordnik API Team"
},
"license": {
"name": "MIT"
}
}
}
72 changes: 72 additions & 0 deletions spec/lib/openapi_v3_blocks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'json'
require 'swagger/blocks'

# TODO Test data originally based on the Swagger UI example data

RESOURCE_LISTING_JSON_V3 = open(File.expand_path('../openapi_v3_api_declaration.json', __FILE__)).read

class PetControllerV3
include Swagger::Blocks

swagger_root do
key :openapi, '3.0.0'
info version: '1.0.0' do
key :title, 'Swagger Petstore'
key :description, 'A sample API that uses a petstore as an example to ' \
'demonstrate features in the openapi-3.0.0 specification'
key :termsOfService, 'http://helloreverb.com/terms/'
contact do
key :name, 'Wordnik API Team'
end
license do
key :name, 'MIT'
end
end
end
end

describe 'Swagger::Blocks v3' do
describe 'build_json' do
it 'outputs the correct data' do
swaggered_classes = [
PetControllerV3,
# PetV2,
# ErrorModelV2
]
actual = Swagger::Blocks.build_root_json(swaggered_classes)
actual = JSON.parse(actual.to_json) # For access consistency.
data = JSON.parse(RESOURCE_LISTING_JSON_V3)

# Multiple expectations for better test diff output.
expect(actual['info']).to eq(data['info'])
expect(actual['paths']).to be
expect(actual['paths']['/pets']).to be
expect(actual['paths']['/pets']).to eq(data['paths']['/pets'])
expect(actual['paths']['/pets/{id}']).to be
expect(actual['paths']['/pets/{id}']['get']).to be
expect(actual['paths']['/pets/{id}']['get']).to eq(data['paths']['/pets/{id}']['get'])
expect(actual['paths']).to eq(data['paths'])
expect(actual['definitions']).to eq(data['definitions'])
expect(actual).to eq(data)
end
it 'is idempotent' do
# swaggered_classes = [PetControllerV2, PetV2, ErrorModelV2]
swaggered_classes = [PetControllerV3]
actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
data = JSON.parse(RESOURCE_LISTING_JSON_V3)
expect(actual).to eq(data)
end

it 'errors if no swagger_root is declared' do
expect {
Swagger::Blocks.build_root_json([])
}.to raise_error(Swagger::Blocks::DeclarationError)
end

it 'errors if multiple swagger_roots are declared' do
expect {
Swagger::Blocks.build_root_json([PetControllerV3, PetControllerV3])
}.to raise_error(Swagger::Blocks::DeclarationError)
end
end
end

0 comments on commit a46e4db

Please sign in to comment.