manticore-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +39 -0
  3. data/.openapi-generator/FILES +149 -0
  4. data/.openapi-generator/VERSION +1 -0
  5. data/.openapi-generator-ignore +5 -0
  6. data/.rspec +2 -0
  7. data/.rubocop.yml +160 -0
  8. data/Gemfile +12 -0
  9. data/Gemfile.lock +103 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +136 -0
  12. data/Rakefile +10 -0
  13. data/docs/AggComposite.md +20 -0
  14. data/docs/AggCompositeSource.md +18 -0
  15. data/docs/AggCompositeTerm.md +18 -0
  16. data/docs/AggTerms.md +20 -0
  17. data/docs/Aggregation.md +22 -0
  18. data/docs/AutocompleteRequest.md +22 -0
  19. data/docs/BoolFilter.md +22 -0
  20. data/docs/BulkResponse.md +26 -0
  21. data/docs/DeleteDocumentRequest.md +24 -0
  22. data/docs/DeleteResponse.md +26 -0
  23. data/docs/ErrorResponse.md +20 -0
  24. data/docs/FulltextFilter.md +24 -0
  25. data/docs/GeoDistance.md +24 -0
  26. data/docs/GeoDistanceLocationAnchor.md +20 -0
  27. data/docs/Highlight.md +62 -0
  28. data/docs/HighlightAllOfFields.md +15 -0
  29. data/docs/HighlightFieldOption.md +26 -0
  30. data/docs/HitsHits.md +22 -0
  31. data/docs/IndexApi.md +401 -0
  32. data/docs/InsertDocumentRequest.md +24 -0
  33. data/docs/Join.md +24 -0
  34. data/docs/JoinCond.md +22 -0
  35. data/docs/JoinOn.md +22 -0
  36. data/docs/KnnQuery.md +28 -0
  37. data/docs/Match.md +22 -0
  38. data/docs/MatchAll.md +18 -0
  39. data/docs/PercolateRequest.md +18 -0
  40. data/docs/PercolateRequestQuery.md +18 -0
  41. data/docs/QueryFilter.md +34 -0
  42. data/docs/Range.md +24 -0
  43. data/docs/ReplaceDocumentRequest.md +18 -0
  44. data/docs/ResponseError.md +49 -0
  45. data/docs/ResponseErrorDetails.md +22 -0
  46. data/docs/SearchApi.md +204 -0
  47. data/docs/SearchQuery.md +36 -0
  48. data/docs/SearchRequest.md +46 -0
  49. data/docs/SearchResponse.md +30 -0
  50. data/docs/SearchResponseHits.md +24 -0
  51. data/docs/SourceRules.md +20 -0
  52. data/docs/SqlResponse.md +49 -0
  53. data/docs/SuccessResponse.md +28 -0
  54. data/docs/UpdateDocumentRequest.md +26 -0
  55. data/docs/UpdateResponse.md +24 -0
  56. data/docs/UtilsApi.md +76 -0
  57. data/lib/manticore/client/api/index_api.rb +442 -0
  58. data/lib/manticore/client/api/search_api.rb +232 -0
  59. data/lib/manticore/client/api/utils_api.rb +93 -0
  60. data/lib/manticore/client/api_client.rb +437 -0
  61. data/lib/manticore/client/api_error.rb +58 -0
  62. data/lib/manticore/client/configuration.rb +385 -0
  63. data/lib/manticore/client/models/agg_composite.rb +233 -0
  64. data/lib/manticore/client/models/agg_composite_source.rb +238 -0
  65. data/lib/manticore/client/models/agg_composite_term.rb +239 -0
  66. data/lib/manticore/client/models/agg_terms.rb +249 -0
  67. data/lib/manticore/client/models/aggregation.rb +240 -0
  68. data/lib/manticore/client/models/autocomplete_request.rb +276 -0
  69. data/lib/manticore/client/models/bool_filter.rb +247 -0
  70. data/lib/manticore/client/models/bulk_response.rb +264 -0
  71. data/lib/manticore/client/models/delete_document_request.rb +269 -0
  72. data/lib/manticore/client/models/delete_response.rb +262 -0
  73. data/lib/manticore/client/models/error_response.rb +250 -0
  74. data/lib/manticore/client/models/fulltext_filter.rb +252 -0
  75. data/lib/manticore/client/models/geo_distance.rb +306 -0
  76. data/lib/manticore/client/models/geo_distance_location_anchor.rb +232 -0
  77. data/lib/manticore/client/models/highlight.rb +525 -0
  78. data/lib/manticore/client/models/highlight_all_of_fields.rb +105 -0
  79. data/lib/manticore/client/models/highlight_field_option.rb +262 -0
  80. data/lib/manticore/client/models/hits_hits.rb +242 -0
  81. data/lib/manticore/client/models/insert_document_request.rb +286 -0
  82. data/lib/manticore/client/models/join.rb +327 -0
  83. data/lib/manticore/client/models/join_cond.rb +276 -0
  84. data/lib/manticore/client/models/join_on.rb +272 -0
  85. data/lib/manticore/client/models/knn_query.rb +307 -0
  86. data/lib/manticore/client/models/match.rb +290 -0
  87. data/lib/manticore/client/models/match_all.rb +262 -0
  88. data/lib/manticore/client/models/percolate_request.rb +238 -0
  89. data/lib/manticore/client/models/percolate_request_query.rb +238 -0
  90. data/lib/manticore/client/models/query_filter.rb +307 -0
  91. data/lib/manticore/client/models/range.rb +252 -0
  92. data/lib/manticore/client/models/replace_document_request.rb +239 -0
  93. data/lib/manticore/client/models/response_error.rb +105 -0
  94. data/lib/manticore/client/models/response_error_details.rb +261 -0
  95. data/lib/manticore/client/models/search_query.rb +316 -0
  96. data/lib/manticore/client/models/search_request.rb +382 -0
  97. data/lib/manticore/client/models/search_response.rb +281 -0
  98. data/lib/manticore/client/models/search_response_hits.rb +254 -0
  99. data/lib/manticore/client/models/source_rules.rb +236 -0
  100. data/lib/manticore/client/models/sql_response.rb +106 -0
  101. data/lib/manticore/client/models/success_response.rb +272 -0
  102. data/lib/manticore/client/models/update_document_request.rb +296 -0
  103. data/lib/manticore/client/models/update_response.rb +252 -0
  104. data/lib/manticore/client/version.rb +7 -0
  105. data/lib/manticore/client.rb +32 -0
  106. data/lib/manticore-client.rb +3 -0
  107. data/manticore-client.gemspec +34 -0
  108. data/spec/api/index_api_spec.rb +179 -0
  109. data/spec/api/search_api_spec.rb +94 -0
  110. data/spec/api/utils_api_spec.rb +100 -0
  111. data/spec/spec_helper.rb +92 -0
  112. data/spec/support/manticore_sql_helper.rb +33 -0
  113. metadata +218 -0
