|
| 1 | +# Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT. |
| 2 | + |
| 3 | +require "date" |
| 4 | +require "time" |
| 5 | + |
| 6 | +module Algolia |
| 7 | + module Search |
| 8 | + module BuiltInOperationValue |
| 9 | + class << self |
| 10 | + # List of class defined in oneOf (OpenAPI v3) |
| 11 | + def openapi_one_of |
| 12 | + [ |
| 13 | + :"Integer", |
| 14 | + :"String" |
| 15 | + ] |
| 16 | + end |
| 17 | + |
| 18 | + # Builds the object |
| 19 | + # @param [Mixed] Data to be matched against the list of oneOf items |
| 20 | + # @return [Object] Returns the model or the data itself |
| 21 | + def build(data) |
| 22 | + # Go through the list of oneOf items and attempt to identify the appropriate one. |
| 23 | + # Note: |
| 24 | + # - We do not attempt to check whether exactly one item matches. |
| 25 | + # - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 }) |
| 26 | + # due to the way the deserialization is made in the base_object template (it just casts without verifying). |
| 27 | + # - TODO: scalar values are de facto behaving as if they were nullable. |
| 28 | + # - TODO: logging when debugging is set. |
| 29 | + openapi_one_of.each do |klass| |
| 30 | + begin |
| 31 | + # "nullable: true" |
| 32 | + next if klass == :AnyType |
| 33 | + typed_data = find_and_cast_into_type(klass, data) |
| 34 | + return typed_data if typed_data |
| 35 | + # rescue all errors so we keep iterating even if the current item lookup raises |
| 36 | + rescue |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + openapi_one_of.include?(:AnyType) ? data : nil |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + |
| 45 | + SchemaMismatchError = Class.new(StandardError) |
| 46 | + |
| 47 | + # Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse. |
| 48 | + def find_and_cast_into_type(klass, data) |
| 49 | + return if data.nil? |
| 50 | + |
| 51 | + case klass.to_s |
| 52 | + when "Boolean" |
| 53 | + return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass) |
| 54 | + when "Float" |
| 55 | + return data if data.instance_of?(Float) |
| 56 | + when "Integer" |
| 57 | + return data if data.instance_of?(Integer) |
| 58 | + when "Time" |
| 59 | + return Time.parse(data) |
| 60 | + when "Date" |
| 61 | + return Date.parse(data) |
| 62 | + when "String" |
| 63 | + return data if data.instance_of?(String) |
| 64 | + # "type: object" |
| 65 | + when "Object" |
| 66 | + return data if data.instance_of?(Hash) |
| 67 | + # "type: array" |
| 68 | + when /\AArray<(?<sub_type>.+)>\z/ |
| 69 | + if data.instance_of?(Array) |
| 70 | + sub_type = Regexp.last_match[:sub_type] |
| 71 | + return data.map { |item| find_and_cast_into_type(sub_type, item) } |
| 72 | + end |
| 73 | + # "type: object" with "additionalProperties: { ... }" |
| 74 | + when /\AHash<String, (?<sub_type>.+)>\z/ |
| 75 | + if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) } |
| 76 | + sub_type = Regexp.last_match[:sub_type] |
| 77 | + return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) } |
| 78 | + end |
| 79 | + # model |
| 80 | + else |
| 81 | + const = Algolia::Search.const_get(klass) |
| 82 | + if const |
| 83 | + # nested oneOf model |
| 84 | + if const.respond_to?(:openapi_one_of) |
| 85 | + model = const.build(data) |
| 86 | + else |
| 87 | + # raise if data contains keys that are not known to the model |
| 88 | + raise unless (data.keys - const.acceptable_attributes).empty? |
| 89 | + model = const.build_from_hash(data) |
| 90 | + end |
| 91 | + |
| 92 | + return model if model |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + # if no match by now, raise |
| 97 | + raise |
| 98 | + rescue |
| 99 | + raise SchemaMismatchError, "#{data} doesn't match the #{klass} type" |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + end |
| 105 | +end |
0 commit comments