Class: Apipie::SwaggerGenerator
- Inherits:
-
Object
- Object
- Apipie::SwaggerGenerator
- Defined in:
- lib/apipie/swagger_generator.rb
Overview
Defined Under Namespace
Classes: SwaggerTypeWithFormat
Instance Attribute Summary collapse
-
#computed_interface_id ⇒ Object
readonly
Returns the value of attribute computed_interface_id.
Instance Method Summary collapse
- #add_headers_from_hash(swagger_params_array, headers) ⇒ Object
- #add_missing_params(method, path) ⇒ Object
- #add_params_from_hash(swagger_params_array, param_defs, prefix = nil, default_value_for_in = nil) ⇒ Object
- #add_resource_description(resource_name, resource) ⇒ Object
- #add_resource_methods(resource_name, resource_defs) ⇒ Object
-
#add_resources(resources) ⇒ Object
————————————————————————– Engine interface methods ————————————————————————–.
-
#add_ruby_method(paths, ruby_method) ⇒ Object
————————————————————————– Create swagger definitions for a ruby method ————————————————————————–.
-
#body_allowed_for_current_method ⇒ Object
————————————————————————– swagger “Params” block generation ————————————————————————–.
- #gen_referenced_block_from_params_array(name, params_array, allow_nulls = false) ⇒ Object
- #generate_from_resources(version, resources, method_name, lang, clear_warnings = false) ⇒ Object
-
#include_op_id_in_computed_interface_id(op_id) ⇒ Object
the @computed_interface_id is a number that is uniquely derived from the list of operations added to the swagger definition (in an order-dependent way).
- #include_warning_tags? ⇒ Boolean
- #info(msg) ⇒ Object
-
#init_swagger_vars(version, lang, clear_warnings = false) ⇒ Object
————————————————————————– Initialization ————————————————————————–.
-
#initialize(apipie) ⇒ SwaggerGenerator
constructor
A new instance of SwaggerGenerator.
-
#json_schema_for_method_response(method, return_code, allow_nulls) ⇒ Object
————————————————————————– Responses ————————————————————————–.
- #json_schema_for_self_describing_class(cls, allow_nulls) ⇒ Object
- #json_schema_obj_from_params_array(params_array, allow_nulls = false) ⇒ Object
- #json_schema_param_defs_from_params_array(params_array, allow_nulls = false) ⇒ Object
- #lookup ⇒ Object
-
#param_names_from_path(path) ⇒ Object
————————————————————————– Auto-insertion of parameters that are implicitly defined in the path ————————————————————————–.
- #params_in_body? ⇒ Boolean
- #params_in_body_use_reference? ⇒ Boolean
-
#ref_to(name) ⇒ Object
————————————————————————– JSON schema and referenced-object generation ————————————————————————–.
- #remove_colons(str) ⇒ Object
- #response_schema(response, allow_nulls = false) ⇒ Object
- #responses_use_reference? ⇒ Boolean
-
#ruby_name_for_method(method) ⇒ Object
————————————————————————– Logging, debugging and regression-testing utilities ————————————————————————–.
- #save_field(entry, openapi_key, v, apipie_key = openapi_key, translate = false) ⇒ Object
-
#swagger_atomic_param(param_desc, in_schema, name, allow_nulls) ⇒ Object
————————————————————————– The core routine for creating a swagger parameter definition block.
- #swagger_id_for_typename(typename) ⇒ Object
- #swagger_op_id_for_method(method) ⇒ Object
- #swagger_op_id_for_path(http_method, path) ⇒ Object
- #swagger_param_type(param_desc) ⇒ Object
- #swagger_params_array_for_method(method, path) ⇒ Object
-
#swagger_path(str) ⇒ Object
————————————————————————– Utilities for conversion of ruby syntax to swagger syntax ————————————————————————–.
- #swagger_responses_hash_for_method(method) ⇒ Object
-
#tag_name_for_resource(resource) ⇒ Object
————————————————————————– Create a tag description for a described resource ————————————————————————–.
- #warn(warning_num, msg) ⇒ Object
- #warn_added_missing_slash(path) ⇒ Object
- #warn_hash_without_internal_typespec(param_name) ⇒ Object
- #warn_inferring_boolean(name) ⇒ Object
- #warn_missing_method_summary ⇒ Object
- #warn_no_return_codes_specified ⇒ Object
- #warn_optional_param_in_path(param_name) ⇒ Object
- #warn_optional_without_default_value(param_name) ⇒ Object
- #warn_param_ignored_in_form_data(param_name) ⇒ Object
- #warn_path_parameter_not_described(name, path) ⇒ Object
Constructor Details
#initialize(apipie) ⇒ SwaggerGenerator
Returns a new instance of SwaggerGenerator.
14 15 16 17 |
# File 'lib/apipie/swagger_generator.rb', line 14 def initialize(apipie) @apipie = apipie @issued_warnings = [] end |
Instance Attribute Details
#computed_interface_id ⇒ Object (readonly)
Returns the value of attribute computed_interface_id.
12 13 14 |
# File 'lib/apipie/swagger_generator.rb', line 12 def computed_interface_id @computed_interface_id end |
Instance Method Details
#add_headers_from_hash(swagger_params_array, headers) ⇒ Object
652 653 654 655 656 657 658 659 660 661 662 663 664 |
# File 'lib/apipie/swagger_generator.rb', line 652 def add_headers_from_hash(swagger_params_array, headers) swagger_headers = headers.map do |header| { name: header[:name], in: 'header', required: header[:options][:required], description: header[:description], type: header[:options][:type] || 'string' } end swagger_params_array.push(*swagger_headers) end |
#add_missing_params(method, path) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/apipie/swagger_generator.rb', line 431 def add_missing_params(method, path) param_names_from_method = method.params.map {|name, desc| name} missing = param_names_from_path(path) - param_names_from_method result = method.params missing.each do |name| warn_path_parameter_not_described(name, path) result[name.to_sym] = OpenStruct.new({ required: true, _gen_added_from_path: true, name: name, validator: Apipie::Validator::NumberValidator.new(nil), options: { in: "path" } }) end result end |
#add_params_from_hash(swagger_params_array, param_defs, prefix = nil, default_value_for_in = nil) ⇒ Object
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/apipie/swagger_generator.rb', line 667 def add_params_from_hash(swagger_params_array, param_defs, prefix=nil, default_value_for_in=nil) if default_value_for_in @default_value_for_param_in = default_value_for_in else if body_allowed_for_current_method @default_value_for_param_in = "formData" else @default_value_for_param_in = "query" end end param_defs.each do |name, desc| if !prefix.nil? name = "#{prefix}[#{name}]" end if swagger_param_type(desc) == "object" if desc.validator.params_ordered params_hash = Hash[desc.validator.params_ordered.map {|desc| [desc.name, desc]}] add_params_from_hash(swagger_params_array, params_hash, name) else warn_param_ignored_in_form_data(desc.name) end else param_entry = swagger_atomic_param(desc, false, name, false) if param_entry[:required] swagger_params_array.unshift(param_entry) else swagger_params_array.push(param_entry) end end end end |
#add_resource_description(resource_name, resource) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/apipie/swagger_generator.rb', line 182 def add_resource_description(resource_name, resource) if resource._full_description @tags << { name: tag_name_for_resource(resource), description: Apipie.app.translate(resource._full_description, @current_lang) } end end |
#add_resource_methods(resource_name, resource_defs) ⇒ Object
112 113 114 115 116 |
# File 'lib/apipie/swagger_generator.rb', line 112 def add_resource_methods(resource_name, resource_defs) resource_defs._methods.each do |apipie_method_name, apipie_method_defs| add_ruby_method(@paths, apipie_method_defs) end end |
#add_resources(resources) ⇒ Object
Engine interface methods
105 106 107 108 109 110 |
# File 'lib/apipie/swagger_generator.rb', line 105 def add_resources(resources) resources.each do |resource_name, resource_defs| add_resource_description(resource_name, resource_defs) add_resource_methods(resource_name, resource_defs) end end |
#add_ruby_method(paths, ruby_method) ⇒ Object
Create swagger definitions for a ruby method
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/apipie/swagger_generator.rb', line 195 def add_ruby_method(paths, ruby_method) if @only_method return unless ruby_method.method == @only_method else return if !ruby_method.show end for api in ruby_method.apis do # controller: ruby_method.resource.controller.name, path = swagger_path(api.path) paths[path] ||= {} methods = paths[path] @current_method = ruby_method @warnings_issued = false responses = swagger_responses_hash_for_method(ruby_method) if = @warnings_issued ? ['warnings issued'] : [] else = [] end op_id = swagger_op_id_for_path(api.http_method, api.path) include_op_id_in_computed_interface_id(op_id) method_key = api.http_method.downcase @current_http_method = method_key methods[method_key] = { tags: [tag_name_for_resource(ruby_method.resource)] + + ruby_method.tag_list., consumes: params_in_body? ? ['application/json'] : ['application/x-www-form-urlencoded', 'multipart/form-data'], operationId: op_id, summary: Apipie.app.translate(api.short_description, @current_lang), parameters: swagger_params_array_for_method(ruby_method, api.path), responses: responses, description: ruby_method.full_description } if methods[method_key][:summary].nil? methods[method_key].delete(:summary) warn_missing_method_summary end end end |
#body_allowed_for_current_method ⇒ Object
swagger “Params” block generation
612 613 614 |
# File 'lib/apipie/swagger_generator.rb', line 612 def body_allowed_for_current_method !(['get', 'head'].include?(@current_http_method)) end |
#gen_referenced_block_from_params_array(name, params_array, allow_nulls = false) ⇒ Object
547 548 549 550 551 552 553 554 555 |
# File 'lib/apipie/swagger_generator.rb', line 547 def gen_referenced_block_from_params_array(name, params_array, allow_nulls=false) return ref_to(:name) if @definitions.key(:name) schema_obj = json_schema_obj_from_params_array(params_array, allow_nulls) return nil if schema_obj.nil? @definitions[name.to_sym] = schema_obj ref_to(name.to_sym) end |
#generate_from_resources(version, resources, method_name, lang, clear_warnings = false) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/apipie/swagger_generator.rb', line 36 def generate_from_resources(version, resources, method_name, lang, clear_warnings=false) init_swagger_vars(version, lang, clear_warnings) @lang = lang @only_method = method_name add_resources(resources) @swagger[:info]["x-computed-id"] = @computed_interface_id if Apipie.configuration.swagger_generate_x_computed_id_field? return @swagger end |
#include_op_id_in_computed_interface_id(op_id) ⇒ Object
the @computed_interface_id is a number that is uniquely derived from the list of operations added to the swagger definition (in an order-dependent way). it can be used for regression testing, allowing some differentiation between changes that result from changes to the input and those that result from changes to the generation algorithms. note that at the moment, this only takes operation ids into account, and ignores parameter definitions, so it’s only partially useful.
169 170 171 |
# File 'lib/apipie/swagger_generator.rb', line 169 def include_op_id_in_computed_interface_id(op_id) @computed_interface_id = Zlib::crc32("#{@computed_interface_id} #{op_id}") if Apipie.configuration.swagger_generate_x_computed_id_field? end |
#include_warning_tags? ⇒ Boolean
31 32 33 |
# File 'lib/apipie/swagger_generator.rb', line 31 def Apipie.configuration. end |
#info(msg) ⇒ Object
157 158 159 |
# File 'lib/apipie/swagger_generator.rb', line 157 def info(msg) print "--- INFO: [#{ruby_name_for_method(@current_method)}] -- #{msg}\n" end |
#init_swagger_vars(version, lang, clear_warnings = false) ⇒ Object
Initialization
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/apipie/swagger_generator.rb', line 52 def init_swagger_vars(version, lang, clear_warnings=false) # docs = { # :name => Apipie.configuration.app_name, # :info => Apipie.app_info(version, lang), # :copyright => Apipie.configuration.copyright, # :doc_url => Apipie.full_url(url_args), # :api_url => Apipie.api_base_url(version), # :resources => _resources # } @swagger = { swagger: '2.0', info: { title: "#{Apipie.configuration.app_name}", description: "#{Apipie.app_info(version, lang)}#{Apipie.configuration.copyright}", version: "#{version}", "x-copyright" => Apipie.configuration.copyright, }, basePath: Apipie.api_base_url(version), consumes: [], paths: {}, definitions: {}, tags: [], } if Apipie.configuration.swagger_api_host @swagger[:host] = Apipie.configuration.swagger_api_host end if params_in_body? @swagger[:consumes] = ['application/json'] @swagger[:info][:title] += " (params in:body)" else @swagger[:consumes] = ['application/x-www-form-urlencoded', 'multipart/form-data'] @swagger[:info][:title] += " (params in:formData)" end @paths = @swagger[:paths] @definitions = @swagger[:definitions] @tags = @swagger[:tags] @issued_warnings = [] if clear_warnings || @issued_warnings.nil? @computed_interface_id = 0 @current_lang = lang end |
#json_schema_for_method_response(method, return_code, allow_nulls) ⇒ Object
Responses
345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/apipie/swagger_generator.rb', line 345 def json_schema_for_method_response(method, return_code, allow_nulls) @definitions = {} for response in method.returns if response.code.to_s == return_code.to_s schema = response_schema(response, allow_nulls) if response.code.to_s == return_code.to_s schema[:definitions] = @definitions if @definitions != {} return schema end end nil end |
#json_schema_for_self_describing_class(cls, allow_nulls) ⇒ Object
357 358 359 360 |
# File 'lib/apipie/swagger_generator.rb', line 357 def json_schema_for_self_describing_class(cls, allow_nulls) adapter = ResponseDescriptionAdapter.from_self_describing_class(cls) response_schema(adapter, allow_nulls) end |
#json_schema_obj_from_params_array(params_array, allow_nulls = false) ⇒ Object
536 537 538 539 540 541 542 543 544 545 |
# File 'lib/apipie/swagger_generator.rb', line 536 def json_schema_obj_from_params_array(params_array, allow_nulls = false) (param_defs, required_params) = json_schema_param_defs_from_params_array(params_array, allow_nulls) result = {type: "object"} result[:properties] = param_defs result[:additionalProperties] = false unless Apipie.configuration.swagger_allow_additional_properties_in_response result[:required] = required_params if required_params.length > 0 param_defs.length > 0 ? result : nil end |
#json_schema_param_defs_from_params_array(params_array, allow_nulls = false) ⇒ Object
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/apipie/swagger_generator.rb', line 557 def json_schema_param_defs_from_params_array(params_array, allow_nulls = false) param_defs = {} required_params = [] params_array ||= [] for param_desc in params_array if !param_desc.respond_to?(:required) # pp param_desc raise ("unexpected param_desc format") end required_params.push(param_desc.name.to_sym) if param_desc.required param_type = swagger_param_type(param_desc) if param_type == "object" && param_desc.validator.params_ordered schema = json_schema_obj_from_params_array(param_desc.validator.params_ordered, allow_nulls) if param_desc.additional_properties schema[:additionalProperties] = true end if param_desc.is_array? new_schema = { type: 'array', items: schema } schema = new_schema end if allow_nulls # ideally we would write schema[:type] = ["object", "null"] # but due to a bug in the json-schema gem, we need to use anyOf # see https://github.com/ruby-json-schema/json-schema/issues/404 new_schema = { anyOf: [schema, {type: "null"}] } schema = new_schema end param_defs[param_desc.name.to_sym] = schema if !schema.nil? else param_defs[param_desc.name.to_sym] = swagger_atomic_param(param_desc, true, nil, allow_nulls) end end [param_defs, required_params] end |
#lookup ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/apipie/swagger_generator.rb', line 292 def lookup @lookup ||= { numeric: "number", hash: "object", array: "array", # see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types integer: SwaggerTypeWithFormat.new("integer", "int32"), long: SwaggerTypeWithFormat.new("integer", "int64"), number: SwaggerTypeWithFormat.new("number", nil), # here just for completeness float: SwaggerTypeWithFormat.new("number", "float"), double: SwaggerTypeWithFormat.new("number", "double"), string: SwaggerTypeWithFormat.new("string", nil), # here just for completeness byte: SwaggerTypeWithFormat.new("string", "byte"), binary: SwaggerTypeWithFormat.new("string", "binary"), boolean: SwaggerTypeWithFormat.new("boolean", nil), # here just for completeness date: SwaggerTypeWithFormat.new("string", "date"), dateTime: SwaggerTypeWithFormat.new("string", "date-time"), password: SwaggerTypeWithFormat.new("string", "password"), } end |
#param_names_from_path(path) ⇒ Object
Auto-insertion of parameters that are implicitly defined in the path
425 426 427 428 429 |
# File 'lib/apipie/swagger_generator.rb', line 425 def param_names_from_path(path) path.scan(/:(\w+)/).map do |ar| ar[0].to_sym end end |
#params_in_body? ⇒ Boolean
19 20 21 |
# File 'lib/apipie/swagger_generator.rb', line 19 def params_in_body? Apipie.configuration.swagger_content_type_input == :json end |
#params_in_body_use_reference? ⇒ Boolean
23 24 25 |
# File 'lib/apipie/swagger_generator.rb', line 23 def params_in_body_use_reference? Apipie.configuration.swagger_json_input_uses_refs end |
#ref_to(name) ⇒ Object
JSON schema and referenced-object generation
531 532 533 |
# File 'lib/apipie/swagger_generator.rb', line 531 def ref_to(name) "#/definitions/#{name}" end |
#remove_colons(str) ⇒ Object
258 259 260 |
# File 'lib/apipie/swagger_generator.rb', line 258 def remove_colons(str) str.gsub(":", "_") end |
#response_schema(response, allow_nulls = false) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/apipie/swagger_generator.rb', line 362 def response_schema(response, allow_nulls=false) begin # no need to warn about "missing default value for optional param" when processing response definitions prev_value = @disable_default_value_warning @disable_default_value_warning = true if responses_use_reference? && response.typename schema = {"$ref" => gen_referenced_block_from_params_array(swagger_id_for_typename(response.typename), response.params_ordered, allow_nulls)} else schema = json_schema_obj_from_params_array(response.params_ordered, allow_nulls) end ensure @disable_default_value_warning = prev_value end if response.is_array? && schema schema = { type: allow_nulls ? ["array","null"] : "array", items: schema } end if response.allow_additional_properties schema[:additionalProperties] = true end schema end |
#responses_use_reference? ⇒ Boolean
27 28 29 |
# File 'lib/apipie/swagger_generator.rb', line 27 def responses_use_reference? Apipie.configuration.swagger_responses_use_refs? end |
#ruby_name_for_method(method) ⇒ Object
Logging, debugging and regression-testing utilities
123 124 125 126 |
# File 'lib/apipie/swagger_generator.rb', line 123 def ruby_name_for_method(method) return "<no method>" if method.nil? method.resource.controller.name + "#" + method.method end |
#save_field(entry, openapi_key, v, apipie_key = openapi_key, translate = false) ⇒ Object
458 459 460 461 462 463 464 465 466 |
# File 'lib/apipie/swagger_generator.rb', line 458 def save_field(entry, openapi_key, v, apipie_key=openapi_key, translate=false) if v.key?(apipie_key) if translate entry[openapi_key] = Apipie.app.translate(v[apipie_key], @current_lang) else entry[openapi_key] = v[apipie_key] end end end |
#swagger_atomic_param(param_desc, in_schema, name, allow_nulls) ⇒ Object
The core routine for creating a swagger parameter definition block. The output is slightly different when the parameter is inside a schema block.
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/apipie/swagger_generator.rb', line 457 def swagger_atomic_param(param_desc, in_schema, name, allow_nulls) def save_field(entry, openapi_key, v, apipie_key=openapi_key, translate=false) if v.key?(apipie_key) if translate entry[openapi_key] = Apipie.app.translate(v[apipie_key], @current_lang) else entry[openapi_key] = v[apipie_key] end end end swagger_def = {} swagger_def[:name] = name if !name.nil? swg_param_type = swagger_param_type(param_desc) swagger_def[:type] = swg_param_type.to_s if (swg_param_type.is_a? SwaggerTypeWithFormat) && !swg_param_type.str_format.nil? swagger_def[:format] = swg_param_type.str_format end if swagger_def[:type] == "array" swagger_def[:items] = {type: "string"} end if swagger_def[:type] == "enum" swagger_def[:type] = "string" swagger_def[:enum] = param_desc.validator.values end if swagger_def[:type] == "object" # we only get here if there is no specification of properties for this object swagger_def[:additionalProperties] = true warn_hash_without_internal_typespec(param_desc.name) end if param_desc.is_array? new_swagger_def = { items: swagger_def, type: 'array' } swagger_def = new_swagger_def if allow_nulls swagger_def[:type] = [swagger_def[:type], "null"] end end if allow_nulls swagger_def[:type] = [swagger_def[:type], "null"] end if !in_schema swagger_def[:in] = param_desc..fetch(:in, @default_value_for_param_in) swagger_def[:required] = param_desc.required if param_desc.required end save_field(swagger_def, :description, param_desc., :desc, true) unless param_desc.[:desc].nil? save_field(swagger_def, :default, param_desc., :default_value) if param_desc.respond_to?(:_gen_added_from_path) && !param_desc.required warn_optional_param_in_path(param_desc.name) swagger_def[:required] = true end if !swagger_def[:required] && !swagger_def.key?(:default) warn_optional_without_default_value(param_desc.name) unless @disable_default_value_warning end swagger_def end |
#swagger_id_for_typename(typename) ⇒ Object
266 267 268 |
# File 'lib/apipie/swagger_generator.rb', line 266 def swagger_id_for_typename(typename) typename end |
#swagger_op_id_for_method(method) ⇒ Object
262 263 264 |
# File 'lib/apipie/swagger_generator.rb', line 262 def swagger_op_id_for_method(method) remove_colons method.resource.controller.name + "::" + method.method end |
#swagger_op_id_for_path(http_method, path) ⇒ Object
270 271 272 273 274 |
# File 'lib/apipie/swagger_generator.rb', line 270 def swagger_op_id_for_path(http_method, path) # using lowercase http method, because the 'swagger-codegen' tool outputs # strange method names if the http method is in uppercase http_method.downcase + path.gsub(/\//,'_').gsub(/:(\w+)/, '\1').gsub(/_$/,'') end |
#swagger_param_type(param_desc) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/apipie/swagger_generator.rb', line 315 def swagger_param_type(param_desc) if param_desc.nil? raise("problem") end v = param_desc.validator if v.nil? return "string" end if v.class == Apipie::Validator::EnumValidator || (v.respond_to?(:is_enum?) && v.is_enum?) if v.values - [true, false] == [] && [true, false] - v.values == [] warn_inferring_boolean(param_desc.name) return "boolean" else return "enum" end elsif v.class == Apipie::Validator::HashValidator # pp v end return lookup[v.expected_type.to_sym] || v.expected_type end |
#swagger_params_array_for_method(method, path) ⇒ Object
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
# File 'lib/apipie/swagger_generator.rb', line 616 def swagger_params_array_for_method(method, path) swagger_result = [] all_params_hash = add_missing_params(method, path) body_param_defs_array = all_params_hash.map {|k, v| v if !param_names_from_path(path).include?(k)}.select{|v| !v.nil?} body_param_defs_hash = all_params_hash.select {|k, v| v if !param_names_from_path(path).include?(k)} path_param_defs_hash = all_params_hash.select {|k, v| v if param_names_from_path(path).include?(k)} path_param_defs_hash.each{|name,desc| desc.required = true} add_params_from_hash(swagger_result, path_param_defs_hash, nil, "path") if params_in_body? && body_allowed_for_current_method if params_in_body_use_reference? swagger_schema_for_body = {"$ref" => gen_referenced_block_from_params_array("#{swagger_op_id_for_method(method)}_input", body_param_defs_array)} else swagger_schema_for_body = json_schema_obj_from_params_array(body_param_defs_array) end swagger_body_param = { name: 'body', in: 'body', schema: swagger_schema_for_body } swagger_result.push(swagger_body_param) if !swagger_schema_for_body.nil? else add_params_from_hash(swagger_result, body_param_defs_hash) end add_headers_from_hash(swagger_result, method.headers) if method.headers.present? swagger_result end |
#swagger_path(str) ⇒ Object
Utilities for conversion of ruby syntax to swagger syntax
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/apipie/swagger_generator.rb', line 247 def swagger_path(str) str = str.gsub(/:(\w+)/, '{\1}') str = str.gsub(/\/$/, '') if str[0] != '/' warn_added_missing_slash(str) str = '/' + str end str end |
#swagger_responses_hash_for_method(method) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/apipie/swagger_generator.rb', line 392 def swagger_responses_hash_for_method(method) result = {} for error in method.errors error_block = {description: Apipie.app.translate(error.description, @current_lang)} result[error.code] = error_block end for response in method.returns swagger_response_block = { description: response.description } schema = response_schema(response) swagger_response_block[:schema] = schema if schema result[response.code] = swagger_response_block end if result.length == 0 warn_no_return_codes_specified result[200] = {description: 'ok'} end result end |
#tag_name_for_resource(resource) ⇒ Object
Create a tag description for a described resource
177 178 179 180 |
# File 'lib/apipie/swagger_generator.rb', line 177 def tag_name_for_resource(resource) # resource.controller resource._id end |
#warn(warning_num, msg) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/apipie/swagger_generator.rb', line 139 def warn(warning_num, msg) suppress = Apipie.configuration.swagger_suppress_warnings return if suppress == true return if suppress.is_a?(Array) && suppress.include?(warning_num) method_id = ruby_name_for_method(@current_method) warning_id = "#{method_id}#{warning_num}#{msg}" if @issued_warnings.include?(warning_id) # Already issued this warning for the current method return end print "WARNING (#{warning_num}): [#{method_id}] -- #{msg}\n" @issued_warnings.push(warning_id) @warnings_issued = true end |
#warn_added_missing_slash(path) ⇒ Object
130 |
# File 'lib/apipie/swagger_generator.rb', line 130 def warn_added_missing_slash(path) warn 101,"added missing / at beginning of path: #{path}"; end |
#warn_hash_without_internal_typespec(param_name) ⇒ Object
132 |
# File 'lib/apipie/swagger_generator.rb', line 132 def warn_hash_without_internal_typespec(param_name) warn 103,"the parameter :#{param_name} is a generic Hash without an internal type specification"; end |
#warn_inferring_boolean(name) ⇒ Object
137 |
# File 'lib/apipie/swagger_generator.rb', line 137 def warn_inferring_boolean(name) warn 108,"the parameter [#{name}] is Enum with [true,false] values. Inferring 'boolean'"; end |
#warn_missing_method_summary ⇒ Object
129 |
# File 'lib/apipie/swagger_generator.rb', line 129 def warn_missing_method_summary() warn 100, "missing short description for method"; end |
#warn_no_return_codes_specified ⇒ Object
131 |
# File 'lib/apipie/swagger_generator.rb', line 131 def warn_no_return_codes_specified() warn 102,"no return codes ('errors') specified"; end |
#warn_optional_param_in_path(param_name) ⇒ Object
133 |
# File 'lib/apipie/swagger_generator.rb', line 133 def warn_optional_param_in_path(param_name) warn 104, "the parameter :#{param_name} is 'in-path'. Ignoring 'not required' in DSL"; end |
#warn_optional_without_default_value(param_name) ⇒ Object
134 |
# File 'lib/apipie/swagger_generator.rb', line 134 def warn_optional_without_default_value(param_name) warn 105,"the parameter :#{param_name} is optional but default value is not specified (use :default_value => ...)"; end |
#warn_param_ignored_in_form_data(param_name) ⇒ Object
135 |
# File 'lib/apipie/swagger_generator.rb', line 135 def warn_param_ignored_in_form_data(param_name) warn 106,"ignoring param :#{param_name} -- cannot include Hash without fields in a formData specification"; end |
#warn_path_parameter_not_described(name, path) ⇒ Object
136 |
# File 'lib/apipie/swagger_generator.rb', line 136 def warn_path_parameter_not_described(name,path) warn 107,"the parameter :#{name} appears in the path #{path} but is not described"; end |