@@ -0,0 +1,252 @@
1
+ =begin
2
+ #Manticore Search Client
3
+
4
+ #Сlient for Manticore Search.
5
+
6
+ The version of the OpenAPI document: 5.0.0
7
+ Contact: info@manticoresearch.com
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.13.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'time'
15
+
16
+ module Manticore::Client
17
+ # Filter helper object defining the 'range' condition
18
+ class Range
19
+ attr_accessor :lt
20
+
21
+ attr_accessor :lte
22
+
23
+ attr_accessor :gt
24
+
25
+ attr_accessor :gte
26
+
27
+ # Attribute mapping from ruby-style variable name to JSON key.
28
+ def self.attribute_map
29
+ {
30
+ :'lt' => :'lt',
31
+ :'lte' => :'lte',
32
+ :'gt' => :'gt',
33
+ :'gte' => :'gte'
34
+ }
35
+ end
36
+
37
+ # Returns attribute mapping this model knows about
38
+ def self.acceptable_attribute_map
39
+ attribute_map
40
+ end
41
+
42
+ # Returns all the JSON keys this model knows about
43
+ def self.acceptable_attributes
44
+ acceptable_attribute_map.values
45
+ end
46
+
47
+ # Attribute type mapping.
48
+ def self.openapi_types
49
+ {
50
+ :'lt' => :'Object',
51
+ :'lte' => :'Object',
52
+ :'gt' => :'Object',
53
+ :'gte' => :'Object'
54
+ }
55
+ end
56
+
57
+ # List of attributes with nullable: true
58
+ def self.openapi_nullable
59
+ Set.new([
60
+ :'lt',
61
+ :'lte',
62
+ :'gt',
63
+ :'gte'
64
+ ])
65
+ end
66
+
67
+ # Initializes the object
68
+ # @param [Hash] attributes Model attributes in the form of hash
69
+ def initialize(attributes = {})
70
+ if (!attributes.is_a?(Hash))
71
+ fail ArgumentError, "The input argument (attributes) must be a hash in `Manticore::Client::Range` initialize method"
72
+ end
73
+
74
+ # check to see if the attribute exists and convert string to symbol for hash key
75
+ acceptable_attribute_map = self.class.acceptable_attribute_map
76
+ attributes = attributes.each_with_object({}) { |(k, v), h|
77
+ if (!acceptable_attribute_map.key?(k.to_sym))
78
+ fail ArgumentError, "`#{k}` is not a valid attribute in `Manticore::Client::Range`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
79
+ end
80
+ h[k.to_sym] = v
81
+ }
82
+
83
+ if attributes.key?(:'lt')
84
+ self.lt = attributes[:'lt']
85
+ end
86
+
87
+ if attributes.key?(:'lte')
88
+ self.lte = attributes[:'lte']
89
+ end
90
+
91
+ if attributes.key?(:'gt')
92
+ self.gt = attributes[:'gt']
93
+ end
94
+
95
+ if attributes.key?(:'gte')
96
+ self.gte = attributes[:'gte']
97
+ end
98
+ end
99
+
100
+ # Show invalid properties with the reasons. Usually used together with valid?
101
+ # @return Array for valid properties with the reasons
102
+ def list_invalid_properties
103
+ warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
104
+ invalid_properties = Array.new
105
+ invalid_properties
106
+ end
107
+
108
+ # Check to see if the all the properties in the model are valid
109
+ # @return true if the model is valid
110
+ def valid?
111
+ warn '[DEPRECATED] the `valid?` method is obsolete'
112
+ true
113
+ end
114
+
115
+ # Checks equality by comparing each attribute.
116
+ # @param [Object] Object to be compared
117
+ def ==(o)
118
+ return true if self.equal?(o)
119
+ self.class == o.class &&
120
+ lt == o.lt &&
121
+ lte == o.lte &&
122
+ gt == o.gt &&
123
+ gte == o.gte
124
+ end
125
+
126
+ # @see the `==` method
127
+ # @param [Object] Object to be compared
128
+ def eql?(o)
129
+ self == o
130
+ end
131
+
132
+ # Calculates hash code according to all attributes.
133
+ # @return [Integer] Hash code
134
+ def hash
135
+ [lt, lte, gt, gte].hash
136
+ end
137
+
138
+ # Builds the object from hash
139
+ # @param [Hash] attributes Model attributes in the form of hash
140
+ # @return [Object] Returns the model itself
141
+ def self.build_from_hash(attributes)
142
+ return nil unless attributes.is_a?(Hash)
143
+ attributes = attributes.transform_keys(&:to_sym)
144
+ transformed_hash = {}
145
+ openapi_types.each_pair do |key, type|
146
+ if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
147
+ transformed_hash["#{key}"] = nil
148
+ elsif type =~ /\AArray<(.*)>/i
149
+ # check to ensure the input is an array given that the attribute
150
+ # is documented as an array but the input is not
151
+ if attributes[attribute_map[key]].is_a?(Array)
152
+ transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
153
+ end
154
+ elsif !attributes[attribute_map[key]].nil?
155
+ transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
156
+ end
157
+ end
158
+ new(transformed_hash)
159
+ end
160
+
161
+ # Deserializes the data based on type
162
+ # @param string type Data type
163
+ # @param string value Value to be deserialized
164
+ # @return [Object] Deserialized data
165
+ def self._deserialize(type, value)
166
+ case type.to_sym
167
+ when :Time
168
+ Time.parse(value)
169
+ when :Date
170
+ Date.parse(value)
171
+ when :String
172
+ value.to_s
173
+ when :Integer
174
+ value.to_i
175
+ when :Float
176
+ value.to_f
177
+ when :Boolean
178
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
179
+ true
180
+ else
181
+ false
182
+ end
183
+ when :Object
184
+ # generic object (usually a Hash), return directly
185
+ value
186
+ when /\AArray<(?<inner_type>.+)>\z/
187
+ inner_type = Regexp.last_match[:inner_type]
188
+ value.map { |v| _deserialize(inner_type, v) }
189
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
190
+ k_type = Regexp.last_match[:k_type]
191
+ v_type = Regexp.last_match[:v_type]
192
+ {}.tap do |hash|
193
+ value.each do |k, v|
194
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
195
+ end
196
+ end
197
+ else # model
198
+ # models (e.g. Pet) or oneOf
199
+ klass = Manticore::Client.const_get(type)
200
+ klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
201
+ end
202
+ end
203
+
204
+ # Returns the string representation of the object
205
+ # @return [String] String presentation of the object
206
+ def to_s
207
+ to_hash.to_s
208
+ end
209
+
210
+ # to_body is an alias to to_hash (backward compatibility)
211
+ # @return [Hash] Returns the object in the form of hash
212
+ def to_body
213
+ to_hash
214
+ end
215
+
216
+ # Returns the object in the form of hash
217
+ # @return [Hash] Returns the object in the form of hash
218
+ def to_hash
219
+ hash = {}
220
+ self.class.attribute_map.each_pair do |attr, param|
221
+ value = self.send(attr)
222
+ if value.nil?
223
+ is_nullable = self.class.openapi_nullable.include?(attr)
224
+ next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
225
+ end
226
+
227
+ hash[param] = _to_hash(value)
228
+ end
229
+ hash
230
+ end
231
+
232
+ # Outputs non-array value in the form of hash
233
+ # For object, use to_hash. Otherwise, just return the value
234
+ # @param [Object] value Any valid value
235
+ # @return [Hash] Returns the value in the form of hash
236
+ def _to_hash(value)
237
+ if value.is_a?(Array)
238
+ value.compact.map { |v| _to_hash(v) }
239
+ elsif value.is_a?(Hash)
240
+ {}.tap do |hash|
241
+ value.each { |k, v| hash[k] = _to_hash(v) }
242
+ end
243
+ elsif value.respond_to? :to_hash
244
+ value.to_hash
245
+ else
246
+ value
247
+ end
248
+ end
249
+
250
+ end
251
+
252
+ end
@@ -0,0 +1,239 @@
1
+ =begin
2
+ #Manticore Search Client
3
+
4
+ #Сlient for Manticore Search.
5
+
6
+ The version of the OpenAPI document: 5.0.0
7
+ Contact: info@manticoresearch.com
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.13.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'time'
15
+
16
+ module Manticore::Client
17
+ # Object containing the document data for replacing an existing document in a table.
18
+ class ReplaceDocumentRequest
19
+ # Object containing the new document data to replace the existing one.
20
+ attr_accessor :doc
21
+
22
+ # Attribute mapping from ruby-style variable name to JSON key.
23
+ def self.attribute_map
24
+ {
25
+ :'doc' => :'doc'
26
+ }
27
+ end
28
+
29
+ # Returns attribute mapping this model knows about
30
+ def self.acceptable_attribute_map
31
+ attribute_map
32
+ end
33
+
34
+ # Returns all the JSON keys this model knows about
35
+ def self.acceptable_attributes
36
+ acceptable_attribute_map.values
37
+ end
38
+
39
+ # Attribute type mapping.
40
+ def self.openapi_types
41
+ {
42
+ :'doc' => :'Object'
43
+ }
44
+ end
45
+
46
+ # List of attributes with nullable: true
47
+ def self.openapi_nullable
48
+ Set.new([
49
+ ])
50
+ end
51
+
52
+ # Initializes the object
53
+ # @param [Hash] attributes Model attributes in the form of hash
54
+ def initialize(attributes = {})
55
+ if (!attributes.is_a?(Hash))
56
+ fail ArgumentError, "The input argument (attributes) must be a hash in `Manticore::Client::ReplaceDocumentRequest` initialize method"
57
+ end
58
+
59
+ # check to see if the attribute exists and convert string to symbol for hash key
60
+ acceptable_attribute_map = self.class.acceptable_attribute_map
61
+ attributes = attributes.each_with_object({}) { |(k, v), h|
62
+ if (!acceptable_attribute_map.key?(k.to_sym))
63
+ fail ArgumentError, "`#{k}` is not a valid attribute in `Manticore::Client::ReplaceDocumentRequest`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
64
+ end
65
+ h[k.to_sym] = v
66
+ }
67
+
68
+ if attributes.key?(:'doc')
69
+ self.doc = attributes[:'doc']
70
+ else
71
+ self.doc = nil
72
+ end
73
+ end
74
+
75
+ # Show invalid properties with the reasons. Usually used together with valid?
76
+ # @return Array for valid properties with the reasons
77
+ def list_invalid_properties
78
+ warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
79
+ invalid_properties = Array.new
80
+ if @doc.nil?
81
+ invalid_properties.push('invalid value for "doc", doc cannot be nil.')
82
+ end
83
+
84
+ invalid_properties
85
+ end
86
+
87
+ # Check to see if the all the properties in the model are valid
88
+ # @return true if the model is valid
89
+ def valid?
90
+ warn '[DEPRECATED] the `valid?` method is obsolete'
91
+ return false if @doc.nil?
92
+ true
93
+ end
94
+
95
+ # Custom attribute writer method with validation
96
+ # @param [Object] doc Value to be assigned
97
+ def doc=(doc)
98
+ if doc.nil?
99
+ fail ArgumentError, 'doc cannot be nil'
100
+ end
101
+
102
+ @doc = doc
103
+ end
104
+
105
+ # Checks equality by comparing each attribute.
106
+ # @param [Object] Object to be compared
107
+ def ==(o)
108
+ return true if self.equal?(o)
109
+ self.class == o.class &&
110
+ doc == o.doc
111
+ end
112
+
113
+ # @see the `==` method
114
+ # @param [Object] Object to be compared
115
+ def eql?(o)
116
+ self == o
117
+ end
118
+
119
+ # Calculates hash code according to all attributes.
120
+ # @return [Integer] Hash code
121
+ def hash
122
+ [doc].hash
123
+ end
124
+
125
+ # Builds the object from hash
126
+ # @param [Hash] attributes Model attributes in the form of hash
127
+ # @return [Object] Returns the model itself
128
+ def self.build_from_hash(attributes)
129
+ return nil unless attributes.is_a?(Hash)
130
+ attributes = attributes.transform_keys(&:to_sym)
131
+ transformed_hash = {}
132
+ openapi_types.each_pair do |key, type|
133
+ if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
134
+ transformed_hash["#{key}"] = nil
135
+ elsif type =~ /\AArray<(.*)>/i
136
+ # check to ensure the input is an array given that the attribute
137
+ # is documented as an array but the input is not
138
+ if attributes[attribute_map[key]].is_a?(Array)
139
+ transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
140
+ end
141
+ elsif !attributes[attribute_map[key]].nil?
142
+ transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
143
+ end
144
+ end
145
+ new(transformed_hash)
146
+ end
147
+
148
+ # Deserializes the data based on type
149
+ # @param string type Data type
150
+ # @param string value Value to be deserialized
151
+ # @return [Object] Deserialized data
152
+ def self._deserialize(type, value)
153
+ case type.to_sym
154
+ when :Time
155
+ Time.parse(value)
156
+ when :Date
157
+ Date.parse(value)
158
+ when :String
159
+ value.to_s
160
+ when :Integer
161
+ value.to_i
162
+ when :Float
163
+ value.to_f
164
+ when :Boolean
165
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
166
+ true
167
+ else
168
+ false
169
+ end
170
+ when :Object
171
+ # generic object (usually a Hash), return directly
172
+ value
173
+ when /\AArray<(?<inner_type>.+)>\z/
174
+ inner_type = Regexp.last_match[:inner_type]
175
+ value.map { |v| _deserialize(inner_type, v) }
176
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
177
+ k_type = Regexp.last_match[:k_type]
178
+ v_type = Regexp.last_match[:v_type]
179
+ {}.tap do |hash|
180
+ value.each do |k, v|
181
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
182
+ end
183
+ end
184
+ else # model
185
+ # models (e.g. Pet) or oneOf
186
+ klass = Manticore::Client.const_get(type)
187
+ klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
188
+ end
189
+ end
190
+
191
+ # Returns the string representation of the object
192
+ # @return [String] String presentation of the object
193
+ def to_s
194
+ to_hash.to_s
195
+ end
196
+
197
+ # to_body is an alias to to_hash (backward compatibility)
198
+ # @return [Hash] Returns the object in the form of hash
199
+ def to_body
200
+ to_hash
201
+ end
202
+
203
+ # Returns the object in the form of hash
204
+ # @return [Hash] Returns the object in the form of hash
205
+ def to_hash
206
+ hash = {}
207
+ self.class.attribute_map.each_pair do |attr, param|
208
+ value = self.send(attr)
209
+ if value.nil?
210
+ is_nullable = self.class.openapi_nullable.include?(attr)
211
+ next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
212
+ end
213
+
214
+ hash[param] = _to_hash(value)
215
+ end
216
+ hash
217
+ end
218
+
219
+ # Outputs non-array value in the form of hash
220
+ # For object, use to_hash. Otherwise, just return the value
221
+ # @param [Object] value Any valid value
222
+ # @return [Hash] Returns the value in the form of hash
223
+ def _to_hash(value)
224
+ if value.is_a?(Array)
225
+ value.compact.map { |v| _to_hash(v) }
226
+ elsif value.is_a?(Hash)
227
+ {}.tap do |hash|
228
+ value.each { |k, v| hash[k] = _to_hash(v) }
229
+ end
230
+ elsif value.respond_to? :to_hash
231
+ value.to_hash
232
+ else
233
+ value
234
+ end
235
+ end
236
+
237
+ end
238
+
239
+ end
@@ -0,0 +1,105 @@
1
+ =begin
2
+ #Manticore Search Client
3
+
4
+ #Сlient for Manticore Search.
5
+
6
+ The version of the OpenAPI document: 5.0.0
7
+ Contact: info@manticoresearch.com
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.13.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'time'
15
+
16
+ module Manticore::Client
17
+ module ResponseError
18
+ class << self
19
+ # List of class defined in oneOf (OpenAPI v3)
20
+ def openapi_one_of
21
+ [
22
+ :'ResponseErrorDetails',
23
+ :'String'
24
+ ]
25
+ end
26
+
27
+ # Builds the object
28
+ # @param [Mixed] Data to be matched against the list of oneOf items
29
+ # @return [Object] Returns the model or the data itself
30
+ def build(data)
31
+ # Go through the list of oneOf items and attempt to identify the appropriate one.
32
+ # Note:
33
+ # - We do not attempt to check whether exactly one item matches.
34
+ # - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 })
35
+ # due to the way the deserialization is made in the base_object template (it just casts without verifying).
36
+ # - TODO: scalar values are de facto behaving as if they were nullable.
37
+ # - TODO: logging when debugging is set.
38
+ openapi_one_of.each do |klass|
39
+ begin
40
+ next if klass == :AnyType # "nullable: true"
41
+ typed_data = find_and_cast_into_type(klass, data)
42
+ return typed_data if typed_data
43
+ rescue # rescue all errors so we keep iterating even if the current item lookup raises
44
+ end
45
+ end
46
+
47
+ openapi_one_of.include?(:AnyType) ? data : nil
48
+ end
49
+
50
+ private
51
+
52
+ SchemaMismatchError = Class.new(StandardError)
53
+
54
+ # Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse.
55
+ def find_and_cast_into_type(klass, data)
56
+ return if data.nil?
57
+
58
+ case klass.to_s
59
+ when 'Boolean'
60
+ return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass)
61
+ when 'Float'
62
+ return data if data.instance_of?(Float)
63
+ when 'Integer'
64
+ return data if data.instance_of?(Integer)
65
+ when 'Time'
66
+ return Time.parse(data)
67
+ when 'Date'
68
+ return Date.parse(data)
69
+ when 'String'
70
+ return data if data.instance_of?(String)
71
+ when 'Object' # "type: object"
72
+ return data if data.instance_of?(Hash)
73
+ when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
74
+ if data.instance_of?(Array)
75
+ sub_type = Regexp.last_match[:sub_type]
76
+ return data.map { |item| find_and_cast_into_type(sub_type, item) }
77
+ end
78
+ when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }"
79
+ if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) }
80
+ sub_type = Regexp.last_match[:sub_type]
81
+ return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) }
82
+ end
83
+ else # model
84
+ const = Manticore::Client.const_get(klass)
85
+ if const
86
+ if const.respond_to?(:openapi_one_of) # nested oneOf model
87
+ model = const.build(data)
88
+ return model if model
89
+ else
90
+ # raise if data contains keys that are not known to the model
91
+ raise if const.respond_to?(:acceptable_attributes) && !(data.keys - const.acceptable_attributes).empty?
92
+ model = const.build_from_hash(data)
93
+ return model if model
94
+ end
95
+ end
96
+ end
97
+
98
+ raise # if no match by now, raise
99
+ rescue
100
+ raise SchemaMismatchError, "#{data} doesn't match the #{klass} type"
101
+ end
102
+ end
103
+ end
104
+
105
+ end