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
# File 'lib/openapi_parameters/parameter.rb', line 8

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

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



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

def definition
  @definition
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#allow_reserved?Boolean

Returns:

  • (Boolean)


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

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

#array?Boolean

Returns:

  • (Boolean)


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

def array?
  type == 'array'
end

#deep_object?Boolean

Returns:

  • (Boolean)


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

def deep_object?
  @is_deep_object
end

#deprecated?Boolean

Returns:

  • (Boolean)


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

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

#explode?Boolean

Returns:

  • (Boolean)


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

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”.



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

def location
  definition['in']
end

#media_typeObject



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

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

#object?Boolean

Returns:

  • (Boolean)


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

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

#primitive?Boolean

Returns:

  • (Boolean)


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

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

#required?Boolean

Returns:

  • (Boolean)


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

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

  definition['required'] == true
end

#schemaObject



28
29
30
31
32
# File 'lib/openapi_parameters/parameter.rb', line 28

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

  definition['schema']
end

#styleObject



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

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

  DEFAULT_STYLE.fetch(location)
end

#typeObject



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

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