Class: OpenapiParameters::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_parameters/parameter.rb

Overview

Represents a parameter in an OpenAPI operation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ Parameter

Returns a new instance of Parameter.

Parameters:

  • definition (Hash)

    The parameter definition. A string keyed Hash.



8
9
10
11
12
13
14
# File 'lib/openapi_parameters/parameter.rb', line 8

def initialize(definition)
  @definition = definition
  @name = definition['name']
  @is_deep_object = style == 'deepObject'
  @converter = Converters[schema]
  check_supported!
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



16
17
18
# File 'lib/openapi_parameters/parameter.rb', line 16

def definition
  @definition
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/openapi_parameters/parameter.rb', line 16

def name
  @name
end

Instance Method Details

#allow_reserved?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/openapi_parameters/parameter.rb', line 75

def allow_reserved?
  definition['allowReserved'] == true
end

#array?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/openapi_parameters/parameter.rb', line 51

def array?
  type == 'array'
end

#convert(value) ⇒ Object



18
19
20
# File 'lib/openapi_parameters/parameter.rb', line 18

def convert(value)
  @converter.call(value)
end

#deep_object?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/openapi_parameters/parameter.rb', line 22

def deep_object?
  @is_deep_object
end

#deprecated?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/openapi_parameters/parameter.rb', line 71

def deprecated?
  definition['deprecated'] == true
end

#explode?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/openapi_parameters/parameter.rb', line 79

def explode?
  return definition['explode'] if definition.key?('explode')
  return true if style == 'form'

  false
end

#locationString Also known as: in

Returns The location of the parameter in the request, “path”, “query”, “header” or “cookie”.

Returns:

  • (String)

    The location of the parameter in the request, “path”, “query”, “header” or “cookie”.



27
28
29
# File 'lib/openapi_parameters/parameter.rb', line 27

def location
  definition['in']
end

#media_typeObject



39
40
41
# File 'lib/openapi_parameters/parameter.rb', line 39

def media_type
  definition['content']&.keys&.first
end

#object?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/openapi_parameters/parameter.rb', line 55

def object?
  type == 'object' || style == 'deepObject' || schema&.key?('properties')
end

#primitive?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/openapi_parameters/parameter.rb', line 47

def primitive?
  type != 'object' && type != 'array'
end

#required?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/openapi_parameters/parameter.rb', line 65

def required?
  return true if location == 'path'

  definition['required'] == true
end

#schemaObject



33
34
35
36
37
# File 'lib/openapi_parameters/parameter.rb', line 33

def schema
  return definition.dig('content', media_type, 'schema') if media_type

  definition['schema']
end

#styleObject



59
60
61
62
63
# File 'lib/openapi_parameters/parameter.rb', line 59

def style
  return definition['style'] if definition['style']

  DEFAULT_STYLE.fetch(location)
end

#typeObject



43
44
45
# File 'lib/openapi_parameters/parameter.rb', line 43

def type
  schema && schema['type']
end