Class: Jsapi::Meta::Parameter::Model

Inherits:
Base::Model show all
Includes:
OpenAPI::Extensions, ToOpenAPI
Defined in:
lib/jsapi/meta/parameter/model.rb

Overview

Specifies a parameter.

Constant Summary

Constants included from Base::Attributes

Base::Attributes::DEFAULT_ARRAY, Base::Attributes::DEFAULT_HASH

Instance Method Summary collapse

Methods included from ToOpenAPI

#to_openapi

Methods included from OpenAPI::Extensions

#add_openapi_extension, #openapi_extensions, #openapi_extensions=

Methods inherited from Base::Model

#inspect, #merge!, #reference?, #resolve

Methods included from Base::Attributes

#attribute, #attribute_names

Constructor Details

#initialize(name, keywords = {}) ⇒ Model

Creates a new parameter.

Raises an ArgumentError if name is blank.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsapi/meta/parameter/model.rb', line 52

def initialize(name, keywords = {})
  raise ArgumentError, "parameter name can't be blank" if name.blank?

  @name = name.to_s

  keywords = keywords.dup
  super(keywords.extract!(:deprecated, :description, :examples, :in, :openapi_extensions))

  add_example(value: keywords.delete(:example)) if keywords.key?(:example)
  keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)

  @schema = Schema.new(keywords)
end

Instance Method Details

#allow_empty_value?Boolean

Returns true if empty values are allowed as specified by OpenAPI, false otherwise.

Returns:

  • (Boolean)


68
69
70
# File 'lib/jsapi/meta/parameter/model.rb', line 68

def allow_empty_value?
  schema.existence <= Existence::ALLOW_EMPTY && self.in != 'path'
end

#deprecatedObject

:attr: deprecated Specifies whether or not the parameter is deprecated.



16
# File 'lib/jsapi/meta/parameter/model.rb', line 16

attribute :deprecated, values: [true, false]

#descriptionObject

:attr: description The description of the parameter.



21
# File 'lib/jsapi/meta/parameter/model.rb', line 21

attribute :description, String

#examplesObject

:attr_reader: examples The examples.



26
# File 'lib/jsapi/meta/parameter/model.rb', line 26

attribute :examples, { String => Example }, default_key: 'default'

#inObject

:attr: in The location of the parameter. Possible values are:

  • "header"

  • "path"

  • "query"

The default location is "query".



37
# File 'lib/jsapi/meta/parameter/model.rb', line 37

attribute :in, String, values: %w[header path query], default: 'query'

#nameObject

:attr_reader: name The name of the parameter.



42
# File 'lib/jsapi/meta/parameter/model.rb', line 42

attribute :name, read_only: true

#required?Boolean

Returns true if it is required as specified by JSON Schema, false otherwise.

Returns:

  • (Boolean)


74
75
76
# File 'lib/jsapi/meta/parameter/model.rb', line 74

def required?
  schema.existence > Existence::ALLOW_OMITTED || self.in == 'path'
end

#schemaObject

:attr_reader: schema The Schema of the parameter.



47
# File 'lib/jsapi/meta/parameter/model.rb', line 47

attribute :schema, read_only: true

#to_openapi_parameters(version, definitions) ⇒ Object

Returns an array of hashes representing the OpenAPI parameter objects.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jsapi/meta/parameter/model.rb', line 79

def to_openapi_parameters(version, definitions)
  version = OpenAPI::Version.from(version)
  schema = self.schema.resolve(definitions)

  if schema.object?
    explode_object_parameter(
      name,
      schema,
      version,
      definitions,
      required: required?,
      deprecated: deprecated?
    )
  else
    parameter_name = schema.array? ? "#{name}[]" : name
    [
      with_openapi_extensions(
        if version.major == 2
          {
            name: parameter_name,
            in: self.in,
            description: description,
            required: required?.presence,
            allowEmptyValue: allow_empty_value?.presence,
            collectionFormat: ('multi' if schema.array?)
          }.merge(schema.to_openapi(version))
        else
          {
            name: parameter_name,
            in: self.in,
            description: description,
            required: required?.presence,
            allowEmptyValue: allow_empty_value?.presence,
            deprecated: deprecated?.presence,
            schema: schema.to_openapi(version),
            examples: examples.transform_values(&:to_openapi).presence

            # NOTE: collectionFormat is replaced by style and explode.
            #       The default values for query parameters are:
            #       - style: 'form'
            #       - explode: true
          }
        end
      )
    ]
  end
end