Class: Jsapi::Meta::Parameter::Model
- Defined in:
- lib/jsapi/meta/parameter/model.rb
Instance Method Summary collapse
-
#allow_empty_value? ⇒ Boolean
Returns true if empty values are allowed as specified by OpenAPI, false otherwise.
-
#deprecated ⇒ Object
:attr: deprecated Specifies whether or not the parameter is deprecated.
-
#description ⇒ Object
:attr: description The optional description of the parameter.
-
#examples ⇒ Object
:attr_reader: examples The optional examples.
-
#in ⇒ Object
:attr: in The location of the parameter.
-
#initialize(name, keywords = {}) ⇒ Model
constructor
Creates a new parameter.
-
#name ⇒ Object
:attr_reader: name The name of the parameter.
-
#required? ⇒ Boolean
Returns true if it is required as specified by JSON Schema, false otherwise.
-
#schema ⇒ Object
:attr_reader: schema The Schema of the parameter.
-
#to_openapi(version, definitions) ⇒ Object
Returns an array of hashes representing the OpenAPI parameter objects.
Methods inherited from Base
#inspect, #reference?, #resolve
Methods included from Attributes::ClassMethods
Constructor Details
#initialize(name, keywords = {}) ⇒ Model
Creates a new parameter.
Raises an ArgumentError
if name
is blank.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jsapi/meta/parameter/model.rb', line 47 def initialize(name, keywords = {}) raise ArgumentError, "parameter name can't be blank" if name.blank? keywords = keywords.dup super(keywords.extract!(:deprecated, :description, :examples, :in)) add_example(value: keywords.delete(:example)) if keywords.key?(:example) @name = name.to_s @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.
61 62 63 |
# File 'lib/jsapi/meta/parameter/model.rb', line 61 def allow_empty_value? schema.existence <= Existence::ALLOW_EMPTY && self.in != 'path' end |
#deprecated ⇒ Object
:attr: deprecated Specifies whether or not the parameter is deprecated.
10 |
# File 'lib/jsapi/meta/parameter/model.rb', line 10 attribute :deprecated, values: [true, false] |
#description ⇒ Object
:attr: description The optional description of the parameter.
15 |
# File 'lib/jsapi/meta/parameter/model.rb', line 15 attribute :description, String |
#examples ⇒ Object
:attr_reader: examples The optional examples.
20 |
# File 'lib/jsapi/meta/parameter/model.rb', line 20 attribute :examples, { String => Example }, default_key: 'default' |
#in ⇒ Object
:attr: in The location of the parameter. Possible values are:
-
"path"
-
"query"
The default location is "query"
.
30 |
# File 'lib/jsapi/meta/parameter/model.rb', line 30 attribute :in, String, values: %w[path query], default: 'query' |
#name ⇒ Object
:attr_reader: name The name of the parameter.
35 |
# File 'lib/jsapi/meta/parameter/model.rb', line 35 attribute :name, writer: false |
#required? ⇒ Boolean
Returns true if it is required as specified by JSON Schema, false otherwise.
67 68 69 |
# File 'lib/jsapi/meta/parameter/model.rb', line 67 def required? schema.existence > Existence::ALLOW_OMITTED || self.in == 'path' end |
#schema ⇒ Object
:attr_reader: schema The Schema of the parameter.
40 |
# File 'lib/jsapi/meta/parameter/model.rb', line 40 attribute :schema, writer: false |
#to_openapi(version, definitions) ⇒ Object
Returns an array of hashes representing the OpenAPI parameter objects.
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/jsapi/meta/parameter/model.rb', line 72 def to_openapi(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 [ 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) # NOTE: collectionFormat is replaced by style and explode. # The default values for query parameters are: # - style: 'form' # - explode: true } end.compact ] end end |