From 6bbe7c5b81f2c46889beeb3320690a301af8d8d6 Mon Sep 17 00:00:00 2001 From: Jan Jedrychowski Date: Sat, 30 Jan 2016 00:42:37 +0100 Subject: [PATCH] Inline keys support. Optional blocks --- README.md | 28 ++++ lib/swagger/blocks.rb | 209 +++++++++++++++-------------- spec/lib/swagger_blocks_spec.rb | 58 +++----- spec/lib/swagger_v2_blocks_spec.rb | 31 ++--- 4 files changed, 163 insertions(+), 163 deletions(-) diff --git a/README.md b/README.md index 31f46a3..03af798 100644 --- a/README.md +++ b/README.md @@ -331,6 +331,34 @@ The `key` block simply takes the value you give and puts it directly into the fi key :foo, {some_complex: {nested_object: true}} ``` +#### Inline keys + +It is possible to condensed syntax to omit numerous `key` calls using hash argument. + +All three calls are equal: + +```ruby +parameter do + key :paramType, :path + key :name, :petId + key :description, 'ID of pet that needs to be fetched' + key :type, :string +end +``` + +```ruby +parameter paramType: :path, name: :petId do + key :description, 'ID of pet that needs to be fetched' + key :type, :string +end +``` + +```ruby +parameter paramType: :path, name: :petId, + description: 'ID of pet that needs to be fetched', + type: :string +``` + #### Writing JSON to a file If you are not serving the JSON directly and need to write it to a file for some reason, you can easily use `build_root_json` for that as well: diff --git a/lib/swagger/blocks.rb b/lib/swagger/blocks.rb index eb7b5bc..8717b82 100644 --- a/lib/swagger/blocks.rb +++ b/lib/swagger/blocks.rb @@ -113,8 +113,8 @@ module ClassMethods # v1.2: http://goo.gl/PvwUXj#51-resource-listing # v2.0: Defines a Swagger Object # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swagger-object - def swagger_root(&block) - @swagger_root_node ||= Swagger::Blocks::RootNode.call(&block) + def swagger_root(inline_keys = nil, &block) + @swagger_root_node ||= Swagger::Blocks::RootNode.call(inline_keys: inline_keys, &block) end # v1.2: Defines a Swagger API Declaration. @@ -122,7 +122,7 @@ def swagger_root(&block) # v1.2: # v1.2: @param resource_name [Symbol] An identifier for this API. All swagger_api_root declarations # v1.2: with the same resource_name will be into a single API root node. - def swagger_api_root(resource_name, &block) + def swagger_api_root(resource_name, inline_keys = nil, &block) resource_name = resource_name.to_sym # Map of path names to ApiDeclarationNodes. @@ -136,7 +136,7 @@ def swagger_api_root(resource_name, &block) api_node.instance_eval(&block) else # First time we've seen this `swagger_api_root :resource_name`. - api_node = Swagger::Blocks::ApiDeclarationNode.call(version: '1.2', &block) + api_node = Swagger::Blocks::ApiDeclarationNode.call(version: '1.2', inline_keys: inline_keys, &block) end # Add it into the resource_name to node map (may harmlessly overwrite the same object). @@ -165,16 +165,16 @@ def swagger_path(path, &block) # v1.2: Defines a Swagger Model. # v1.2: http://goo.gl/PvwUXj#526-models-object - def swagger_model(name, &block) + def swagger_model(name, inline_keys = nil, &block) @swagger_models_node ||= Swagger::Blocks::ModelsNode.new @swagger_models_node.version = '1.2' - @swagger_models_node.model(name, &block) + @swagger_models_node.model(name, inline_keys, &block) end # v2.0: Defines a Swagger Definition Schema, # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#definitionsObject and # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schema-object - def swagger_schema(name, &block) + def swagger_schema(name, inline_keys = nil, &block) @swagger_schema_node_map ||= {} schema_node = @swagger_schema_node_map[name] @@ -183,7 +183,7 @@ def swagger_schema(name, &block) schema_node.instance_eval(&block) else # First time we've seen this schema_node - @swagger_schema_node_map[name] = Swagger::Blocks::SchemaNode.call(version: '2.0', &block) + @swagger_schema_node_map[name] = Swagger::Blocks::SchemaNode.call(version: '2.0', inline_keys: inline_keys, &block) end end @@ -217,7 +217,8 @@ def self.call(options = {}, &block) instance = new instance.name = options[:name] if options[:name] instance.version = options[:version] - instance.instance_eval(&block) + instance.keys options[:inline_keys] + instance.instance_eval(&block) if block instance end @@ -248,6 +249,10 @@ def data @data ||= {} end + def keys(data) + self.data.merge!(data) if data + end + def key(key, value) self.data[key] = value end @@ -288,60 +293,60 @@ def has_api_path?(api_path) api_paths.include?(api_path) end - def authorization(name, &block) + def authorization(name, inline_keys = nil, &block) raise NotSupportedError unless is_swagger_1_2? self.data[:authorizations] ||= Swagger::Blocks::ResourceListingAuthorizationsNode.new self.data[:authorizations].version = version - self.data[:authorizations].authorization(name, &block) + self.data[:authorizations].authorization(name, inline_keys, &block) end - def info(&block) - self.data[:info] = Swagger::Blocks::InfoNode.call(version: version, &block) + def info(inline_keys = nil, &block) + self.data[:info] = Swagger::Blocks::InfoNode.call(version: version, inline_keys: inline_keys, &block) end - def api(&block) + def api(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_1_2? self.data[:apis] ||= [] - self.data[:apis] << Swagger::Blocks::ResourceNode.call(version: version, &block) + self.data[:apis] << Swagger::Blocks::ResourceNode.call(version: version, inline_keys: inline_keys ,&block) end - def parameter(param, &block) + def parameter(param, inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? # TODO validate 'param' is as per spec self.data[:parameters] ||= {} - self.data[:parameters][param] = Swagger::Blocks::ParameterNode.call(version: version, &block) + self.data[:parameters][param] = Swagger::Blocks::ParameterNode.call(version: version, inline_keys: inline_keys, &block) end - def response(resp, &block) + def response(resp, inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? # TODO validate 'resp' is as per spec self.data[:responses] ||= {} - self.data[:responses][resp] = Swagger::Blocks::ResponseNode.call(version: version, &block) + self.data[:responses][resp] = Swagger::Blocks::ResponseNode.call(version: version, inline_keys: inline_keys, &block) end - def security_definition(name, &block) + def security_definition(name, inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? self.data[:securityDefinitions] ||= {} - self.data[:securityDefinitions][name] = Swagger::Blocks::SecuritySchemeNode.call(version: version, &block) + self.data[:securityDefinitions][name] = Swagger::Blocks::SecuritySchemeNode.call(version: version, inline_keys: inline_keys, &block) end - def security(&block) + def security(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? self.data[:security] ||= [] - self.data[:security] << Swagger::Blocks::SecurityRequirementNode.call(version: version, &block) + self.data[:security] << Swagger::Blocks::SecurityRequirementNode.call(version: version, inline_keys: inline_keys, &block) end - def tag(&block) + def tag(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? self.data[:tags] ||= [] - self.data[:tags] << Swagger::Blocks::TagNode.call(version: version, &block) + self.data[:tags] << Swagger::Blocks::TagNode.call(version: version, inline_keys: inline_keys, &block) end # Use 'tag' instead. @@ -355,8 +360,8 @@ class ResourceNode < Node; end # v1.2: NOTE: in the spec this is different than API Declaration authorizations. # v1.2: http://goo.gl/PvwUXj#514-authorizations-object class ResourceListingAuthorizationsNode < Node - def authorization(name, &block) - self.data[name] = Swagger::Blocks::ResourceListingAuthorizationNode.call(version: version, &block) + def authorization(name, inline_keys = nil, &block) + self.data[name] = Swagger::Blocks::ResourceListingAuthorizationNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -365,33 +370,33 @@ def authorization(name, &block) class ResourceListingAuthorizationNode < Node GRANT_TYPES = [:implicit, :authorization_code].freeze - def scope(&block) + def scope(inline_keys = nil, &block) self.data[:scopes] ||= [] - self.data[:scopes] << Swagger::Blocks::ScopeNode.call(version: version, &block) + self.data[:scopes] << Swagger::Blocks::ScopeNode.call(version: version, inline_keys: inline_keys, &block) end - def grant_type(name, &block) + def grant_type(name, inline_keys = nil, &block) raise ArgumentError.new("#{name} not in #{GRANT_TYPES}") if !GRANT_TYPES.include?(name) self.data[:grantTypes] ||= Swagger::Blocks::GrantTypesNode.new self.data[:grantTypes].version = version - self.data[:grantTypes].implicit(&block) if name == :implicit - self.data[:grantTypes].authorization_code(&block) if name == :authorization_code + self.data[:grantTypes].implicit(inline_keys, &block) if name == :implicit + self.data[:grantTypes].authorization_code(inline_keys, &block) if name == :authorization_code end end # v1.2: http://goo.gl/PvwUXj#513-info-object # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#infoObject class InfoNode < Node - def contact(&block) + def contact(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? - self.data[:contact] = Swagger::Blocks::ContactNode.call(version: version, &block) + self.data[:contact] = Swagger::Blocks::ContactNode.call(version: version, inline_keys: inline_keys, &block) end - def license(&block) + def license(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? - self.data[:license] = Swagger::Blocks::LicenseNode.call(version: version, &block) + self.data[:license] = Swagger::Blocks::LicenseNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -409,12 +414,12 @@ class ScopesNode < Node; end # v1.2: http://goo.gl/PvwUXj#517-grant-types-object class GrantTypesNode < Node - def implicit(&block) - self.data[:implicit] = Swagger::Blocks::ImplicitNode.call(version: version, &block) + def implicit(inline_keys, &block) + self.data[:implicit] = Swagger::Blocks::ImplicitNode.call(inline_keys: inline_keys, version: version, &block) end - def authorization_code(&block) - self.data[:authorization_code] = Swagger::Blocks::AuthorizationCodeNode.call(version: version, &block) + def authorization_code(inline_keys, &block) + self.data[:authorization_code] = Swagger::Blocks::AuthorizationCodeNode.call(inline_keys: inline_keys, version: version, &block) end end @@ -430,12 +435,12 @@ class LoginEndpointNode < Node; end # v1.2: http://goo.gl/PvwUXj#519-authorization-code-object class AuthorizationCodeNode < Node - def token_request_endpoint(&block) - self.data[:tokenRequestEndpoint] = Swagger::Blocks::TokenRequestEndpointNode.call(version: version, &block) + def token_request_endpoint(inline_keys = nil, &block) + self.data[:tokenRequestEndpoint] = Swagger::Blocks::TokenRequestEndpointNode.call(version: version, inline_keys: inline_keys, &block) end - def token_endpoint(&block) - self.data[:tokenEndpoint] = Swagger::Blocks::TokenEndpointNode.call(version: version, &block) + def token_endpoint(inline_keys = nil, &block) + self.data[:tokenEndpoint] = Swagger::Blocks::TokenEndpointNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -451,7 +456,7 @@ class TokenEndpointNode < Node; end # v1.2: http://goo.gl/PvwUXj#52-api-declaration class ApiDeclarationNode < Node - def api(&block) + def api(inline_keys = nil, &block) self.data[:apis] ||= [] # Important: to conform with the Swagger spec, merge with any previous API declarations @@ -461,7 +466,7 @@ def api(&block) # http://goo.gl/PvwUXj#522-api-object # - The API Object describes one or more operations on a single path. In the apis array, # there MUST be only one API Object per path. - temp_api_node = Swagger::Blocks::ApiNode.call(version: version, &block) + temp_api_node = Swagger::Blocks::ApiNode.call(version: version, inline_keys: inline_keys, &block) api_node = self.data[:apis].select do |api| api.data[:path] == temp_api_node.data[:path] end[0] # Embrace Ruby wtfs. @@ -478,9 +483,9 @@ def api(&block) # v1.2: http://goo.gl/PvwUXj#522-api-object class ApiNode < Node - def operation(&block) + def operation(inline_keys = nil, &block) self.data[:operations] ||= [] - self.data[:operations] << Swagger::Blocks::OperationNode.call(version: version, &block) + self.data[:operations] << Swagger::Blocks::OperationNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -489,10 +494,10 @@ class PathNode < Node OPERATION_TYPES = [:get, :put, :post, :delete, :options, :head, :patch].freeze # TODO support ^x- Vendor Extensions - def operation(op, &block) + def operation(op, inline_keys = nil, &block) op = op.to_sym raise ArgumentError.new("#{name} not in #{OPERATION_TYPES}") if !OPERATION_TYPES.include?(op) - self.data[op] = Swagger::Blocks::OperationNode.call(version: version, &block) + self.data[op] = Swagger::Blocks::OperationNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -500,51 +505,51 @@ def operation(op, &block) # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operation-object class OperationNode < Node - def parameter(&block) + def parameter(inline_keys = nil, &block) self.data[:parameters] ||= [] - self.data[:parameters] << Swagger::Blocks::ParameterNode.call(version: version, &block) + self.data[:parameters] << Swagger::Blocks::ParameterNode.call(version: version, inline_keys: inline_keys, &block) end - def response_message(&block) + def response_message(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_1_2? self.data[:responseMessages] ||= [] - self.data[:responseMessages] << Swagger::Blocks::Node.call(version: version, &block) + self.data[:responseMessages] << Swagger::Blocks::Node.call(version: version, inline_keys: inline_keys, &block) end - def authorization(name, &block) + def authorization(name, inline_keys = nil, &block) raise NotSupportedError unless is_swagger_1_2? self.data[:authorizations] ||= Swagger::Blocks::ApiAuthorizationsNode.new self.data[:authorizations].version = version - self.data[:authorizations].authorization(name, &block) + self.data[:authorizations].authorization(name, inline_keys, &block) end - def items(&block) + def items(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_1_2? - self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, &block) + self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block) end - def response(resp, &block) + def response(resp, inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? # TODO validate 'resp' is as per spec self.data[:responses] ||= {} - self.data[:responses][resp] = Swagger::Blocks::ResponseNode.call(version: version, &block) + self.data[:responses][resp] = Swagger::Blocks::ResponseNode.call(version: version, inline_keys: inline_keys, &block) end - def externalDocs(&block) + def externalDocs(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? - self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, &block) + self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, inline_keys: inline_keys, &block) end - def security(&block) + def security(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? self.data[:security] ||= [] - self.data[:security] << Swagger::Blocks::SecurityRequirementNode.call(version: version, &block) + self.data[:security] << Swagger::Blocks::SecurityRequirementNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -558,16 +563,16 @@ class SecurityRequirementNode < Node; end class SecuritySchemeNode < Node # TODO support ^x- Vendor Extensions - def scopes(&block) - self.data[:scopes] = Swagger::Blocks::ScopesNode.call(version: version, &block) + def scopes(inline_keys = nil, &block) + self.data[:scopes] = Swagger::Blocks::ScopesNode.call(version: version, inline_keys: inline_keys, &block) end end # v1.2: NOTE: in the spec this is different than Resource Listing's authorizations. # v1.2: http://goo.gl/PvwUXj#514-authorizations-object class ApiAuthorizationsNode < Node - def authorization(name, &block) - self.data[name] ||= Swagger::Blocks::ApiAuthorizationNode.call(version: version, &block) + def authorization(name, inline_keys, &block) + self.data[name] ||= Swagger::Blocks::ApiAuthorizationNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -581,9 +586,9 @@ def as_json self.data[:_scopes].map { |s| s.as_json } end - def scope(&block) + def scope(inline_keys = nil, &block) self.data[:_scopes] ||= [] - self.data[:_scopes] << Swagger::Blocks::ApiAuthorizationScopeNode.call(version: version, &block) + self.data[:_scopes] << Swagger::Blocks::ApiAuthorizationScopeNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -593,20 +598,20 @@ class ApiAuthorizationScopeNode < Node; end # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#responseObject class ResponseNode < Node - def schema(&block) - self.data[:schema] = Swagger::Blocks::SchemaNode.call(version: version, &block) + def schema(inline_keys = nil, &block) + self.data[:schema] = Swagger::Blocks::SchemaNode.call(version: version, inline_keys: inline_keys, &block) end - def header(head, &block) + def header(head, inline_keys = nil, &block) # TODO validate 'head' is as per spec self.data[:headers] ||= {} - self.data[:headers][head] = Swagger::Blocks::HeaderNode.call(version: version, &block) + self.data[:headers][head] = Swagger::Blocks::HeaderNode.call(version: version, inline_keys: inline_keys, &block) end - def example(exam, &block) + def example(exam, inline_keys = nil, &block) # TODO validate 'exam' is as per spec self.data[:examples] ||= {} - self.data[:examples][exam] = Swagger::Blocks::ExampleNode.call(version: version, &block) + self.data[:examples][exam] = Swagger::Blocks::ExampleNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -642,40 +647,40 @@ def key(key, value) raise NotSupportedError end - def schema(&block) - data << Swagger::Blocks::SchemaNode.call(version: version, &block) + def schema(inline_keys = nil, &block) + data << Swagger::Blocks::SchemaNode.call(version: version, inline_keys: inline_keys, &block) end end # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schema-object class SchemaNode < Node - def items(&block) - self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, &block) + def items(inline_keys = nil, &block) + self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block) end def allOf(&block) self.data[:allOf] = Swagger::Blocks::AllOfNode.call(version: version, &block) end - def property(name, &block) + def property(name, inline_keys = nil, &block) self.data[:properties] ||= Swagger::Blocks::PropertiesNode.new self.data[:properties].version = version - self.data[:properties].property(name, &block) + self.data[:properties].property(name, inline_keys, &block) end - def xml(&block) - self.data[:xml] = Swagger::Blocks::XmlNode.call(version: version, &block) + def xml(inline_keys = nil, &block) + self.data[:xml] = Swagger::Blocks::XmlNode.call(version: version, inline_keys: inline_keys, &block) end - def externalDocs(&block) - self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, &block) + def externalDocs(inline_keys = nil, &block) + self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, inline_keys: inline_keys, &block) end end # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#headerObject class HeaderNode < Node - def items(&block) - self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, &block) + def items(inline_keys = nil, &block) + self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -692,16 +697,16 @@ class ItemsNode < Node; end # v1.2: http://goo.gl/PvwUXj#524-parameter-object # v2.0: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#parameter-object class ParameterNode < Node - def schema(&block) + def schema(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? - self.data[:schema] = Swagger::Blocks::SchemaNode.call(version: version, &block) + self.data[:schema] = Swagger::Blocks::SchemaNode.call(version: version, inline_keys: inline_keys, &block) end - def items(&block) + def items(inline_keys = nil, &block) raise NotSupportedError unless is_swagger_2_0? - self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, &block) + self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -710,8 +715,8 @@ class TagNode < Node # TODO support ^x- Vendor Extensions - def externalDocs(&block) - self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, &block) + def externalDocs(inline_keys = nil, &block) + self.data[:externalDocs] = Swagger::Blocks::ExternalDocsNode.call(version: version, inline_keys: inline_keys, &block) end end @@ -725,31 +730,31 @@ def merge!(other_models_node) self.data.merge!(other_models_node.data) end - def model(name, &block) - self.data[name] ||= Swagger::Blocks::ModelNode.call(version: version, &block) + def model(name, inline_keys, &block) + self.data[name] ||= Swagger::Blocks::ModelNode.call(version: version, inline_keys: inline_keys, &block) end end # v1.2: http://goo.gl/PvwUXj#527-model-object class ModelNode < Node - def property(name, &block) + def property(name, inline_keys = nil, &block) self.data[:properties] ||= Swagger::Blocks::PropertiesNode.new self.data[:properties].version = version - self.data[:properties].property(name, &block) + self.data[:properties].property(name, inline_keys, &block) end end # v1.2: http://goo.gl/PvwUXj#527-model-object class PropertiesNode < Node - def property(name, &block) - self.data[name] = Swagger::Blocks::PropertyNode.call(version: version, &block) + def property(name, inline_keys = nil, &block) + self.data[name] = Swagger::Blocks::PropertyNode.call(version: version, inline_keys: inline_keys, &block) end end # v1.2: http://goo.gl/PvwUXj#527-model-object class PropertyNode < Node - def items(&block) - self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, &block) + def items(inline_keys = nil, &block) + self.data[:items] = Swagger::Blocks::ItemsNode.call(version: version, inline_keys: inline_keys, &block) end end end diff --git a/spec/lib/swagger_blocks_spec.rb b/spec/lib/swagger_blocks_spec.rb index dea6e24..717298a 100644 --- a/spec/lib/swagger_blocks_spec.rb +++ b/spec/lib/swagger_blocks_spec.rb @@ -10,21 +10,16 @@ class PetController include Swagger::Blocks - swagger_root do - key :swaggerVersion, '1.2' + swagger_root swaggerVersion: '1.2'do key :apiVersion, '1.0.0' - info do - key :title, 'Swagger Sample App' + info title: 'Swagger Sample App' do key :description, "This is a sample server Petstore server. You can find out more about Swagger \n at http://swagger.wordnik.com or on irc.freenode.net, #swagger. For this sample,\n you can use the api key \"special-key\" to test the authorization filters" key :termsOfServiceUrl, 'http://helloreverb.com/terms/' - key :contact, 'apiteam@wordnik.com' - key :license, 'Apache 2.0' - key :licenseUrl, 'http://www.apache.org/licenses/LICENSE-2.0.html' - end - api do - key :path, '/pet' - key :description, 'Operations about pets' + keys contact: 'apiteam@wordnik.com', + license: 'Apache 2.0', + licenseUrl: 'http://www.apache.org/licenses/LICENSE-2.0.html' end + api path: '/pet', description: 'Operations about pets' api do key :path, '/user' key :description, 'Operations about user' @@ -33,39 +28,31 @@ class PetController key :path, '/store' key :description, 'Operations about store' end - authorization :oauth2 do - key :type, 'oauth2' - scope do - key :scope, 'email' - key :description, 'Access to your email address' - end + authorization :oauth2, type: 'oauth2' do + scope scope: 'email', description: 'Access to your email address' scope do key :scope, 'pets' key :description, 'Access to your pets' end - grant_type :implicit do + grant_type :implicit, tokenName: 'access_token' do login_endpoint do key :url, 'http://petstore.swagger.wordnik.com/oauth/dialog' end - key :tokenName, 'access_token' end grant_type :authorization_code do - token_request_endpoint do + token_request_endpoint clientSecretName: 'client_secret' do key :url, 'http://petstore.swagger.wordnik.com/oauth/requestToken' key :clientIdName, 'client_id' - key :clientSecretName, 'client_secret' end - token_endpoint do + token_endpoint tokenName: 'access_code' do key :url, 'http://petstore.swagger.wordnik.com/oauth/token' - key :tokenName, 'access_code' end end end end # All swagger_api_root declarations with the same key will be merged. - swagger_api_root :pets do - key :swaggerVersion, '1.2' + swagger_api_root :pets, swaggerVersion: '1.2' do key :apiVersion, '1.0.0' key :basePath, 'http://petstore.swagger.wordnik.com/api' key :resourcePath, '/pet' @@ -131,8 +118,7 @@ class PetController key :description, 'anything' end end - parameter do - key :paramType, :path + parameter paramType: :path do key :name, :petId key :description, 'ID of pet that needs to be fetched' key :required, true @@ -145,8 +131,7 @@ class PetController key :required, true key :type, :Pet end - response_message do - key :code, 400 + response_message code: 400 do key :message, 'Invalid tag value' end end @@ -156,15 +141,12 @@ class PetController swagger_api_root :pets do api do key :path, '/pet/findByStatus' - operation do - key :method, 'GET' + operation method: 'GET' do key :summary, 'Finds Pets by status' key :notes, 'Multiple status values can be provided with comma seperated strings' key :type, :array key :nickname, :findPetsByStatus - items do - key :'$ref', :Pet - end + items :'$ref' => :Pet parameter do key :paramType, :query key :name, :status @@ -178,10 +160,7 @@ class PetController 'sold', ] end - response_message do - key :code, 400 - key :message, 'Invalid status value' - end + response_message code: 400, message: 'Invalid status value' end end end @@ -225,8 +204,7 @@ class TagModel class OtherModelsContainer include Swagger::Blocks - swagger_model :Pet do - key :id, :Pet + swagger_model :Pet, id: :Pet do key :required, [:id, :name] property :id do key :type, :integer diff --git a/spec/lib/swagger_v2_blocks_spec.rb b/spec/lib/swagger_v2_blocks_spec.rb index 00e892c..a37fa36 100644 --- a/spec/lib/swagger_v2_blocks_spec.rb +++ b/spec/lib/swagger_v2_blocks_spec.rb @@ -8,10 +8,9 @@ class PetControllerV2 include Swagger::Blocks - swagger_root do + swagger_root host: 'petstore.swagger.wordnik.com' do key :swagger, '2.0' - info do - key :version, '1.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 swagger-2.0 specification' @@ -23,13 +22,11 @@ class PetControllerV2 key :name, 'MIT' end end - key :host, 'petstore.swagger.wordnik.com' key :basePath, '/api' key :schemes, ['http'] key :consumes, ['application/json'] key :produces, ['application/json'] - security_definition :api_key do - key :type, :apiKey + security_definition :api_key, type: :apiKey do key :name, :api_key key :in, :header end @@ -37,16 +34,13 @@ class PetControllerV2 key :type, :oauth2 key :authorizationUrl, 'http://swagger.io/api/oauth/dialog' key :flow, :implicit - scopes do - key 'write:pets', 'modify pets in your account' + scopes 'write:pets' => 'modify pets in your account' do key 'read:pets', 'read your pets' end end - tag do - key :name, 'pet' + tag name: 'pet' do key :description, 'Pets operations' - externalDocs do - key :description, 'Find more info here' + externalDocs description: 'Find more info here' do key :url, 'https://swagger.io' end end @@ -83,8 +77,7 @@ class PetControllerV2 end response 200 do key :description, 'pet response' - schema do - key :type, :array + schema type: :array do items do key :'$ref', :Pet end @@ -119,8 +112,7 @@ class PetControllerV2 key :'$ref', '#/parameters/Pet' end end - response :default do - key :description, 'unexpected error' + response :default, description: 'unexpected error' do schema do key :'$ref', :ErrorModel end @@ -158,9 +150,7 @@ class PetControllerV2 key :'$ref', :ErrorModel end end - security do - key :api_key, [] - end + security api_key: [] security do key :petstore_auth, ['write:pets', 'read:pets'] end @@ -193,8 +183,7 @@ class PetControllerV2 class PetV2 include Swagger::Blocks - swagger_schema :Pet do - key :required, [:id, :name] + swagger_schema :Pet, required: [:id, :name] do property :id do key :type, :integer key :format, :int64