Class: Sinatra::SwaggerExposer::Configuration::SwaggerTypeProperty

Inherits:
Object
  • Object
show all
Includes:
SwaggerConfigurationUtilities, SwaggerParameterValidationHelper
Defined in:
lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb

Overview

A property of a type

Constant Summary collapse

OTHER_PROPERTIES =
[:example, :description, :format, :minLength, :maxLength, :default]
PROPERTIES =
[:type] + OTHER_PROPERTIES

Constants included from SwaggerParameterHelper

SwaggerParameterHelper::HOW_TO_PASS, SwaggerParameterHelper::HOW_TO_PASS_BODY, SwaggerParameterHelper::HOW_TO_PASS_HEADER, SwaggerParameterHelper::HOW_TO_PASS_PATH, SwaggerParameterHelper::HOW_TO_PASS_QUERY, SwaggerParameterHelper::PARAMS_DEFAULT, SwaggerParameterHelper::PARAMS_EXAMPLE, SwaggerParameterHelper::PARAMS_EXCLUSIVE_MAXIMUM, SwaggerParameterHelper::PARAMS_EXCLUSIVE_MINIMUM, SwaggerParameterHelper::PARAMS_FORMAT, SwaggerParameterHelper::PARAMS_LIST, SwaggerParameterHelper::PARAMS_MAXIMUM, SwaggerParameterHelper::PARAMS_MAX_LENGTH, SwaggerParameterHelper::PARAMS_MINIMUM, SwaggerParameterHelper::PARAMS_MIN_LENGTH, SwaggerParameterHelper::PRIMITIVE_TYPES, SwaggerParameterHelper::PRIMITIVE_TYPES_FOR_NON_BODY, SwaggerParameterHelper::TYPE_ARRAY, SwaggerParameterHelper::TYPE_BOOLEAN, SwaggerParameterHelper::TYPE_BYTE, SwaggerParameterHelper::TYPE_DATE, SwaggerParameterHelper::TYPE_DATE_TIME, SwaggerParameterHelper::TYPE_DOUBLE, SwaggerParameterHelper::TYPE_FILE, SwaggerParameterHelper::TYPE_FLOAT, SwaggerParameterHelper::TYPE_INTEGER, SwaggerParameterHelper::TYPE_LONG, SwaggerParameterHelper::TYPE_NUMBER, SwaggerParameterHelper::TYPE_PASSWORD, SwaggerParameterHelper::TYPE_STRING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SwaggerParameterValidationHelper

#check_boolean, #validate_length_parameter, #validate_length_parameters, #validate_limit_parameter, #validate_limit_parameters, #validate_params

Methods included from SwaggerConfigurationUtilities

#check_name, #get_type, #hash_to_swagger, #list_or_none, #ref_to_type, #type_to_s, #white_list_params

Constructor Details

#initialize(type_name, property_name, property_properties, known_types) ⇒ SwaggerTypeProperty

Returns a new instance of SwaggerTypeProperty.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb', line 22

def initialize(type_name, property_name, property_properties, known_types)
  @name = property_name

  unless property_properties.is_a? Hash
    raise SwaggerInvalidException.new("Property [#{property_name}] value [#{property_properties}] of [#{type_name}] should be a hash")
  end

  if property_properties.key? :type
    get_type(property_properties[:type], PRIMITIVE_TYPES + known_types)
  end

  white_list_params(property_properties, PROPERTIES)

  validate_params(@type, property_properties)

  @other_properties = property_properties.select do |key, value|
    OTHER_PROPERTIES.include? key
  end
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



14
15
16
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb', line 14

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb', line 14

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb', line 14

def type
  @type
end

Instance Method Details

#propertiesObject



42
43
44
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb', line 42

def properties
  @other_properties
end

#to_sObject



71
72
73
74
75
76
77
78
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb', line 71

def to_s
  {
    :name => @name,
    :type => @type,
    :items => @items,
    :other_properties => @other_properties,
  }.to_json
end

#to_swaggerObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sinatra/swagger-exposer/configuration/swagger-type-property.rb', line 46

def to_swagger
  result = @other_properties.clone

  if @type
    if @type == TYPE_ARRAY
      result[:type] = TYPE_ARRAY
      if @items
        if PRIMITIVE_TYPES.include? @items
          result[:items] = {:type => @items}
        else
          result[:items] = ref_to_type(@items)
        end
      end
    else
      if PRIMITIVE_TYPES.include? @type
        result[:type] = @type
      else
        result['$ref'] = "#/definitions/#{@type}"
      end
    end
  end

  result
end